vendor/sylius/shop-api-plugin/src/Controller/Product/ShowLatestProductAction.php line 23

  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  * (c) Paweł Jędrzejewski
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. declare(strict_types=1);
  9. namespace Sylius\ShopApiPlugin\Controller\Product;
  10. use FOS\RestBundle\View\View;
  11. use FOS\RestBundle\View\ViewHandlerInterface;
  12. use Sylius\Component\Channel\Context\ChannelContextInterface;
  13. use Sylius\Component\Channel\Context\ChannelNotFoundException;
  14. use Sylius\ShopApiPlugin\ViewRepository\Product\ProductLatestViewRepositoryInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  18. final class ShowLatestProductAction
  19. {
  20.     /** @var ViewHandlerInterface */
  21.     private $viewHandler;
  22.     /** @var ProductLatestViewRepositoryInterface */
  23.     private $productLatestQuery;
  24.     /** @var ChannelContextInterface */
  25.     private $channelContext;
  26.     public function __construct(
  27.         ViewHandlerInterface $viewHandler,
  28.         ProductLatestViewRepositoryInterface $productLatestQuery,
  29.         ChannelContextInterface $channelContext,
  30.     ) {
  31.         $this->viewHandler $viewHandler;
  32.         $this->productLatestQuery $productLatestQuery;
  33.         $this->channelContext $channelContext;
  34.     }
  35.     public function __invoke(Request $request): Response
  36.     {
  37.         try {
  38.             $channel $this->channelContext->getChannel();
  39.             return $this->viewHandler->handle(View::create($this->productLatestQuery->getLatestProducts(
  40.                 $channel->getCode(),
  41.                 $request->query->get('locale'),
  42.                 $request->query->getInt('limit'4),
  43.             ), Response::HTTP_OK));
  44.         } catch (ChannelNotFoundException $exception) {
  45.             throw new NotFoundHttpException('Channel has not been found.');
  46.         } catch (\InvalidArgumentException $exception) {
  47.             throw new NotFoundHttpException($exception->getMessage());
  48.         }
  49.     }
  50. }