Passed
Push — master ( 4e6543...4e73df )
by Rafael
09:16
created

SiteHandlingStatus   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getStatus() 0 31 4
A generateValidationResultsForSingleSiteLanguage() 0 19 2
A fetchInvalidPartsOfUri() 0 18 3
A generateValidationReportForSingleSite() 0 33 5
A __construct() 0 4 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Report;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2019- dkd Internet Services GmbH ([email protected])
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\Domain\Site\Site;
28
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
29
use ApacheSolrForTypo3\Solr\Domain\Site\Typo3ManagedSite;
30
use ApacheSolrForTypo3\Solr\System\Configuration\ExtensionConfiguration;
31
use ApacheSolrForTypo3\Solr\System\Url\UrlHelper;
32
use Exception;
33
use Psr\Http\Message\UriInterface;
34
use TYPO3\CMS\Core\Site\Entity\Site as Typo3Site;
35
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
36
use TYPO3\CMS\Core\Utility\GeneralUtility;
37
use TYPO3\CMS\Reports\Status;
38
39
/**
40
 * Provides an status report about current state of site handling configurations.
41
 *
42
 * Following thigs are checked currently:
43
 * * Entry Point[base] scheme expects -> http[s]
44
 * * Entry Point[base] authority expects -> [user-info@]host[:port]
45
 */
