Passed
Push — master ( 14a771...9e67b2 )
by Timo
05:24
created

SiteHandlingStatus   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 49
c 1
b 0
f 0
dl 0
loc 105
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A generateValidationReportForSiteHandlingConfiguration() 0 35 5
A getStatus() 0 31 4
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\SiteRepository;
28
use ApacheSolrForTypo3\Solr\Domain\Site\Typo3ManagedSite;
29
use ApacheSolrForTypo3\Solr\System\Configuration\ExtensionConfiguration;
30
use Exception;
31
use TYPO3\CMS\Core\Site\Entity\Site as Typo3Site;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
use TYPO3\CMS\Reports\Status;
34
35
/**
36
 * Provides an status report about current state of site handling configurations.
37
 *
38
 * Following thigs are checked currently:
39
 * * Entry Point[base] scheme expects -> http[s]
40
 * * Entry Point[base] authority expects -> [user-info@]host[:port]
41
 */
42
class SiteHandlingStatus extends AbstractSolrStatus
43
{
44
    const TITLE_SITE_HANDLING_CONFIGURATION = 'Site handling configuration';
45
46
    /**
47
     * Site Repository
48
     *
49
     * @var SiteRepository
50
     */
51
    protected $siteRepository = null;
52
53
    /**
54
     * @var ExtensionConfiguration
55
     */
56
    protected $extensionConfiguration = null;
57
58
    /**
59
     * SolrStatus constructor.
60
     * @param ExtensionConfiguration $extensionConfiguration
61
     * @param SiteRepository|null $siteRepository
62
     */
63
    public function __construct(ExtensionConfiguration $extensionConfiguration = null, SiteRepository $siteRepository = null)
64
    {
65
        $this->extensionConfiguration = $extensionConfiguration ?? GeneralUtility::makeInstance(ExtensionConfiguration::class);
66
        $this->siteRepository = $siteRepository ?? GeneralUtility::makeInstance(SiteRepository::class);
67
    }
68
69
    /**
70
     * @return array
71
     * @throws Exception
72
     */
73
    public function getStatus()
74
    {
75
        $reports = [];
76
77
        if ($this->extensionConfiguration->getIsAllowLegacySiteModeEnabled()) {
78
            $reports[] = GeneralUtility::makeInstance(
79
                Status::class,
80
                /** @scrutinizer ignore-type */ self::TITLE_SITE_HANDLING_CONFIGURATION,
81
                /** @scrutinizer ignore-type */ 'The lagacy site mode is enabled.',
82
                /** @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.',
83
                /** @scrutinizer ignore-type */ Status::WARNING
84
            );
85
            return $reports;
86
        }
87
88
        foreach ($this->siteRepository->getAvailableSites() as $site) {
89
            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
                return $reports;
98
            }
99
100
            $reports[] = $this->generateValidationReportForSiteHandlingConfiguration($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 generateValidationReportForSiteHandlingConfiguration(Typo3Site $ypo3Site): Status
113
    {
114
        $variables = [
115
            'identifier' => $ypo3Site->getIdentifier()
116
        ];
117
        $globalPassedStateForThisSite = true;
118
119
        // check scheme of base URI
120
        $schemeValidationStatus = !empty($ypo3Site->getBase()->getScheme()) && (strpos('http', $ypo3Site->getBase()->getScheme()) !== false);
121
        $variables['validationResults']['scheme'] = [
122
            'label' => 'Requirement: Valid scheme in Entry Point[base].',
123
            'message' => 'Entry Point[base] must contain valid HTTP scheme as http[s]:// to be able to index records.',
124
            'passed' => $schemeValidationStatus
125
        ];
126
        $globalPassedStateForThisSite = $globalPassedStateForThisSite && $schemeValidationStatus;
127
128
        // check authority of base URI
129
        $authorityValidationStatus = !empty($ypo3Site->getBase()->getAuthority());
130
        $variables['validationResults']['authority'] = [
131
            'label' => 'Requirement: Valid Authority in Entry Point[base].',
132
            'message' => 'Entry Point[base] must define the authority as [user-info@]host[:port] to be able to index records.',
133
            'passed' => $authorityValidationStatus
134
        ];
135
        $globalPassedStateForThisSite = $globalPassedStateForThisSite && $authorityValidationStatus;
136
137
        $renderedReport = $this->getRenderedReport('SiteHandlingStatus.html', $variables);
138
        /* @var Status $status */
139
        $status = GeneralUtility::makeInstance(
140
            Status::class,
141
            /** @scrutinizer ignore-type */ 'Site Identifier: ' . $ypo3Site->getIdentifier(),
142
            /** @scrutinizer ignore-type */ '',
143
            /** @scrutinizer ignore-type */ $renderedReport,
144
            /** @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...
145
        );
146
        return $status;
147
    }
148
}
149