Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
74.39% covered (warning)
74.39%
61 / 82
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ConfigurationValidator
74.39% covered (warning)
74.39%
61 / 82
50.00% covered (danger)
50.00%
1 / 2
15.84
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 validateConfiguration
73.75% covered (warning)
73.75%
59 / 80
0.00% covered (danger)
0.00%
0 / 1
14.60
1<?php
2
3namespace MediaWiki\Wikispeech;
4
5/**
6 * @file
7 * @ingroup Extensions
8 * @license GPL-2.0-or-later
9 */
10
11use Config;
12use Psr\Log\LoggerInterface;
13
14class ConfigurationValidator {
15
16    /** @var Config */
17    private $config;
18
19    /** @var LoggerInterface */
20    private $logger;
21
22    public function __construct( Config $config, LoggerInterface $logger ) {
23        $this->config = $config;
24        $this->logger = $logger;
25    }
26
27    /**
28     * Investigates whether or not configuration is valid.
29     *
30     * Writes all invalid configuration entries to the log.
31     *
32     * @since 0.1.11
33     * @return bool true if all configuration passes validation
34     */
35    public function validateConfiguration(): bool {
36        $success = true;
37
38        $speechoidUrl = $this->config->get( 'WikispeechSpeechoidUrl' );
39        if ( !filter_var( $speechoidUrl, FILTER_VALIDATE_URL ) ) {
40            $this->logger
41                ->warning( __METHOD__ . ': Configuration value for ' .
42                    '\'WikispeechSpeechoidUrl\' is not a valid URL: {value}',
43                    [ 'value' => $speechoidUrl ]
44                );
45            $success = false;
46        }
47        $speechoidResponseTimeoutSeconds = $this->config
48            ->get( 'WikispeechSpeechoidResponseTimeoutSeconds' );
49        if ( $speechoidResponseTimeoutSeconds &&
50            !is_int( $speechoidResponseTimeoutSeconds ) ) {
51            $this->logger
52                ->warning( __METHOD__ . ': Configuration value ' .
53                    '\'WikispeechSpeechoidResponseTimeoutSeconds\' ' .
54                    'is not a falsy or integer value.'
55                );
56            $success = false;
57        }
58
59        $utteranceTimeToLiveDays = $this->config
60            ->get( 'WikispeechUtteranceTimeToLiveDays' );
61        if ( $utteranceTimeToLiveDays === null ) {
62            $this->logger
63                ->warning( __METHOD__ . ': Configuration value for ' .
64                    '\'WikispeechUtteranceTimeToLiveDays\' is missing.'
65                );
66            $success = false;
67        }
68        $utteranceTimeToLiveDays = intval( $utteranceTimeToLiveDays );
69        if ( $utteranceTimeToLiveDays < 0 ) {
70            $this->logger
71                ->warning( __METHOD__ . ': Configuration value for ' .
72                    '\'WikispeechUtteranceTimeToLiveDays\' must not be negative.'
73                );
74            $success = false;
75        }
76
77        $minimumMinutesBetweenFlushExpiredUtterancesJobs = $this->config
78            ->get( 'WikispeechMinimumMinutesBetweenFlushExpiredUtterancesJobs' );
79        if ( $minimumMinutesBetweenFlushExpiredUtterancesJobs === null ) {
80            $this->logger
81                ->warning( __METHOD__ . ': Configuration value for ' .
82                    '\'WikispeechMinimumMinutesBetweenFlushExpiredUtterancesJobs\' ' .
83                    'is missing.'
84                );
85            $success = false;
86        }
87        $minimumMinutesBetweenFlushExpiredUtterancesJobs = intval(
88            $minimumMinutesBetweenFlushExpiredUtterancesJobs
89        );
90        if ( $minimumMinutesBetweenFlushExpiredUtterancesJobs < 0 ) {
91            $this->logger
92                ->warning( __METHOD__ . ': Configuration value for ' .
93                    '\'WikispeechMinimumMinutesBetweenFlushExpiredUtterancesJobs\'' .
94                    ' must not be negative.'
95                );
96            $success = false;
97        }
98
99        $fileBackendName = $this->config->get( 'WikispeechUtteranceFileBackendName' );
100        if ( $fileBackendName === null ) {
101            $this->logger
102                ->warning( __METHOD__ . ':  Configuration value ' .
103                    '\'WikispeechUtteranceFileBackendName\' is missing.'
104                );
105            // This is not a failure.
106            // It will fall back on default, but admin should be aware.
107        } elseif ( !is_string( $fileBackendName ) ) {
108            $this->logger
109                ->warning( __METHOD__ . ': Configuration value ' .
110                    '\'WikispeechUtteranceFileBackendName\' is not a string value.'
111                );
112            $success = false;
113        }
114
115        $fileBackendContainerName = $this->config
116            ->get( 'WikispeechUtteranceFileBackendContainerName' );
117        if ( $fileBackendContainerName === null ) {
118            $this->logger
119                ->warning( __METHOD__ . ': Configuration value ' .
120                    '\'WikispeechUtteranceFileBackendContainerName\' is missing.'
121                );
122            $success = false;
123        } elseif ( !is_string( $fileBackendContainerName ) ) {
124            $this->logger
125                ->warning( __METHOD__ . ': Configuration value ' .
126                    '\'WikispeechUtteranceFileBackendContainerName\' is not a string value.'
127                );
128            $success = false;
129        }
130
131        return $success;
132    }
133}