Passed
Push — release-11.0.x ( 181948...809732 )
by Rafael
28:42 queued 25:26
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 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 4
    public function __construct(ExtensionConfiguration $extensionConfiguration = null, SiteRepository $siteRepository = null)
75
    {
76 4
        $this->extensionConfiguration = $extensionConfiguration ?? GeneralUtility::makeInstance(ExtensionConfiguration::class);
77 4
        $this->siteRepository = $siteRepository ?? GeneralUtility::makeInstance(SiteRepository::class);
78 4
    }
79
80
    /**
81
     * @return array
82
     * @throws Exception
83
     */
84 4
    public function getStatus()
85
    {
86 4
        $reports = [];
87
88
        /* @var Site $site */
89 4
        foreach ($this->siteRepository->getAvailableSites() as $site) {
90 4
            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 4
            $reports[] = $this->generateValidationReportForSingleSite($site->getTypo3SiteObject());
101
        }
102
103 4
        return $reports;
104
    }
105
106
    /**
107
     * Renders validation results for desired typo3 site configuration.
108
     *
109
     * @param Typo3Site $ypo3Site
110
     * @return Status
111
     */
112 4
    protected function generateValidationReportForSingleSite(Typo3Site $ypo3Site): Status
113
    {
114
        $variables = [
115 4
            'identifier' => $ypo3Site->getIdentifier()
116
        ];
117 4
        $globalPassedStateForThisSite = true;
118
119 4
        foreach ($ypo3Site->getAllLanguages() as $siteLanguage) {
120 4
            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 4
            $variables['validationResults'][$siteLanguage->getTitle()] = $this->generateValidationResultsForSingleSiteLanguage($siteLanguage);
132 4
            $globalPassedStateForThisSite = $globalPassedStateForThisSite && $variables['validationResults'][$siteLanguage->getTitle()]['passed'];
133
        }
134
135 4
        $renderedReport = $this->getRenderedReport('SiteHandlingStatus.html', $variables);
136
        /* @var Status $status */
137 4
        $status = GeneralUtility::makeInstance(
138 4
            Status::class,
139 4
            /** @scrutinizer ignore-type */ sprintf('Site Identifier: "%s"', $ypo3Site->getIdentifier()),
140 4
            /** @scrutinizer ignore-type */ '',
141 4
            /** @scrutinizer ignore-type */ $renderedReport,
142 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...
143
        );
144 4
        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 4
    protected function generateValidationResultsForSingleSiteLanguage(SiteLanguage $siteLanguage): array
154
    {
155
        $validationResult = [
156 4
            'label' => 'Language: ' . $siteLanguage->getTitle(),
157
            'passed' => true,
158
            'CSSClassesFor' => [
159 4
                'tr' => self::CSS_STATUS_OK
160
            ]
161
        ];
162
163 4
        if (!GeneralUtility::isValidUrl((string)$siteLanguage->getBase())) {
164 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()));
165 3
            $validationResult['passed'] = false;
166 3
            $validationResult['CSSClassesFor']['tr'] = self::CSS_STATUS_ERROR;
167
        } else {
168 2
            $validationResult['message'] = sprintf('Entry Point[base]="%s" is valid URL.', (string)$siteLanguage->getBase());
169
        }
170
171 4
        return $validationResult;
172
    }
173
174
    /**
175
     * @param UriInterface $uri
176
     * @return string
177
     */
178 3
    protected function fetchInvalidPartsOfUri(UriInterface $uri): string
179
    {
180 3
        $invalidParts = '';
181
        /* @var UrlHelper $solrUriHelper */
182 3
        $solrUriHelper = GeneralUtility::makeInstance(UrlHelper::class, $uri);
183
        try {
184 3
            $scheme = $solrUriHelper->getScheme();
185 3
            if (empty($scheme)  ) {
186 3
                $invalidParts .= 'scheme';
187
            }
188
        } catch (\TypeError $error) {
189
            $invalidParts .= 'scheme';
190
        }
191
192
        try {
193 3
            $host = $solrUriHelper->getHost();
194 3
            if (empty($host)) {
195 3
                $invalidParts .= ', host';
196
            }
197
        } catch (\TypeError $error) {
198
            $invalidParts .= ', host';
199
        }
200
201 3
        return $invalidParts;
202
    }
203
}
204