예제 #1
0
 /**
  * @test
  */
 public function correctCodeIsReturnedFromException()
 {
     $this->assertEquals(RestUtils::INVALID_SHORTCODE_ERROR, RestUtils::getRestErrorCodeFromException(new InvalidShortCodeException()));
     $this->assertEquals(RestUtils::INVALID_URL_ERROR, RestUtils::getRestErrorCodeFromException(new InvalidUrlException()));
     $this->assertEquals(RestUtils::INVALID_ARGUMENT_ERROR, RestUtils::getRestErrorCodeFromException(new InvalidArgumentException()));
     $this->assertEquals(RestUtils::INVALID_CREDENTIALS_ERROR, RestUtils::getRestErrorCodeFromException(new AuthenticationException()));
     $this->assertEquals(RestUtils::UNKNOWN_ERROR, RestUtils::getRestErrorCodeFromException(new WrongIpException()));
 }
예제 #2
0
 /**
  * @param Request $request
  * @param Response $response
  * @param callable|null $out
  * @return null|Response
  */
 protected function dispatch(Request $request, Response $response, callable $out = null)
 {
     $shortCode = $request->getAttribute('shortCode');
     $bodyParams = $request->getParsedBody();
     if (!isset($bodyParams['tags'])) {
         return new JsonResponse(['error' => RestUtils::INVALID_ARGUMENT_ERROR, 'message' => $this->translator->translate('A list of tags was not provided')], 400);
     }
     $tags = $bodyParams['tags'];
     try {
         $shortUrl = $this->shortUrlService->setTagsByShortCode($shortCode, $tags);
         return new JsonResponse(['tags' => $shortUrl->getTags()->toArray()]);
     } catch (InvalidShortCodeException $e) {
         return new JsonResponse(['error' => RestUtils::getRestErrorCodeFromException($e), 'message' => sprintf($this->translator->translate('No URL found for short code "%s"'), $shortCode)], 404);
     }
 }
예제 #3
0
 /**
  * @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);
     }
 }
예제 #4
0
 /**
  * @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');
     try {
         $longUrl = $this->urlShortener->shortCodeToUrl($shortCode);
         if (!isset($longUrl)) {
             return new JsonResponse(['error' => RestUtils::INVALID_ARGUMENT_ERROR, 'message' => sprintf($this->translator->translate('No URL found for short code "%s"'), $shortCode)], 404);
         }
         return new JsonResponse(['longUrl' => $longUrl]);
     } catch (InvalidShortCodeException $e) {
         $this->logger->warning('Provided short code with invalid format.' . PHP_EOL . $e);
         return new JsonResponse(['error' => RestUtils::getRestErrorCodeFromException($e), 'message' => sprintf($this->translator->translate('Provided short code "%s" has an invalid format'), $shortCode)], 400);
     } catch (\Exception $e) {
         $this->logger->error('Unexpected error while resolving the URL behind a short code.' . PHP_EOL . $e);
         return new JsonResponse(['error' => RestUtils::UNKNOWN_ERROR, 'message' => $this->translator->translate('Unexpected error occurred')], 500);
     }
 }
예제 #5
0
 /**
  * @param Request $request
  * @param Response $response
  * @param callable|null $out
  * @return null|Response
  */
 public function dispatch(Request $request, Response $response, callable $out = null)
 {
     $postData = $request->getParsedBody();
     if (!isset($postData['longUrl'])) {
         return new JsonResponse(['error' => RestUtils::INVALID_ARGUMENT_ERROR, 'message' => $this->translator->translate('A URL was not provided')], 400);
     }
     $longUrl = $postData['longUrl'];
     $tags = isset($postData['tags']) && is_array($postData['tags']) ? $postData['tags'] : [];
     try {
         $shortCode = $this->urlShortener->urlToShortCode(new Uri($longUrl), $tags);
         $shortUrl = (new Uri())->withPath($shortCode)->withScheme($this->domainConfig['schema'])->withHost($this->domainConfig['hostname']);
         return new JsonResponse(['longUrl' => $longUrl, 'shortUrl' => $shortUrl->__toString(), 'shortCode' => $shortCode]);
     } catch (InvalidUrlException $e) {
         $this->logger->warning('Provided Invalid URL.' . PHP_EOL . $e);
         return new JsonResponse(['error' => RestUtils::getRestErrorCodeFromException($e), 'message' => sprintf($this->translator->translate('Provided URL %s is invalid. Try with a different one.'), $longUrl)], 400);
     } catch (\Exception $e) {
         $this->logger->error('Unexpected error creating shortcode.' . PHP_EOL . $e);
         return new JsonResponse(['error' => RestUtils::UNKNOWN_ERROR, 'message' => $this->translator->translate('Unexpected error occurred')], 500);
     }
 }