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