Completed
Branch master (9d8d38)
by Łukasz
03:25
created

ShowProductDetailsAction   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 0
cbo 8
dl 0
loc 71
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B __invoke() 0 25 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 Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
8
use Sylius\Component\Core\Model\ChannelInterface;
9
use Sylius\Component\Core\Model\ProductInterface;
10
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
11
use Sylius\Component\Product\Model\ProductAssociationInterface;
12
use Sylius\ShopApiPlugin\Factory\ProductViewFactoryInterface;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
16
17
final class ShowProductDetailsAction
18
{
19
    /**
20
     * @var ChannelRepositoryInterface
21
     */
22
    private $channelRepository;
23
24
    /**
25
     * @var ProductRepositoryInterface
26
     */
27
    private $productRepository;
28
29
    /**
30
     * @var ViewHandlerInterface
31
     */
32
    private $viewHandler;
33
34
    /**
35
     * @var ProductViewFactoryInterface
36
     */
37
    private $productViewFactory;
38
39
    /**
40
     * @param ChannelRepositoryInterface $channelRepository
41
     * @param ProductRepositoryInterface $productRepository
42
     * @param ViewHandlerInterface $viewHandler
43
     * @param ProductViewFactoryInterface $productViewFactory
44
     */
45
    public function __construct(
46
        ChannelRepositoryInterface $channelRepository,
47
        ProductRepositoryInterface $productRepository,
48
        ViewHandlerInterface $viewHandler,
49
        ProductViewFactoryInterface $productViewFactory
50
    ) {
51
        $this->channelRepository = $channelRepository;
52
        $this->productRepository = $productRepository;
53
        $this->viewHandler = $viewHandler;
54
        $this->productViewFactory = $productViewFactory;
55
    }
56
57
    /**
58
     * @param Request $request
59
     *
60
     * @return Response
61
     */
62
    public function __invoke(Request $request)
63
    {
64
        if (!$request->query->has('channel')) {
65
            throw new NotFoundHttpException('Cannot find product without channel provided');
66
        }
67
68
        /** @var ChannelInterface $channel */
69
        $channelCode = $request->query->get('channel');
70
        $channel = $this->channelRepository->findOneByCode($channelCode);
71
72
        if (null === $channel) {
73
            throw new NotFoundHttpException(sprintf('Channel with code %s has not been found', $channelCode));
74
        }
75
76
        $locale = $request->query->has('locale') ? $request->query->get('locale') : $channel->getDefaultLocale()->getCode();
77
78
        $productSlug = $request->attributes->get('slug');
79
        $product = $this->productRepository->findOneByChannelAndSlug($channel, $locale, $productSlug);
0 ignored issues
show
Compatibility introduced by
$channel of type object<Sylius\Component\...Model\ChannelInterface> is not a sub-type of object<Sylius\Component\...Model\ChannelInterface>. It seems like you assume a child interface of the interface Sylius\Component\Channel\Model\ChannelInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
80
81
        if (null === $product) {
82
            throw new NotFoundHttpException(sprintf('Product with slug %s has not been found in %s locale.', $productSlug, $locale));
83
        }
84
85
        return $this->viewHandler->handle(View::create($this->productViewFactory->create($product, $channel, $locale), Response::HTTP_OK));
0 ignored issues
show
Compatibility introduced by
$channel of type object<Sylius\Component\...Model\ChannelInterface> is not a sub-type of object<Sylius\Component\...Model\ChannelInterface>. It seems like you assume a child interface of the interface Sylius\Component\Channel\Model\ChannelInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
86
    }
87
}
88