SectionUrlProvider::getTranslations()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
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\SectionInterface;
14
use BitBag\SyliusCmsPlugin\Entity\SectionTranslationInterface;
15
use BitBag\SyliusCmsPlugin\Repository\SectionRepositoryInterface;
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 SectionUrlProvider implements UrlProviderInterface
30
{
31
    /** @var SectionRepositoryInterface */
32
    private $sectionRepository;
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
        SectionRepositoryInterface $sectionRepository,
48
        RouterInterface $router,
49
        UrlFactoryInterface $sitemapUrlFactory,
50
        LocaleContextInterface $localeContext,
51
        ChannelContextInterface $channelContext
52
    ) {
53
        $this->sectionRepository = $sectionRepository;
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_sections';
63
    }
64
65
    public function generate(ChannelInterface $channel): iterable
66
    {
67
        $urls = [];
68
69
        foreach ($this->getSections() as $section) {
70
            $urls[] = $this->createSectionUrl($section);
71
        }
72
73
        return $urls;
74
    }
75
76
    private function getTranslations(SectionInterface $section): Collection
77
    {
78
        return $section->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 getSections(): iterable
89
    {
90
        return $this->sectionRepository->findAll();
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 createSectionUrl(SectionInterface $section): UrlInterface
104
    {
105
        $location = $this->router->generate('bitbag_sylius_cms_plugin_shop_section_show', [
106
            'code' => $section->getCode(),
107
            '_locale' => $this->localeContext->getLocaleCode(),
108
        ]);
109
        $url = $this->sitemapUrlFactory->createNew($location);
110
111
        $url->setChangeFrequency(ChangeFrequency::daily());
112
        $url->setPriority(0.7);
113
114
        /** @var SectionTranslationInterface $translation */
115
        foreach ($this->getTranslations($section) as $translation) {
116
            if (!$translation->getLocale() || !$this->localeInLocaleCodes($translation) || $translation->getLocale() === $this->localeContext->getLocaleCode()) {
117
                continue;
118
            }
119
120
            $location = $this->router->generate('bitbag_sylius_cms_plugin_shop_section_show', [
121
                'code' => $section->getCode(),
122
                '_locale' => $translation->getLocale(),
123
            ]);
124
125
            $url->addAlternative(new AlternativeUrl($location, $translation->getLocale()));
126
        }
127
128
        return $url;
129
    }
130
}
131