/** * @param Request $request * @param Response $response * @param callable|null $out * @return null|Response */ public function dispatch(Request $request, Response $response, callable $out = null) { $shortCode = $request->getAttribute('shortCode'); $startDate = $this->getDateQueryParam($request, 'startDate'); $endDate = $this->getDateQueryParam($request, 'endDate'); try { $visits = $this->visitsTracker->info($shortCode, new DateRange($startDate, $endDate)); return new JsonResponse(['visits' => ['data' => $visits]]); } catch (InvalidArgumentException $e) { $this->logger->warning('Provided nonexistent shortcode' . PHP_EOL . $e); return new JsonResponse(['error' => RestUtils::getRestErrorCodeFromException($e), 'message' => sprintf($this->translator->translate('Provided short code %s does not exist'), $shortCode)], 404); } catch (\Exception $e) { $this->logger->error('Unexpected error while parsing short code' . PHP_EOL . $e); return new JsonResponse(['error' => RestUtils::UNKNOWN_ERROR, 'message' => $this->translator->translate('Unexpected error occurred')], 500); } }
public function execute(InputInterface $input, OutputInterface $output) { $shortCode = $input->getArgument('shortCode'); $startDate = $this->getDateOption($input, 'startDate'); $endDate = $this->getDateOption($input, 'endDate'); $visits = $this->visitsTracker->info($shortCode, new DateRange($startDate, $endDate)); $table = new Table($output); $table->setHeaders([$this->translator->translate('Referer'), $this->translator->translate('Date'), $this->translator->translate('Remote Address'), $this->translator->translate('User agent')]); foreach ($visits as $row) { $rowData = $row->jsonSerialize(); // Unset location info unset($rowData['visitLocation']); $table->addRow(array_values($rowData)); } $table->render(); }
/** * Process an incoming request and/or response. * * Accepts a server-side request and a response instance, and does * something with them. * * If the response is not complete and/or further processing would not * interfere with the work done in the middleware, or if the middleware * wants to delegate to another process, it can use the `$out` callable * if present. * * If the middleware does not return a value, execution of the current * request is considered complete, and the response instance provided will * be considered the response to return. * * Alternately, the middleware may return a response instance. * * Often, middleware will `return $out();`, with the assumption that a * later middleware will return a response. * * @param Request $request * @param Response $response * @param null|callable $out * @return null|Response */ public function __invoke(Request $request, Response $response, callable $out = null) { $shortCode = $request->getAttribute('shortCode', ''); try { $longUrl = $this->urlShortener->shortCodeToUrl($shortCode); // If provided shortCode does not belong to a valid long URL, dispatch next middleware, which will trigger // a not-found error if (!isset($longUrl)) { return $this->notFoundResponse($request, $response, $out); } // Track visit to this short code $this->visitTracker->track($shortCode, $request); // Return a redirect response to the long URL. // Use a temporary redirect to make sure browsers always hit the server for analytics purposes return new RedirectResponse($longUrl); } catch (\Exception $e) { // In case of error, dispatch 404 error $this->logger->error('Error redirecting to long URL.' . PHP_EOL . $e); return $this->notFoundResponse($request, $response, $out); } }