1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusVendorPlugin\SitemapProvider; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Collections\Collection; |
8
|
|
|
use Odiseo\SyliusVendorPlugin\Doctrine\ORM\VendorRepositoryInterface; |
9
|
|
|
use Odiseo\SyliusVendorPlugin\Model\VendorInterface; |
10
|
|
|
use Odiseo\SyliusVendorPlugin\Model\VendorTranslationInterface; |
11
|
|
|
use SitemapPlugin\Factory\SitemapUrlFactoryInterface; |
12
|
|
|
use SitemapPlugin\Model\ChangeFrequency; |
13
|
|
|
use SitemapPlugin\Model\SitemapUrlInterface; |
14
|
|
|
use SitemapPlugin\Provider\UrlProviderInterface; |
15
|
|
|
use Sylius\Component\Channel\Context\ChannelContextInterface; |
16
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
17
|
|
|
use Sylius\Component\Locale\Context\LocaleContextInterface; |
18
|
|
|
use Sylius\Component\Locale\Model\LocaleInterface; |
19
|
|
|
use Sylius\Component\Resource\Model\TranslationInterface; |
20
|
|
|
use Symfony\Component\Routing\RouterInterface; |
21
|
|
|
|
22
|
|
|
final class VendorUrlProvider implements UrlProviderInterface |
23
|
|
|
{ |
24
|
|
|
/** @var VendorRepositoryInterface */ |
25
|
|
|
private $vendorRepository; |
26
|
|
|
|
27
|
|
|
/** @var RouterInterface */ |
28
|
|
|
private $router; |
29
|
|
|
|
30
|
|
|
/** @var SitemapUrlFactoryInterface */ |
31
|
|
|
private $sitemapUrlFactory; |
32
|
|
|
|
33
|
|
|
/** @var LocaleContextInterface */ |
34
|
|
|
private $localeContext; |
35
|
|
|
|
36
|
|
|
/** @var ChannelContextInterface */ |
37
|
|
|
private $channelContext; |
38
|
|
|
|
39
|
|
|
public function __construct( |
40
|
|
|
VendorRepositoryInterface $vendorRepository, |
41
|
|
|
RouterInterface $router, |
42
|
|
|
SitemapUrlFactoryInterface $sitemapUrlFactory, |
43
|
|
|
LocaleContextInterface $localeContext, |
44
|
|
|
ChannelContextInterface $channelContext |
45
|
|
|
) { |
46
|
|
|
$this->vendorRepository = $vendorRepository; |
47
|
|
|
$this->router = $router; |
48
|
|
|
$this->sitemapUrlFactory = $sitemapUrlFactory; |
49
|
|
|
$this->localeContext = $localeContext; |
50
|
|
|
$this->channelContext = $channelContext; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getName(): string |
54
|
|
|
{ |
55
|
|
|
return 'vendors'; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function generate(): iterable |
59
|
|
|
{ |
60
|
|
|
$urls = []; |
61
|
|
|
|
62
|
|
|
foreach ($this->getVendors() as $vendor) { |
63
|
|
|
$urls[] = $this->createVendorUrl($vendor); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $urls; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function getTranslations(VendorInterface $vendor): Collection |
70
|
|
|
{ |
71
|
|
|
return $vendor->getTranslations()->filter(function (TranslationInterface $translation) { |
72
|
|
|
return $this->localeInLocaleCodes($translation); |
73
|
|
|
}); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function localeInLocaleCodes(TranslationInterface $translation): bool |
77
|
|
|
{ |
78
|
|
|
return in_array($translation->getLocale(), $this->getLocaleCodes()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function getVendors(): iterable |
82
|
|
|
{ |
83
|
|
|
/** @var ChannelInterface $channel */ |
84
|
|
|
$channel = $this->channelContext->getChannel(); |
85
|
|
|
|
86
|
|
|
return $this->vendorRepository->findByChannel($channel); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function getLocaleCodes(): array |
90
|
|
|
{ |
91
|
|
|
/** @var ChannelInterface $channel */ |
92
|
|
|
$channel = $this->channelContext->getChannel(); |
93
|
|
|
|
94
|
|
|
return $channel->getLocales()->map(function (LocaleInterface $locale) { |
95
|
|
|
return $locale->getCode(); |
96
|
|
|
})->toArray(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function createVendorUrl(VendorInterface $vendor): SitemapUrlInterface |
100
|
|
|
{ |
101
|
|
|
$vendorUrl = $this->sitemapUrlFactory->createNew(); |
102
|
|
|
|
103
|
|
|
$vendorUrl->setChangeFrequency(ChangeFrequency::daily()); |
104
|
|
|
$vendorUrl->setPriority(0.7); |
105
|
|
|
|
106
|
|
|
if ($vendor->getUpdatedAt()) { |
107
|
|
|
$vendorUrl->setLastModification($vendor->getUpdatedAt()); |
108
|
|
|
} elseif ($vendor->getCreatedAt()) { |
109
|
|
|
$vendorUrl->setLastModification($vendor->getCreatedAt()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** @var VendorTranslationInterface $translation */ |
113
|
|
|
foreach ($this->getTranslations($vendor) as $translation) { |
114
|
|
|
if (!$translation->getLocale() || !$this->localeInLocaleCodes($translation)) { |
115
|
|
|
continue; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$location = $this->router->generate('odiseo_sylius_vendor_shop_vendor_show', [ |
119
|
|
|
'slug' => $vendor->getSlug(), |
120
|
|
|
'_locale' => $translation->getLocale(), |
121
|
|
|
]); |
122
|
|
|
|
123
|
|
|
if ($translation->getLocale() === $this->localeContext->getLocaleCode()) { |
124
|
|
|
$vendorUrl->setLocalization($location); |
125
|
|
|
|
126
|
|
|
continue; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$vendorUrl->addAlternative($location, $translation->getLocale()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $vendorUrl; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|