Passed
Push — task/2976_TYPO3.11_compatibili... ( 4d1a77...3b9190 )
by Rafael
03:39
created

SiteHandlingStatus   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Test Coverage

Coverage 76.79%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
eloc 64
dl 0
loc 143
ccs 43
cts 56
cp 0.7679
rs 10
c 1
b 0
f 0

5 Methods

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