VendorUrlProvider::getVendors()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 3
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
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\Entity\VendorInterface;
9
use Odiseo\SyliusVendorPlugin\Entity\VendorTranslation;
10
use Odiseo\SyliusVendorPlugin\Repository\VendorRepositoryInterface;
11
use SitemapPlugin\Factory\UrlFactoryInterface;
12
use SitemapPlugin\Factory\AlternativeUrlFactoryInterface;
13
use SitemapPlugin\Model\ChangeFrequency;
14
use SitemapPlugin\Model\UrlInterface;
15
use SitemapPlugin\Provider\UrlProviderInterface;
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 UrlFactoryInterface */
31
    private $sitemapUrlFactory;
32
33
    /** @var AlternativeUrlFactoryInterface */
34
    private $urlAlternativeFactory;
35
36
    /** @var LocaleContextInterface */
37
    private $localeContext;
38
39
    /** @var ChannelInterface */
40
    private $channel;
41
42
    public function __construct(
43
        VendorRepositoryInterface $vendorRepository,
44
        RouterInterface $router,
45
        UrlFactoryInterface $sitemapUrlFactory,
46
        AlternativeUrlFactoryInterface $urlAlternativeFactory,
47
        LocaleContextInterface $localeContext
48
    ) {
49
        $this->vendorRepository = $vendorRepository;
50
        $this->router = $router;
51
        $this->sitemapUrlFactory = $sitemapUrlFactory;
52
        $this->urlAlternativeFactory = $urlAlternativeFactory;
53
        $this->localeContext = $localeContext;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getName(): string
60
    {
61
        return 'vendors';
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function generate(ChannelInterface $channel): iterable
68
    {
69
        $this->channel = $channel;
70
        $urls = [];
71
72
        foreach ($this->getVendors() as $vendor) {
73
            $urls[] = $this->createVendorUrl($vendor);
74
        }
75
76
        return $urls;
77
    }
78
79
    /**
80
     * @param VendorInterface $vendor
81
     * @return Collection|TranslationInterface[]
82
     *
83
     * @psalm-return Collection<array-key, TranslationInterface>
84
     */
85
    private function getTranslations(VendorInterface $vendor): Collection
86
    {
87
        return $vendor->getTranslations()->filter(function (TranslationInterface $translation): bool {
88
            return $this->localeInLocaleCodes($translation);
89
        });
90
    }
91
92
    /**
93
     * @param TranslationInterface $translation
94
     * @return bool
95
     */
96
    private function localeInLocaleCodes(TranslationInterface $translation): bool
97
    {
98
        return in_array($translation->getLocale(), $this->getLocaleCodes(), true);
99
    }
100
101
    /**
102
     * @return iterable
103
     */
104
    private function getVendors(): iterable
105
    {
106
        return $this->vendorRepository->findByChannel($this->channel);
107
    }
108
109
    /**
110
     * @return array
111
     */
112
    private function getLocaleCodes(): array
113
    {
114
        return $this->channel->getLocales()->map(function (LocaleInterface $locale): ?string {
115
            return $locale->getCode();
116
        })->toArray();
117
    }
118
119
    /**
120
     * @param VendorInterface $vendor
121
     * @return UrlInterface
122
     */
123
    private function createVendorUrl(VendorInterface $vendor): UrlInterface
124
    {
125
        $vendorUrl = $this->sitemapUrlFactory->createNew('');
126
127
        $vendorUrl->setChangeFrequency(ChangeFrequency::daily());
128
        $vendorUrl->setPriority(0.7);
129
130
        if (null !== $vendor->getUpdatedAt()) {
131
            $vendorUrl->setLastModification($vendor->getUpdatedAt());
132
        } elseif (null !== $vendor->getCreatedAt()) {
133
            $vendorUrl->setLastModification($vendor->getCreatedAt());
134
        }
135
136
        /** @var VendorTranslation $translation */
137
        foreach ($this->getTranslations($vendor) as $translation) {
138
            if (null === $translation->getLocale() || !$this->localeInLocaleCodes($translation)) {
139
                continue;
140
            }
141
142
            $location = $this->router->generate('odiseo_sylius_vendor_plugin_shop_vendor_index', [
143
                'slug' => $vendor->getSlug(),
144
                '_locale' => $translation->getLocale(),
145
            ]);
146
147
            if ($translation->getLocale() === $this->localeContext->getLocaleCode()) {
148
                $vendorUrl->setLocation($location);
149
150
                continue;
151
            }
152
153
            $vendorUrl->addAlternative($this->urlAlternativeFactory->createNew($location, $translation->getLocale()));
154
        }
155
156
        return $vendorUrl;
157
    }
158
}
159