/**
  * @param Request $request
  * @param Response $response
  * @param callable|null $out
  * @return null|Response
  */
 public function dispatch(Request $request, Response $response, callable $out = null)
 {
     try {
         $params = $this->queryToListParams($request->getQueryParams());
         $shortUrls = $this->shortUrlService->listShortUrls(...$params);
         return new JsonResponse(['shortUrls' => $this->serializePaginator($shortUrls)]);
     } catch (\Exception $e) {
         $this->logger->error('Unexpected error while listing short URLs.' . 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)
 {
     $page = 1;
     do {
         $shortUrls = $this->shortUrlService->listShortUrls($page);
         $page += 1;
         foreach ($shortUrls as $shortUrl) {
             $this->processUrl($shortUrl->getOriginalUrl(), $output);
         }
     } while ($page <= $shortUrls->count());
     $output->writeln('<info>' . $this->translator->translate('Finished processing all URLs') . '</info>');
 }
Exemple #3
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);
     }
 }
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $page = intval($input->getOption('page'));
     $searchTerm = $input->getOption('searchTerm');
     $tags = $input->getOption('tags');
     $tags = !empty($tags) ? explode(',', $tags) : [];
     $showTags = $input->getOption('showTags');
     $orderBy = $input->getOption('orderBy');
     /** @var QuestionHelper $helper */
     $helper = $this->getHelper('question');
     do {
         $result = $this->shortUrlService->listShortUrls($page, $searchTerm, $tags, $this->processOrderBy($input));
         $page++;
         $table = new Table($output);
         $headers = [$this->translator->translate('Short code'), $this->translator->translate('Original URL'), $this->translator->translate('Date created'), $this->translator->translate('Visits count')];
         if ($showTags) {
             $headers[] = $this->translator->translate('Tags');
         }
         $table->setHeaders($headers);
         foreach ($result as $row) {
             $shortUrl = $row->jsonSerialize();
             if ($showTags) {
                 $shortUrl['tags'] = [];
                 foreach ($row->getTags() as $tag) {
                     $shortUrl['tags'][] = $tag->getName();
                 }
                 $shortUrl['tags'] = implode(', ', $shortUrl['tags']);
             } else {
                 unset($shortUrl['tags']);
             }
             $table->addRow(array_values($shortUrl));
         }
         $table->render();
         if ($this->isLastPage($result)) {
             $continue = false;
             $output->writeln(sprintf('<info>%s</info>', $this->translator->translate('You have reached last page')));
         } else {
             $continue = $helper->ask($input, $output, new ConfirmationQuestion(sprintf('<question>' . $this->translator->translate('Continue with page') . ' <bg=cyan;options=bold>%s</>? (y/N)</question> ', $page), false));
         }
     } while ($continue);
 }