Completed
Push — master ( ebbacf...be7db5 )
by Łukasz
24:11 queued 15:48
created

ShowProductCatalogAction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 15

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 0
cbo 15
dl 0
loc 96
rs 9.1666

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
B __invoke() 0 42 6
1
<?php
2
3
namespace Sylius\ShopApiPlugin\Controller\Product;
4
5
use FOS\RestBundle\View\View;
6
use FOS\RestBundle\View\ViewHandlerInterface;
7
use Pagerfanta\Adapter\DoctrineORMAdapter;
8
use Pagerfanta\Pagerfanta;
9
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
10
use Sylius\Component\Core\Model\ChannelInterface;
11
use Sylius\Component\Core\Model\TaxonInterface;
12
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
13
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
14
use Sylius\ShopApiPlugin\Factory\PageViewFactoryInterface;
15
use Sylius\ShopApiPlugin\Factory\ProductViewFactoryInterface;
16
use Sylius\ShopApiPlugin\Request\PageViewRequest;
17
use Sylius\ShopApiPlugin\View\PageLinksView;
18
use Sylius\ShopApiPlugin\View\PageView;
19
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
23
use Symfony\Component\Routing\RouterInterface;
24
25
final class ShowProductCatalogAction
26
{
27
    /**
28
     * @var ChannelRepositoryInterface
29
     */
30
    private $channelRepository;
31
32
    /**
33
     * @var ProductRepositoryInterface
34
     */
35
    private $productRepository;
36
37
    /**
38
     * @var TaxonRepositoryInterface
39
     */
40
    private $taxonRepository;
41
42
    /**
43
     * @var ViewHandlerInterface
44
     */
45
    private $viewHandler;
46
47
    /**
48
     * @var ProductViewFactoryInterface
49
     */
50
    private $productViewFactory;
51
52
    /**
53
     * @var PageViewFactoryInterface
54
     */
55
    private $pageViewFactory;
56
57
    public function __construct(
58
        ChannelRepositoryInterface $channelRepository,
59
        ProductRepositoryInterface $productRepository,
60
        TaxonRepositoryInterface $taxonRepository,
61
        ViewHandlerInterface $viewHandler,
62
        ProductViewFactoryInterface $productViewFactory,
63
        PageViewFactoryInterface $pageViewFactory
64
    ) {
65
        $this->channelRepository = $channelRepository;
66
        $this->productRepository = $productRepository;
67
        $this->taxonRepository = $taxonRepository;
68
        $this->viewHandler = $viewHandler;
69
        $this->productViewFactory = $productViewFactory;
70
        $this->pageViewFactory = $pageViewFactory;
71
    }
72
73
    /**
74
     * @param Request $request
75
     *
76
     * @return Response
77
     */
78
    public function __invoke(Request $request)
79
    {
80
        if (!$request->query->has('channel')) {
81
            throw new NotFoundHttpException('Cannot find product without channel provided');
82
        }
83
84
        $channelCode = $request->query->get('channel');
85
        /** @var ChannelInterface $channel */
86
        $channel = $this->channelRepository->findOneByCode($channelCode);
87
88
        if (null === $channel) {
89
            throw new NotFoundHttpException(sprintf('Channel with code %s has not been found', $channelCode));
90
        }
91
92
        $locale = $request->query->has('locale') ? $request->query->get('locale') : $channel->getDefaultLocale()->getCode();
93
94
        $taxonSlug = $request->attributes->get('taxonomy');
95
        /** @var TaxonInterface $taxon */
96
        $taxon = $this->taxonRepository->findOneBySlug($taxonSlug, $locale);
97
98
        if (null === $taxon) {
99
            throw new NotFoundHttpException(sprintf('Taxon with slug %s in locale %s has not been found', $taxonSlug, $locale));
100
        }
101
102
        $queryBuilder = $this->productRepository->createShopListQueryBuilder($channel, $taxon, $locale);
103
104
        $pagerfanta = new Pagerfanta(new DoctrineORMAdapter($queryBuilder));
105
106
        $pagerfanta->setMaxPerPage($request->query->get('limit', 10));
107
        $pagerfanta->setCurrentPage($request->query->get('page', 1));
108
109
        $page = $this->pageViewFactory->create($pagerfanta, $request->attributes->get('_route'), array_merge(
110
            $request->query->all(),
111
            ['taxonomy' => $taxonSlug]
112
        ));
113
114
        foreach ($pagerfanta->getCurrentPageResults() as $currentPageResult) {
115
            $page->items[] = $this->productViewFactory->create($currentPageResult, $channel, $locale);
116
        }
117
118
        return $this->viewHandler->handle(View::create($page, Response::HTTP_OK));
119
    }
120
}
121