Failed Conditions
Pull Request — master (#237)
by lei
02:15
created

ProductLatestViewRepository::getChannel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\ShopApiPlugin\ViewRepository;
6
7
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
8
use Sylius\Component\Core\Model\ChannelInterface;
9
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
10
use Sylius\Component\Locale\Model\LocaleInterface;
11
use Sylius\ShopApiPlugin\Factory\ProductViewFactoryInterface;
12
use Sylius\ShopApiPlugin\View\ProductListView;
13
use Webmozart\Assert\Assert;
14
15
final class ProductLatestViewRepository implements ProductLatestViewRepositoryInterface
16
{
17
    /** @var ChannelRepositoryInterface */
18
    private $channelRepository;
19
20
    /** @var ProductRepositoryInterface  */
21
    private $productRepository;
22
23
    /** @var ProductViewFactoryInterface */
24
    private $productViewFactory;
25
26
    /**
27
     * @param ChannelRepositoryInterface $channelRepository
28
     * @param ProductRepositoryInterface $productRepository
29
     * @param ProductViewFactoryInterface $productViewFactory
30
     */
31
    public function __construct(
32
        ChannelRepositoryInterface $channelRepository,
33
        ProductRepositoryInterface $productRepository,
34
        ProductViewFactoryInterface $productViewFactory
35
    ) {
36
        $this->channelRepository = $channelRepository;
37
        $this->productRepository = $productRepository;
38
        $this->productViewFactory = $productViewFactory;
39
    }
40
41
    public function getLatestProducts(string $channelCode, ?string $localeCode, int $count): ProductListView
42
    {
43
        $channel = $this->getChannel($channelCode);
44
        $localeCode = $this->getLocaleCode($localeCode, $channel);
45
        $latestProducts = $this->productRepository->findLatestByChannel($channel, $localeCode, $count);
46
47
        Assert::notNull($latestProducts, sprintf('Latest Products not found in %s locale.', $localeCode));
48
49
        $productListView = new ProductListView();
50
51
        foreach ($latestProducts as $product) {
52
            $productListView->items[] = $this->productViewFactory->create($product, $channel, $localeCode);
53
        }
54
55
        return $productListView;
56
    }
57
58
    private function getChannel(string $channelCode): ChannelInterface
59
    {
60
        /** @var ChannelInterface $channel */
61
        $channel = $this->channelRepository->findOneByCode($channelCode);
62
63
        Assert::notNull($channel, sprintf('Channel with code %s has not been found.', $channelCode));
64
65
        return $channel;
66
    }
67
68
    private function getLocaleCode(?string $localeCode, ChannelInterface $channel): string
69
    {
70
        $localeCode = $localeCode ?? $channel->getDefaultLocale()->getCode();
71
        $this->assertLocaleSupport($localeCode, $channel->getLocales());
72
73
        return $localeCode;
74
    }
75
76
    /**
77
     * @param string $localeCode
78
     * @param iterable|LocaleInterface[] $supportedLocales
79
     */
80
    private function assertLocaleSupport(string $localeCode, iterable $supportedLocales):void
81
    {
82
        $supportedLocaleCodes = [];
83
        foreach ($supportedLocales as $locale) {
84
            $supportedLocaleCodes[] = $locale->getCode();
85
        }
86
87
        Assert::oneOf($localeCode, $supportedLocaleCodes);
88
    }
89
}
90