SiteDefinition::getLocalizedField()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 3
nop 1
dl 0
loc 13
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\CoreBundle\Site;
15
16
use Symfony\Component\DependencyInjection\Attribute\Autowire;
17
use Symfony\Component\HttpFoundation\RequestStack;
18
19
class SiteDefinition implements SiteDefinitionInterface
20
{
21
    public function __construct(
22
        private readonly RequestStack $requestStack,
23
        private readonly array $siteData,
24
        #[Autowire(param: 'kernel.default_locale')]
25
        private readonly string $defaultLocale
26
    ) {
27
    }
28
29
    public function getName(): string
30
    {
31
        return $this->getLocalizedField('sitename');
32
    }
33
34
    public function getSlogan(): string
35
    {
36
        return $this->getLocalizedField('slogan');
37
    }
38
39
    public function getPageTitle(): string
40
    {
41
        $title = $this->getName();
42
        $titleScheme = $this->getLocalizedField('page_title_scheme');
43
        if (!empty($titleScheme)) {
44
            $title = str_replace('#sitename#', $this->getName(), $titleScheme);
45
        }
46
47
        return $title;
48
    }
49
50
    public function getMetaDescription(): string
51
    {
52
        return $this->getLocalizedField('meta_description');
53
    }
54
55
    public function getLogoPath(): ?string
56
    {
57
        return '/bundles/core/images/logo_with_title.png';
58
    }
59
60
    public function getMobileLogoPath(): ?string
61
    {
62
        return '/bundles/core/images/zk-power.png';
63
    }
64
65
    public function getIconPath(): ?string
66
    {
67
        return '/bundles/core/images/icon.png';
68
    }
69
70
    public function getStartController(): ?array
71
    {
72
        return $this->getLocalizedField('start_controller');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getLocaliz...eld('start_controller') returns the type string which is incompatible with the type-hinted return array|null.
Loading history...
73
    }
74
75
    public function getAdminMail(): ?string
76
    {
77
        return $this->getLocalizedField('admin_mail');
78
    }
79
80
    private function getLocale(): string
81
    {
82
        return $this->requestStack->getCurrentRequest()->getLocale();
83
    }
84
85
    private function getLocalizedField(string $fieldName): string|array
86
    {
87
        $locale = $this->getLocale();
88
        if (array_key_exists($locale, $this->siteData) && isset($this->siteData[$locale][$fieldName])) {
89
            return $this->siteData[$locale][$fieldName];
90
        }
91
        if (array_key_exists($this->defaultLocale, $this->siteData) && isset($this->siteData[$this->defaultLocale][$fieldName])) {
92
            return $this->siteData[$this->defaultLocale][$fieldName];
93
        }
94
95
        $firstSiteData = array_values($this->siteData)[0];
96
97
        return $firstSiteData[$fieldName] ?? '';
98
    }
99
}
100