PageUrlProvider::generate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusCmsPlugin\SitemapProvider;
12
13
use BitBag\SyliusCmsPlugin\Entity\PageInterface;
14
use BitBag\SyliusCmsPlugin\Entity\PageTranslationInterface;
15
use BitBag\SyliusCmsPlugin\Repository\PageRepositoryInterface;
16
use Doctrine\Common\Collections\Collection;
17
use SitemapPlugin\Factory\UrlFactoryInterface;
0 ignored issues
show
Bug introduced by
The type SitemapPlugin\Factory\UrlFactoryInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use SitemapPlugin\Model\AlternativeUrl;
0 ignored issues
show
Bug introduced by
The type SitemapPlugin\Model\AlternativeUrl was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use SitemapPlugin\Model\ChangeFrequency;
0 ignored issues
show
Bug introduced by
The type SitemapPlugin\Model\ChangeFrequency was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use SitemapPlugin\Model\UrlInterface;
0 ignored issues
show
Bug introduced by
The type SitemapPlugin\Model\UrlInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use SitemapPlugin\Provider\UrlProviderInterface;
0 ignored issues
show
Bug introduced by
The type SitemapPlugin\Provider\UrlProviderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use Sylius\Component\Channel\Context\ChannelContextInterface;
23
use Sylius\Component\Core\Model\ChannelInterface;
24
use Sylius\Component\Locale\Context\LocaleContextInterface;
25
use Sylius\Component\Locale\Model\LocaleInterface;
26
use Sylius\Component\Resource\Model\TranslationInterface;
27
use Symfony\Component\Routing\RouterInterface;
28
29
final class PageUrlProvider implements UrlProviderInterface
30
{
31
    /** @var PageRepositoryInterface */
32
    private $pageRepository;
33
34
    /** @var RouterInterface */
35
    private $router;
36
37
    /** @var UrlFactoryInterface */
38
    private $sitemapUrlFactory;
39
40
    /** @var LocaleContextInterface */
41
    private $localeContext;
42
43
    /** @var ChannelContextInterface */
44
    private $channelContext;
45
46
    public function __construct(
47
        PageRepositoryInterface $pageRepository,
48
        RouterInterface $router,
49
        UrlFactoryInterface $sitemapUrlFactory,
50
        LocaleContextInterface $localeContext,
51
        ChannelContextInterface $channelContext
52
    ) {
53
        $this->pageRepository = $pageRepository;
54
        $this->router = $router;
55
        $this->sitemapUrlFactory = $sitemapUrlFactory;
56
        $this->localeContext = $localeContext;
57
        $this->channelContext = $channelContext;
58
    }
59
60
    public function getName(): string
61
    {
62
        return 'cms_pages';
63
    }
64
65
    public function generate(ChannelInterface $channel): iterable
66
    {
67
        $urls = [];
68
69
        foreach ($this->getPages() as $page) {
70
            $urls[] = $this->createPageUrl($page);
71
        }
72
73
        return $urls;
74
    }
75
76
    private function getTranslations(PageInterface $page): Collection
77
    {
78
        return $page->getTranslations()->filter(function (TranslationInterface $translation) {
79
            return $this->localeInLocaleCodes($translation);
80
        });
81
    }
82
83
    private function localeInLocaleCodes(TranslationInterface $translation): bool
84
    {
85
        return in_array($translation->getLocale(), $this->getLocaleCodes());
86
    }
87
88
    private function getPages(): iterable
89
    {
90
        return $this->pageRepository->findEnabled(true);
91
    }
92
93
    private function getLocaleCodes(): array
94
    {
95
        /** @var ChannelInterface $channel */
96
        $channel = $this->channelContext->getChannel();
97
98
        return $channel->getLocales()->map(function (LocaleInterface $locale) {
99
            return $locale->getCode();
100
        })->toArray();
101
    }
102
103
    private function createPageUrl(PageInterface $page): UrlInterface
104
    {
105
        $location = $this->router->generate('bitbag_sylius_cms_plugin_shop_page_show', [
106
            'slug' => $page->getTranslation($this->localeContext->getLocaleCode())->getSlug(),
0 ignored issues
show
Bug introduced by
The method getSlug() does not exist on Sylius\Component\Resourc...el\TranslationInterface. It seems like you code against a sub-type of Sylius\Component\Resourc...el\TranslationInterface such as Sylius\Component\Product...uctTranslationInterface or BitBag\SyliusCmsPlugin\E...ageTranslationInterface or Sylius\Component\Taxonom...xonTranslationInterface or Sylius\Component\Taxonomy\Model\TaxonTranslation or BitBag\SyliusCmsPlugin\Entity\PageTranslation or Sylius\Component\Product\Model\ProductTranslation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
            'slug' => $page->getTranslation($this->localeContext->getLocaleCode())->/** @scrutinizer ignore-call */ getSlug(),
Loading history...
107
            '_locale' => $this->localeContext->getLocaleCode(),
108
        ]);
109
110
        $pageUrl = $this->sitemapUrlFactory->createNew($location);
111
112
        $pageUrl->setChangeFrequency(ChangeFrequency::daily());
113
        $pageUrl->setPriority(0.7);
114
115
        if ($page->getUpdatedAt()) {
116
            $pageUrl->setLastModification($page->getUpdatedAt());
117
        } elseif ($page->getCreatedAt()) {
118
            $pageUrl->setLastModification($page->getCreatedAt());
119
        }
120
121
        /** @var PageTranslationInterface $translation */
122
        foreach ($this->getTranslations($page) as $translation) {
123
            if (!$translation->getLocale() || !$this->localeInLocaleCodes($translation) || $translation->getLocale() === $this->localeContext->getLocaleCode()) {
124
                continue;
125
            }
126
127
            $location = $this->router->generate('bitbag_sylius_cms_plugin_shop_page_show', [
128
                'slug' => $translation->getSlug(),
129
                '_locale' => $translation->getLocale(),
130
            ]);
131
132
            $pageUrl->addAlternative(new AlternativeUrl($location, $translation->getLocale()));
133
        }
134
135
        return $pageUrl;
136
    }
137
}
138