Passed
Push — task/3376-TYPO3_12_compatibili... ( dc7e5a...379ca7 )
by Rafael
40:48
created

SiteHandlingStatus   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
eloc 71
c 1
b 0
f 0
dl 0
loc 176
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getLabel() 0 3 1
A getStatus() 0 24 3
A generateValidationResultsForSingleSiteLanguage() 0 28 2
A fetchInvalidPartsOfUri() 0 11 3
A generateValidationReportForSingleSite() 0 35 5
A __construct() 0 6 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\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\Type\ContextualFeedbackSeverity;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
use TYPO3\CMS\Reports\Status;
31
32
/**
33
 * Provides a status report about current state of site handling configurations.
34
 *
35
 * Following things are checked currently:
36
 * * Entry Point[base] scheme expects -> http[s]
37
 * * Entry Point[base] authority expects -> [user-info@]host[:port]
38
 */
39
class SiteHandlingStatus extends AbstractSolrStatus
40
{
41
    const TITLE_SITE_HANDLING_CONFIGURATION = 'Site handling configuration';
42
43
    /**
44
     * @var string
45
     */
46
    const
47
        CSS_STATUS_NOTICE = 'notice',
48
    CSS_STATUS_INFO = 'info',
49
    CSS_STATUS_OK = 'success',
50
    CSS_STATUS_WARNING = 'warning',
51
    CSS_STATUS_ERROR = 'danger';
52
53
    /**
54
     * Site Repository
55
     *
56
     * @var SiteRepository
57
     */
58
    protected SiteRepository $siteRepository;
59
60
    /**
61
     * @var ExtensionConfiguration
62
     */
63
    protected ExtensionConfiguration $extensionConfiguration;
64
65
    /**
66
     * SolrStatus constructor.
67
     * @param ExtensionConfiguration|null $extensionConfiguration
68
     * @param SiteRepository|null $siteRepository
69
     */
70
    public function __construct(
71
        ExtensionConfiguration $extensionConfiguration = null,
72
        SiteRepository $siteRepository = null
73
    ) {
74
        $this->extensionConfiguration = $extensionConfiguration ?? GeneralUtility::makeInstance(ExtensionConfiguration::class);
75
        $this->siteRepository = $siteRepository ?? GeneralUtility::makeInstance(SiteRepository::class);
76
    }
77
78
    /**
79
     * Verifies the site configuration.
80
     *
81
     * @return array
82
     *
83
     * @throws DBALDriverException
84
     * @throws Throwable
85
     */
86
    public function getStatus(): array
87
    {
88
        $reports = [];
89
90
        /* @var Site $site */
91
        foreach ($this->siteRepository->getAvailableSites() as $site) {
92
            if (!($site instanceof Site)) {
93
                $reports[] = GeneralUtility::makeInstance(
94
                    Status::class,
95
                    /** @scrutinizer ignore-type */
96
                    self::TITLE_SITE_HANDLING_CONFIGURATION,
97
                    /** @scrutinizer ignore-type */
98
                    'Something went wrong',
99
                    /** @scrutinizer ignore-type */
100
                    vsprintf('The configured Site "%s" is not TYPO3 managed site. Please refer to TYPO3 site management docs and configure the site properly.', [$site->getLabel()]),
101
                    /** @scrutinizer ignore-type */
102
                    ContextualFeedbackSeverity::ERROR
103
                );
104
                continue;
105
            }
106
            $reports[] = $this->generateValidationReportForSingleSite($site->getTypo3SiteObject());
107
        }
108
109
        return $reports;
110
    }
111
112
    /**
113
     * Renders validation results for desired typo3 site configuration.
114
     *
115
     * @param Typo3Site $ypo3Site
116
     * @return Status
117
     */
118
    protected function generateValidationReportForSingleSite(Typo3Site $ypo3Site): Status
119
    {
120
        $variables = [
121
            'identifier' => $ypo3Site->getIdentifier(),
122
        ];
123
        $globalPassedStateForThisSite = true;
124
125
        foreach ($ypo3Site->getAllLanguages() as $siteLanguage) {
126
            if (!$siteLanguage->isEnabled()) {
127
                $variables['validationResults'][$siteLanguage->getTitle()] = [
128
                    'label' => 'Language: ' . $siteLanguage->getTitle(),
129
                    'message' => 'No checks: The language is disabled in site configuration.',
130
                    'CSSClassesFor' => [
131
                        'tr' => self::CSS_STATUS_NOTICE,
132
                    ],
133
                    'passed' => true,
134
                ];
135
                continue;
136
            }
137
            $variables['validationResults'][$siteLanguage->getTitle()] = $this->generateValidationResultsForSingleSiteLanguage($siteLanguage);
138
            $globalPassedStateForThisSite = $globalPassedStateForThisSite && $variables['validationResults'][$siteLanguage->getTitle()]['passed'];
139
        }
140
141
        $renderedReport = $this->getRenderedReport('SiteHandlingStatus.html', $variables);
142
        /* @var Status $status */
143
        return GeneralUtility::makeInstance(
144
            Status::class,
145
            /** @scrutinizer ignore-type */
146
            sprintf('Site Identifier: "%s"', $ypo3Site->getIdentifier()),
147
            /** @scrutinizer ignore-type */
148
            '',
149
            /** @scrutinizer ignore-type */
150
            $renderedReport,
151
            /** @scrutinizer ignore-type */
152
            $globalPassedStateForThisSite == true ? ContextualFeedbackSeverity::OK : ContextualFeedbackSeverity::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...
153
        );
154
    }
155
156
    /**
157
     * {@inheritDoc}
158
     */
159
    public function getLabel(): string
160
    {
161
        return 'solr/site-handling';
162
    }
163
164
    /**
165
     * Generates the validation result array for using them in standalone view as a table row.
166
     *
167
     * @param SiteLanguage $siteLanguage
168
     * @return array
169
     */
170
    protected function generateValidationResultsForSingleSiteLanguage(SiteLanguage $siteLanguage): array
171
    {
172
        $validationResult = [
173
            'label' => 'Language: ' . $siteLanguage->getTitle(),
174
            'passed' => true,
175
            'CSSClassesFor' => [
176
                'tr' => self::CSS_STATUS_OK,
177
            ],
178
        ];
179
180
        if (!GeneralUtility::isValidUrl((string)$siteLanguage->getBase())) {
181
            $validationResult['message'] =
182
                sprintf(
183
                    'Entry Point[base]="%s" is not valid URL.'
184
                    . ' Following parts of defined URL are empty or invalid: "%s"',
185
                    $siteLanguage->getBase()->__toString(),
186
                    $this->fetchInvalidPartsOfUri($siteLanguage->getBase())
187
                );
188
            $validationResult['passed'] = false;
189
            $validationResult['CSSClassesFor']['tr'] = self::CSS_STATUS_ERROR;
190
        } else {
191
            $validationResult['message'] = sprintf(
192
                'Entry Point[base]="%s" is valid URL.',
193
                $siteLanguage->getBase()->__toString()
194
            );
195
        }
196
197
        return $validationResult;
198
    }
199
200
    /**
201
     * @param UriInterface $uri
202
     * @return string
203
     */
204
    protected function fetchInvalidPartsOfUri(UriInterface $uri): string
205
    {
206
        $invalidParts = [];
207
        if (empty($uri->getScheme())) {
208
            $invalidParts[] = 'scheme';
209
        }
210
        if (empty($uri->getHost())) {
211
            $invalidParts[] = 'host';
212
        }
213
214
        return implode(', ', $invalidParts);
215
    }
216
}
217