Failed Conditions
Pull Request — master (#237)
by
unknown
02:14
created

ProductLatestViewRepository::getLocaleCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
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
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
     * ProductLatestViewRepository constructor.
28
     * @param ChannelRepositoryInterface $channelRepository
29
     * @param ProductRepositoryInterface $productRepository
30
     * @param ProductViewFactoryInterface $productViewFactory
31
     */
32
    public function __construct(
33
        ChannelRepositoryInterface $channelRepository,
34
        ProductRepositoryInterface $productRepository,
35
        ProductViewFactoryInterface $productViewFactory
36
    ) {
37
        $this->channelRepository = $channelRepository;
38
        $this->productRepository = $productRepository;
39
        $this->productViewFactory = $productViewFactory;
40
    }
41
42
    public function getLatestProducts(string $channelCode, ?string $localeCode, int $count): ProductListView
43
    {
44
        $channel = $this->getChannel($channelCode);
45
        $localeCode = $this->getLocaleCode($localeCode, $channel);
46
        $latestProducts = $this->productRepository->findLatestByChannel($channel, $localeCode, $count);
47
48
        Assert::notNull($latestProducts, sprintf('Latest Products not found in %s locale.', $localeCode));
49
50
        $productListView = new ProductListView();
51
52
        foreach ($latestProducts as $product) {
53
            $productListView->items[] = $this->productViewFactory->create($product, $channel, $localeCode);
54
        }
55
56
        return $productListView;
57
    }
58
59
    private function getChannel(string $channelCode): ChannelInterface
60
    {
61
        /** @var ChannelInterface $channel */
62
        $channel = $this->channelRepository->findOneByCode($channelCode);
63
64
        Assert::notNull($channel, sprintf('Channel with code %s has not been found.', $channelCode));
65
66
        return $channel;
67
    }
68
69
    private function getLocaleCode(?string $localeCode, ChannelInterface $channel): string
70
    {
71
        $localeCode = $localeCode ?? $channel->getDefaultLocale()->getCode();
72
        $this->assertLocaleSupport($localeCode, $channel->getLocales());
73
74
        return $localeCode;
75
    }
76
77
    /**
78
     * @param string $localeCode
79
     * @param iterable|LocaleInterface[] $supportedLocales
80
     */
81
    private function assertLocaleSupport(string $localeCode, iterable $supportedLocales):void
82
    {
83
        $supportedLocaleCodes = [];
84
        foreach ($supportedLocales as $locale) {
85
            $supportedLocaleCodes[] = $locale->getCode();
86
        }
87
88
        Assert::oneOf($localeCode, $supportedLocaleCodes);
89
    }
90
}
91