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 | /* @var Site $site */ |
||
89 | foreach ($this->siteRepository->getAvailableSites() as $site) { |
||
90 | if (!($site instanceof Typo3ManagedSite)) { |
||
91 | $reports[] = GeneralUtility::makeInstance( |
||
92 | Status::class, |
||
93 | /** @scrutinizer ignore-type */ self::TITLE_SITE_HANDLING_CONFIGURATION, |
||
94 | /** @scrutinizer ignore-type */ 'Something went wrong', |
||
95 | /** @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()]), |
||
96 | /** @scrutinizer ignore-type */ Status::ERROR |
||
97 | ); |
||
98 | continue; |
||
99 | } |
||
100 | $reports[] = $this->generateValidationReportForSingleSite($site->getTypo3SiteObject()); |
||
101 | } |
||
102 | |||
103 | return $reports; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Renders validation results for desired typo3 site configuration. |
||
108 | * |
||
109 | * @param Typo3Site $ypo3Site |
||
110 | * @return Status |
||
111 | */ |
||
112 | protected function generateValidationReportForSingleSite(Typo3Site $ypo3Site): Status |
||
113 | { |
||
114 | $variables = [ |
||
115 | 'identifier' => $ypo3Site->getIdentifier() |
||
116 | ]; |
||
117 | $globalPassedStateForThisSite = true; |
||
118 | |||
119 | foreach ($ypo3Site->getAllLanguages() as $siteLanguage) { |
||
120 | if (!$siteLanguage->isEnabled()) { |
||
121 | $variables['validationResults'][$siteLanguage->getTitle()] = [ |
||
122 | 'label' => 'Language: ' . $siteLanguage->getTitle(), |
||
123 | 'message' => 'No checks: The language is disabled in site configuration.', |
||
124 | 'CSSClassesFor' => [ |
||
125 | 'tr' => self::CSS_STATUS_NOTICE |
||
126 | ], |
||
127 | 'passed' => true |
||
128 | ]; |
||
129 | continue; |
||
130 | } |
||
131 | $variables['validationResults'][$siteLanguage->getTitle()] = $this->generateValidationResultsForSingleSiteLanguage($siteLanguage); |
||
132 | $globalPassedStateForThisSite = $globalPassedStateForThisSite && $variables['validationResults'][$siteLanguage->getTitle()]['passed']; |
||
133 | } |
||
134 | |||
135 | $renderedReport = $this->getRenderedReport('SiteHandlingStatus.html', $variables); |
||
136 | /* @var Status $status */ |
||
137 | $status = GeneralUtility::makeInstance( |
||
138 | Status::class, |
||
139 | /** @scrutinizer ignore-type */ sprintf('Site Identifier: "%s"', $ypo3Site->getIdentifier()), |
||
140 | /** @scrutinizer ignore-type */ '', |
||
141 | /** @scrutinizer ignore-type */ $renderedReport, |
||
142 | /** @scrutinizer ignore-type */ $globalPassedStateForThisSite == true ? Status::OK : Status::ERROR |
||
0 ignored issues
–
show
|
|||
143 | ); |
||
144 | return $status; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Generates the validation result array for using them in standalone view as an table row. |
||
149 | * |
||
150 | * @param SiteLanguage $siteLanguage |
||
151 | * @return array |
||
152 | */ |
||
153 | protected function generateValidationResultsForSingleSiteLanguage(SiteLanguage $siteLanguage): array |
||
154 | { |
||
155 | $validationResult = [ |
||
156 | 'label' => 'Language: ' . $siteLanguage->getTitle(), |
||
157 | 'passed' => true, |
||
158 | 'CSSClassesFor' => [ |
||
159 | 'tr' => self::CSS_STATUS_OK |
||
160 | ] |
||
161 | ]; |
||
162 | |||
163 | if (!GeneralUtility::isValidUrl((string)$siteLanguage->getBase())) { |
||
164 | $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())); |
||
165 | $validationResult['passed'] = false; |
||
166 | $validationResult['CSSClassesFor']['tr'] = self::CSS_STATUS_ERROR; |
||
167 | } else { |
||
168 | $validationResult['message'] = sprintf('Entry Point[base]="%s" is valid URL.', (string)$siteLanguage->getBase()); |
||
169 | } |
||
170 | |||
171 | return $validationResult; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * @param UriInterface $uri |
||
176 | * @return string |
||
177 | */ |
||
178 | protected function fetchInvalidPartsOfUri(UriInterface $uri): string |
||
179 | { |
||
180 | $invalidParts = ''; |
||
181 | /* @var UrlHelper $solrUriHelper */ |
||
182 | $solrUriHelper = GeneralUtility::makeInstance(UrlHelper::class, $uri); |
||
183 | try { |
||
184 | $solrUriHelper->getScheme(); |
||
185 | } catch (\TypeError $error) { |
||
186 | $invalidParts .= 'scheme'; |
||
187 | } |
||
188 | |||
189 | try { |
||
190 | $solrUriHelper->getHost(); |
||
191 | } catch (\TypeError $error) { |
||
192 | $invalidParts .= ', host'; |
||
193 | } |
||
194 | |||
195 | return $invalidParts; |
||
196 | } |
||
197 | } |
||
198 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.