Passed
Pull Request — master (#2809)
by
unknown
34:29
created

SiteConfiguration::addSiteConfigurationToArray()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 8
c 2
b 0
f 0
dl 0
loc 14
rs 10
cc 4
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace ApacheSolrForTypo3\Solr\System\Configuration;
19
20
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
21
use TYPO3\CMS\Core\Site\Entity\Site;
22
use TYPO3\CMS\Core\Site\SiteFinder;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25
/**
26
 * This class represents the configuration from the site configuration
27
 *
28
 * @author Lars Tode <[email protected]>
29
 */
30
class SiteConfiguration implements UnifyConfigurationInterface
31
{
32
    /**
33
     * The root page ID
34
     *
35
     * @var int
36
     */
37
    protected $rootPageUid = 0;
38
39
    /**
40
     * The language uid
41
     *
42
     * @var int
43
     */
44
    protected $languageUid = 0;
45
46
    /**
47
     * @var Site
48
     */
49
    protected $site = null;
50
51
    /**
52
     * The unified configuration
53
     *
54
     * @var array
55
     */
56
    protected $configuration = [];
57
58
    protected $renameConfigurationName = [
59
        'solr_use_write_connection' => 'solr_enabled_write'
60
    ];
61
62
    protected $booleanValues = [
63
        'solr_enabled_read',
64
        'solr_enabled_write',
65
        'solr_use_write_connection'
66
    ];
67
68
    /**
69
     * This constructor contains all required parameters used for other
70
     *
71
     * @param int $pageUid
72
     * @param int $languageUid
73
     */
74
    public function __construct(int $pageUid, int $languageUid = 0)
75
    {
76
        $this->rootPageUid = $pageUid;
77
        $this->languageUid = $languageUid;
78
    }
79
80
    /**
81
     * Return an instance of site configuration.
82
     *
83
     * @param Site $site
84
     * @param int $languageUid
85
     * @return static
86
     */
87
    public static function newWithSite(Site $site, int $languageUid = 0): self
88
    {
89
        $siteConfiguration = new self(
90
            $site->getRootPageId(),
91
            $languageUid
92
        );
93
        $siteConfiguration->site = $site;
94
95
        return $siteConfiguration;
96
    }
97
98
    /**
99
     * {@inheritDoc}
100
     */
101
    public function load(): UnifyConfigurationInterface
102
    {
103
        $site = $this->getSite();
104
        if (!($site instanceof Site)) {
0 ignored issues
show
introduced by
$site is always a sub-type of TYPO3\CMS\Core\Site\Entity\Site.
Loading history...
105
            return $this;
106
        }
107
108
        $this->addSiteConfigurationToArray($site->getConfiguration())
109
            ->addSiteConfigurationToArray($site->getLanguageById($this->languageUid)->toArray());
110
111
        return $this;
112
    }
113
114
    /**
115
     * Add the site configuration
116
     *
117
     * @param array $configuration
118
     * @return $this
119
     */
120
    protected function addSiteConfigurationToArray(array $configuration): SiteConfiguration
121
    {
122
        foreach ($configuration as $name => $value) {
123
            if (substr($name, 0, 5) !== 'solr_') {
124
                continue;
125
            }
126
127
            if (in_array($name, $this->booleanValues)) {
128
                $this->configuration[$name] = filter_var($value, FILTER_VALIDATE_BOOLEAN);
129
            } else {
130
                $this->configuration[$name] = $value;
131
            }
132
        }
133
        return $this;
134
    }
135
136
    /**
137
     * Load the site configuration
138
     *
139
     * @return Site|null
140
     */
141
    protected function getSite(): ?Site
142
    {
143
        if ($this->site instanceof Site) {
0 ignored issues
show
introduced by
$this->site is always a sub-type of TYPO3\CMS\Core\Site\Entity\Site.
Loading history...
144
            return $this->site;
145
        }
146
147
        $siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
148
        try {
149
            /* @var SiteFinder $siteFinder */
150
            $this->site = $siteFinder->getSiteByPageId($this->rootPageUid);
151
        } catch (SiteNotFoundException $e) {
152
            return null;
153
        }
154
155
        return $this->site;
156
    }
157
158
    /**
159
     * {@inheritDoc}
160
     */
161
    public function getUnifiedArray(): array
162
    {
163
        $result = [
164
            'connection' => [
165
                'read' => [],
166
                'write' => [],
167
            ]
168
        ];
169
170
        foreach ($this->configuration as $key => $value) {
171
            if (isset($this->renameConfigurationName[$key])) {
172
                $key = $this->renameConfigurationName[$key];
173
            }
174
            [$prefix, $key, $endpointKey] = explode('_', $key);
175
            $result['connection'][$endpointKey][$key] = $value;
176
        }
177
178
        return $result;
179
    }
180
}
181