Passed
Pull Request — main (#3378)
by Mario
50:21 queued 18:39
created

SiteHandlingStatus::getStatus()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.3332

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 24
ccs 6
cts 9
cp 0.6667
rs 9.8666
c 1
b 0
f 0
cc 3
nc 3
nop 0
crap 3.3332
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\Report;
19
20
use ApacheSolrForTypo3\Solr\Domain\Site\Site;
21
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
22
use ApacheSolrForTypo3\Solr\System\Configuration\ExtensionConfiguration;
23
use Doctrine\DBAL\Driver\Exception as DBALDriverException;
24
use Psr\Http\Message\UriInterface;
25
use Throwable;
26
use TYPO3\CMS\Core\Site\Entity\Site as Typo3Site;
27
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
29
use TYPO3\CMS\Reports\Status;
30
31
/**
32
 * Provides a status report about current state of site handling configurations.
33
 *
34
 * Following things are checked currently:
35
 * * Entry Point[base] scheme expects -> http[s]
36
 * * Entry Point[base] authority expects -> [user-info@]host[:port]
37
 */
38
class SiteHandlingStatus extends AbstractSolrStatus
39
{
40
    const TITLE_SITE_HANDLING_CONFIGURATION = 'Site handling configuration';
41
42
    /**
43
     * @var string
44
     */
45
    const
46
        CSS_STATUS_NOTICE = 'notice',
47
    CSS_STATUS_INFO = 'info',
48
    CSS_STATUS_OK = 'success',
49
    CSS_STATUS_WARNING = 'warning',
50
    CSS_STATUS_ERROR = 'danger';
51
52
    /**
53
     * Site Repository
54
     *
55
     * @var SiteRepository
56
     */
57
    protected $siteRepository;
58
59
    /**
60
     * @var ExtensionConfiguration
61
     */
62
    protected $extensionConfiguration;
63
64
    /**
65
     * SolrStatus constructor.
66
     * @param ExtensionConfiguration|null $extensionConfiguration
67
     * @param SiteRepository|null $siteRepository
68
     */
69 4
    public function __construct(
70
        ExtensionConfiguration $extensionConfiguration = null,
71
        SiteRepository $siteRepository = null
72
    ) {
73 4
        $this->extensionConfiguration = $extensionConfiguration ?? GeneralUtility::makeInstance(ExtensionConfiguration::class);
74 4
        $this->siteRepository = $siteRepository ?? GeneralUtility::makeInstance(SiteRepository::class);
75
    }
76
77
    /**
78
     * @return array
79
     *
80
     * @throws DBALDriverException
81
     * @throws Throwable
82
     * @noinspection PhpMissingReturnTypeInspection see {@link \TYPO3\CMS\Reports\StatusProviderInterface::getStatus()}
83
     */
84 4
    public function getStatus()
85
    {
86 4
        $reports = [];
87
88
        /* @var Site $site */
89 4
        foreach ($this->siteRepository->getAvailableSites() as $site) {
90 4
            if (!($site instanceof Site)) {
91
                $reports[] = GeneralUtility::makeInstance(
92
                    Status::class,
93
                    /** @scrutinizer ignore-type */
94
                    self::TITLE_SITE_HANDLING_CONFIGURATION,
95
                    /** @scrutinizer ignore-type */
96
                    'Something went wrong',
97
                    /** @scrutinizer ignore-type */
98
                    vsprintf('The configured Site "%s" is not TYPO3 managed site. Please refer to TYPO3 site management docs and configure the site properly.', [$site->getLabel()]),
99
                    /** @scrutinizer ignore-type */
100
                    Status::ERROR
101
                );
102
                continue;
103
            }
104 4
            $reports[] = $this->generateValidationReportForSingleSite($site->getTypo3SiteObject());
105
        }
106
107 4
        return $reports;
108
    }
109
110
    /**
111
     * Renders validation results for desired typo3 site configuration.
112
     *
113
     * @param Typo3Site $ypo3Site
114
     * @return Status
115
     */
116 4
    protected function generateValidationReportForSingleSite(Typo3Site $ypo3Site): Status
117
    {
118
        $variables = [
119 4
            'identifier' => $ypo3Site->getIdentifier(),
120
        ];
121 4
        $globalPassedStateForThisSite = true;
122
123 4
        foreach ($ypo3Site->getAllLanguages() as $siteLanguage) {
124 4
            if (!$siteLanguage->isEnabled()) {
125
                $variables['validationResults'][$siteLanguage->getTitle()] = [
126
                    'label' => 'Language: ' . $siteLanguage->getTitle(),
127
                    'message' => 'No checks: The language is disabled in site configuration.',
128
                    'CSSClassesFor' => [
129
                        'tr' => self::CSS_STATUS_NOTICE,
130
                    ],
131
                    'passed' => true,
132
                ];
133
                continue;
134
            }
135 4
            $variables['validationResults'][$siteLanguage->getTitle()] = $this->generateValidationResultsForSingleSiteLanguage($siteLanguage);
136 4
            $globalPassedStateForThisSite = $globalPassedStateForThisSite && $variables['validationResults'][$siteLanguage->getTitle()]['passed'];
137
        }
138
139 4
        $renderedReport = $this->getRenderedReport('SiteHandlingStatus.html', $variables);
140
        /* @var Status $status */
141 4
        return GeneralUtility::makeInstance(
142
            Status::class,
143
            /** @scrutinizer ignore-type */
144 4
            sprintf('Site Identifier: "%s"', $ypo3Site->getIdentifier()),
145
            /** @scrutinizer ignore-type */
146
            '',
147
            /** @scrutinizer ignore-type */
148
            $renderedReport,
149
            /** @scrutinizer ignore-type */
150 4
            $globalPassedStateForThisSite == true ? Status::OK : Status::ERROR
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
151
        );
152
    }
153
154
    /**
155
     * Generates the validation result array for using them in standalone view as a table row.
156
     *
157
     * @param SiteLanguage $siteLanguage
158
     * @return array
159
     */
160 4
    protected function generateValidationResultsForSingleSiteLanguage(SiteLanguage $siteLanguage): array
161
    {
162
        $validationResult = [
163 4
            'label' => 'Language: ' . $siteLanguage->getTitle(),
164
            'passed' => true,
165
            'CSSClassesFor' => [
166 4
                'tr' => self::CSS_STATUS_OK,
167
            ],
168
        ];
169
170 4
        if (!GeneralUtility::isValidUrl((string)$siteLanguage->getBase())) {
171 3
            $validationResult['message'] =
172 3
                sprintf(
173
                    'Entry Point[base]="%s" is not valid URL.'
174
                    . ' Following parts of defined URL are empty or invalid: "%s"',
175 3
                    $siteLanguage->getBase()->__toString(),
176 3
                    $this->fetchInvalidPartsOfUri($siteLanguage->getBase())
177
                );
178 3
            $validationResult['passed'] = false;
179 3
            $validationResult['CSSClassesFor']['tr'] = self::CSS_STATUS_ERROR;
180
        } else {
181 2
            $validationResult['message'] = sprintf(
182
                'Entry Point[base]="%s" is valid URL.',
183 2
                $siteLanguage->getBase()->__toString()
184
            );
185
        }
186
187 4
        return $validationResult;
188
    }
189
190
    /**
191
     * @param UriInterface $uri
192
     * @return string
193
     */
194 3
    protected function fetchInvalidPartsOfUri(UriInterface $uri): string
195
    {
196 3
        $invalidParts = [];
197 3
        if (empty($uri->getScheme())) {
198 3
            $invalidParts[] = 'scheme';
199
        }
200 3
        if (empty($uri->getHost())) {
201 1
            $invalidParts[] = 'host';
202
        }
203
204 3
        return implode(', ', $invalidParts);
205
    }
206
}
207