46
class SiteHandlingStatus extends AbstractSolrStatus
47
{
48
    const TITLE_SITE_HANDLING_CONFIGURATION = 'Site handling configuration';
49
50
    const
51
        CSS_STATUS_NOTICE = 'notice',
52
        CSS_STATUS_INFO = 'info',
53
        CSS_STATUS_OK = 'success',
54
        CSS_STATUS_WARNING = 'warning',
55
        CSS_STATUS_ERROR = 'danger';
56
57
    /**
58
     * Site Repository
59
     *
60
     * @var SiteRepository
61
     */
62
    protected $siteRepository = null;
63
64
    /**
65
     * @var ExtensionConfiguration
66
     */
67
    protected $extensionConfiguration = null;
68
69
    /**
70
     * SolrStatus constructor.
71
     * @param ExtensionConfiguration $extensionConfiguration
72
     * @param SiteRepository|null $siteRepository
73
     */
74
    public function __construct(ExtensionConfiguration $extensionConfiguration = null, SiteRepository $siteRepository = null)
75
    {
76
        $this->extensionConfiguration = $extensionConfiguration ?? GeneralUtility::makeInstance(ExtensionConfiguration::class);
77
        $this->siteRepository = $siteRepository ?? GeneralUtility::makeInstance(SiteRepository::class);
78
    }
79
80
    /**
81
     * @return array
82
     * @throws Exception
83
     */
84
    public function getStatus()
85
    {
86
        $reports = [];
87
88
        if ($this->extensionConfiguration->getIsAllowLegacySiteModeEnabled()) {
89
            $reports[] = GeneralUtility::makeInstance(
90
                Status::class,
91
                /** @scrutinizer ignore-type */ self::TITLE_SITE_HANDLING_CONFIGURATION,
92
                /** @scrutinizer ignore-type */ 'The lagacy site mode is enabled. This setting is global and can not be applied per site.',
93
                /** @scrutinizer ignore-type */ 'The legacy site mode is not recommended and will be removed in EXT:Solr 11. Please switch to site handling as soon as possible.',
94
                /** @scrutinizer ignore-type */ Status::WARNING
95
            );
96
            return $reports;
97
        }
98
99
        /* @var Site $site */
100
        foreach ($this->siteRepository->getAvailableSites() as $site) {
101
            if (!($site instanceof Typo3ManagedSite)) {
102
                $reports[] = GeneralUtility::makeInstance(
103
                    Status::class,
104
                    /** @scrutinizer ignore-type */ self::TITLE_SITE_HANDLING_CONFIGURATION,
105
                    /** @scrutinizer ignore-type */ 'Something went wrong',
106
                    /** @scrutinizer ignore-type */ vsprintf('The configured Site "%s" is not TYPO3 managed site. Please refer to TYPO3 site management docs and configure the site properly.', [$site->getLabel()]),
107
                    /** @scrutinizer ignore-type */ Status::ERROR
108
                );
109
                continue;
110
            }
111
            $reports[] = $this->generateValidationReportForSingleSite($site->getTypo3SiteObject());
112
        }
113
114
        return $reports;
115
    }
116
117
    /**
118
     * Renders validation results for desired typo3 site configuration.
119
     *
120
     * @param Typo3Site $ypo3Site
121
     * @return Status
122
     */
123
    protected function generateValidationReportForSingleSite(Typo3Site $ypo3Site): Status
124
    {
125
        $variables = [
126
            'identifier' => $ypo3Site->getIdentifier()
127
        ];
128
        $globalPassedStateForThisSite = true;
129
130
        foreach ($ypo3Site->getAllLanguages() as $siteLanguage) {
131
            if (!$siteLanguage->isEnabled()) {
132
                $variables['validationResults'][$siteLanguage->getTitle()] = [
133
                    'label' => 'Language: ' . $siteLanguage->getTitle(),
134
                    'message' => 'No checks: The language is disabled in site configuration.',
135
                    'CSSClassesFor' => [
136
                        'tr' => self::CSS_STATUS_NOTICE
137
                    ],
138
                    'passed' => true
139
                ];
140
                continue;
141
            }
142
            $variables['validationResults'][$siteLanguage->getTitle()] = $this->generateValidationResultsForSingleSiteLanguage($siteLanguage);
143
            $globalPassedStateForThisSite = $globalPassedStateForThisSite && $variables['validationResults'][$siteLanguage->getTitle()]['passed'];
144
        }
145
146
        $renderedReport = $this->getRenderedReport('SiteHandlingStatus.html', $variables);
147
        /* @var Status $status */
148
        $status = GeneralUtility::makeInstance(
149
            Status::class,
150
            /** @scrutinizer ignore-type */ sprintf('Site Identifier: "%s"', $ypo3Site->getIdentifier()),
151
            /** @scrutinizer ignore-type */ '',
152
            /** @scrutinizer ignore-type */ $renderedReport,
153
            /** @scrutinizer ignore-type */ $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...
154
        );
155
        return $status;
156
    }
157
158
    /**
159
     * Generates the validation result array for using them in standalone view as an table row.
160
     *
161
     * @param SiteLanguage $siteLanguage
162
     * @return array
163
     */
164
    protected function generateValidationResultsForSingleSiteLanguage(SiteLanguage $siteLanguage): array
165
    {
166
        $validationResult = [
167
            'label' => 'Language: ' . $siteLanguage->getTitle(),
168
            'passed' => true,
169
            'CSSClassesFor' => [
170
                'tr' => self::CSS_STATUS_OK
171
            ]
172
        ];
173
174
        if (!GeneralUtility::isValidUrl((string)$siteLanguage->getBase())) {
175
            $validationResult['message'] = sprintf('Entry Point[base]="%s" is not valid URL. Following parts of defined URL are empty or invalid: "%s"', (string)$siteLanguage->getBase(), $this->fetchInvalidPartsOfUri($siteLanguage->getBase()));
176
            $validationResult['passed'] = false;
177
            $validationResult['CSSClassesFor']['tr'] = self::CSS_STATUS_ERROR;
178
        } else {
179
            $validationResult['message'] = sprintf('Entry Point[base]="%s" is valid URL.', (string)$siteLanguage->getBase());
180
        }
181
182
        return $validationResult;
183
    }
184
185
    /**
186
     * @param UriInterface $uri
187
     * @return string
188
     */
189
    protected function fetchInvalidPartsOfUri(UriInterface $uri): string
190
    {
191
        $invalidParts = '';
192
        /* @var UrlHelper $solrUriHelper */
193
        $solrUriHelper = GeneralUtility::makeInstance(UrlHelper::class, $uri);
194
        try {
195
            $solrUriHelper->getScheme();
196
        } catch (\TypeError $error) {
197
            $invalidParts .= 'scheme';
198
        }
199
200
        try {
201
            $solrUriHelper->getHost();
202
        } catch (\TypeError $error) {
203
            $invalidParts .= ', host';
204
        }
205
206
        return $invalidParts;
207
    }
208
}
209