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

ShowReviewsAction   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 14

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 0
cbo 14
dl 0
loc 76
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
B __invoke() 0 34 5
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\ArrayAdapter;
8
use Pagerfanta\Pagerfanta;
9
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
10
use Sylius\Component\Core\Model\ChannelInterface;
11
use Sylius\Component\Core\Repository\ProductReviewRepositoryInterface;
12
use Sylius\ShopApiPlugin\Factory\PageViewFactoryInterface;
13
use Sylius\ShopApiPlugin\Factory\ProductReviewViewFactoryInterface;
14
use Sylius\ShopApiPlugin\Factory\ProductViewFactoryInterface;
15
use Sylius\ShopApiPlugin\Request\PageViewRequest;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
19
20
final class ShowReviewsAction
21
{
22
    /**
23
     * @var ChannelRepositoryInterface
24
     */
25
    private $channelRepository;
26
27
    /**
28
     * @var ProductReviewRepositoryInterface
29
     */
30
    private $productReviewRepository;
31
32
    /**
33
     * @var ViewHandlerInterface
34
     */
35
    private $viewHandler;
36
37
    /**
38
     * @var ProductReviewViewFactoryInterface
39
     */
40
    private $productReviewViewFactory;
41
42
    /**
43
     * @var PageViewFactoryInterface
44
     */
45
    private $pageViewFactory;
46
47
    public function __construct(
48
        ChannelRepositoryInterface $channelRepository,
49
        ProductReviewRepositoryInterface $productReviewRepository,
50
        ViewHandlerInterface $viewHandler,
51
        ProductReviewViewFactoryInterface $productReviewViewFactory,
52
        PageViewFactoryInterface $pageViewFactory
53
    ) {
54
        $this->channelRepository = $channelRepository;
55
        $this->productReviewRepository = $productReviewRepository;
56
        $this->viewHandler = $viewHandler;
57
        $this->productReviewViewFactory = $productReviewViewFactory;
58
        $this->pageViewFactory = $pageViewFactory;
59
    }
60
61
    public function __invoke(Request $request): Response
62
    {
63
        if (!$request->query->has('channel')) {
64
            throw new NotFoundHttpException('Cannot find product without channel provided');
65
        }
66
67
        $channelCode = $request->query->get('channel');
68
        /** @var ChannelInterface $channel */
69
        $channel = $this->channelRepository->findOneByCode($channelCode);
70
71
        if (null === $channel) {
72
            throw new NotFoundHttpException(sprintf('Channel with code %s has not been found', $channelCode));
73
        }
74
75
        $locale = $request->query->has('locale') ? $request->query->get('locale') : $channel->getDefaultLocale()->getCode();
76
77
        $reviews = $this->productReviewRepository->findAcceptedByProductSlugAndChannel($request->attributes->get('slug'), $locale, $channel);
78
79
        $pagerfanta = new Pagerfanta(new ArrayAdapter($reviews));
80
81
        $pagerfanta->setMaxPerPage($request->query->get('limit', 10));
82
        $pagerfanta->setCurrentPage($request->query->get('page', 1));
83
84
        $page = $this->pageViewFactory->create($pagerfanta, $request->attributes->get('_route'), array_merge(
85
            $request->query->all(),
86
            ['slug' => $request->attributes->get('slug')]
87
        ));
88
89
        foreach ($pagerfanta->getCurrentPageResults() as $currentPageResult) {
90
            $page->items[] = $this->productReviewViewFactory->create($currentPageResult);
91
        }
92
93
        return $this->viewHandler->handle(View::create($page, Response::HTTP_OK));
94
    }
95
}
96