Completed
Pull Request — master (#169)
by Mikołaj
05:25
created

PageUrlProvider   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 0
dl 0
loc 122
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A getName() 0 4 1
A generate() 0 8 2
A getTranslations() 0 6 1
A localeInLocaleCodes() 0 4 1
A getPages() 0 4 1
A getLocaleCodes() 0 13 2
B createPageUrl() 0 39 7
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusCmsPlugin\SitemapProvider;
14
15
use BitBag\SyliusCmsPlugin\Entity\PageContentInterface;
16
use BitBag\SyliusCmsPlugin\Entity\PageTranslationInterface;
17
use BitBag\SyliusCmsPlugin\Repository\PageRepositoryInterface;
18
use Doctrine\Common\Collections\Collection;
19
use SitemapPlugin\Factory\SitemapUrlFactoryInterface;
20
use SitemapPlugin\Model\ChangeFrequency;
21
use SitemapPlugin\Model\SitemapUrlInterface;
22
use SitemapPlugin\Provider\UrlProviderInterface;
23
use Sylius\Component\Channel\Context\ChannelContextInterface;
24
use Sylius\Component\Core\Model\ChannelInterface;
25
use Sylius\Component\Locale\Context\LocaleContextInterface;
26
use Sylius\Component\Locale\Model\LocaleInterface;
27
use Sylius\Component\Resource\Model\TranslationInterface;
28
use Symfony\Component\Routing\RouterInterface;
29
30
class PageUrlProvider implements UrlProviderInterface
31
{
32
    /** @var PageRepositoryInterface */
33
    private $pageRepository;
34
35
    /** @var RouterInterface */
36
    private $router;
37
38
    /** @var SitemapUrlFactoryInterface */
39
    private $sitemapUrlFactory;
40
41
    /** @var LocaleContextInterface */
42
    private $localeContext;
43
44
    /** @var ChannelContextInterface */
45
    private $channelContext;
46
47
    /** @var array */
48
    private $urls = [];
49
50
    /** @var */
51
    private $channelLocaleCodes;
52
53
    public function __construct(
54
        PageRepositoryInterface $pageRepository,
55
        RouterInterface $router,
56
        SitemapUrlFactoryInterface $sitemapUrlFactory,
57
        LocaleContextInterface $localeContext,
58
        ChannelContextInterface $channelContext
59
    ) {
60
        $this->pageRepository = $pageRepository;
61
        $this->router = $router;
62
        $this->sitemapUrlFactory = $sitemapUrlFactory;
63
        $this->localeContext = $localeContext;
64
        $this->channelContext = $channelContext;
65
    }
66
67
    public function getName(): string
68
    {
69
        return 'cms_pages';
70
    }
71
72
    public function generate(): iterable
73
    {
74
        foreach ($this->getPages() as $product) {
75
            $this->urls[] = $this->createPageUrl($product);
76
        }
77
78
        return $this->urls;
79
    }
80
81
    private function getTranslations(PageContentInterface $page): Collection
82
    {
83
        return $page->getTranslations()->filter(function (TranslationInterface $translation) {
84
            return $this->localeInLocaleCodes($translation);
85
        });
86
    }
87
88
    private function localeInLocaleCodes(TranslationInterface $translation): bool
89
    {
90
        return in_array($translation->getLocale(), $this->getLocaleCodes());
91
    }
92
93
    private function getPages(): iterable
94
    {
95
        return $this->pageRepository->findByEnabled(true);
96
    }
97
98
    private function getLocaleCodes(): array
99
    {
100
        if (null === $this->channelLocaleCodes) {
101
            /** @var ChannelInterface $channel */
102
            $channel = $this->channelContext->getChannel();
103
104
            $this->channelLocaleCodes = $channel->getLocales()->map(function (LocaleInterface $locale) {
105
                return $locale->getCode();
106
            })->toArray();
107
        }
108
109
        return $this->channelLocaleCodes;
110
    }
111
112
    private function createPageUrl(PageContentInterface $page): SitemapUrlInterface
113
    {
114
        $pageUrl = $this->sitemapUrlFactory->createNew();
115
116
        $pageUrl->setChangeFrequency(ChangeFrequency::daily());
117
        $pageUrl->setPriority(0.7);
118
119
        if ($page->getUpdatedAt()) {
120
            $pageUrl->setLastModification($page->getUpdatedAt());
121
        } elseif ($page->getCreatedAt()) {
122
            $pageUrl->setLastModification($page->getCreatedAt());
123
        }
124
125
        /** @var PageTranslationInterface $translation */
126
        foreach ($this->getTranslations($page) as $translation) {
127
            if (!$translation->getLocale()) {
128
                continue;
129
            }
130
131
            if (!$this->localeInLocaleCodes($translation)) {
132
                continue;
133
            }
134
135
            $location = $this->router->generate('bitbag_sylius_cms_plugin_shop_page_show', [
136
                'slug' => $translation->getSlug(),
137
                '_locale' => $translation->getLocale(),
138
            ]);
139
140
            if ($translation->getLocale() === $this->localeContext->getLocaleCode()) {
141
                $pageUrl->setLocalization($location);
142
143
                continue;
144
            }
145
146
            $pageUrl->addAlternative($location, $translation->getLocale());
147
        }
148
149
        return $pageUrl;
150
    }
151
}
152