Passed
Pull Request — release-11.2.x (#3528)
by Rafael
09:36
created

generateValidationResultsForSingleSiteLanguage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.4168

Importance

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