Passed
Pull Request — master (#116)
by Théo
02:21
created

Checker   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 16
dl 0
loc 117
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A checkRequirements() 0 22 3
A retrieveRequirements() 0 11 2
C printCheck() 0 69 11
1
<?php
2
3
/*
4
 * This file is part of the box project.
5
 *
6
 * (c) Kevin Herrera <[email protected]>
7
 *     Théo Fidry <[email protected]>
8
 *
9
 * This source file is subject to the MIT license that is bundled
10
 * with this source code in the file LICENSE.
11
 */
12
13
namespace KevinGH\Box\RequirementChecker;
14
15
/**
16
 * The code in this file must be PHP 5.3+ compatible as is used to know if the application can be run.
17
 *
18
 * @private
19
 */
20
final class Checker
21
{
22
    /** @private */
23
    const REQUIREMENTS_CONFIG = '.requirements.php';
24
25
    /**
26
     * @return bool
27
     */
28
    public static function checkRequirements()
29
    {
30
        $requirements = self::retrieveRequirements();
31
32
        $checkPassed = $requirements->evaluateRequirements();
33
34
        $io = new IO();
35
36
        if (false === $checkPassed && IO::VERBOSITY_VERY_VERBOSE > $io->getVerbosity()) {
37
            // Override the default verbosity to output errors regardless of the verbosity asked by the user
38
            $io->setVerbosity(IO::VERBOSITY_VERY_VERBOSE);
39
        }
40
41
        self::printCheck(
42
            new Printer(
43
                $io->getVerbosity(),
44
                $io->hasColorSupport()
45
            ),
46
            $requirements
47
        );
48
49
        return $checkPassed;
50
    }
51
52
    public static function printCheck(Printer $printer, RequirementCollection $requirements)
53
    {
54
        $verbosity = IO::VERBOSITY_VERY_VERBOSE;
55
56
        $iniPath = $requirements->getPhpIniPath();
57
58
        $printer->title('Box Requirements Checker', $verbosity);
59
60
        $printer->printv('> Using PHP ', $verbosity);
61
        $printer->printvln(PHP_VERSION, $verbosity, 'green');
62
63
        $printer->printvln('> PHP is using the following php.ini file:', $verbosity);
64
65
        if ($iniPath) {
66
            $printer->printvln('  '.$iniPath, $verbosity, 'green');
67
        } else {
68
            $printer->printvln('  WARNING: No configuration file (php.ini) used by PHP!', $verbosity, 'yellow');
69
        }
70
71
        $printer->printvln('', $verbosity);
72
73
        if (count($requirements) > 0) {
74
            $printer->printvln('> Checking Box requirements:', $verbosity);
75
            $printer->printv('  ', $verbosity);
76
        } else {
77
            $printer->printvln('> No requirements found.', $verbosity);
78
        }
79
80
        $errorMessages = array();
81
82
        foreach ($requirements->getRequirements() as $requirement) {
83
            if ($errorMessage = $printer->getRequirementErrorMessage($requirement)) {
84
                if ($printer->getVerbosity() === IO::VERBOSITY_DEBUG) {
85
                    $printer->printvln('✘ '.$requirement->getTestMessage(), IO::VERBOSITY_DEBUG, 'red');
86
                    $printer->printv('  ', IO::VERBOSITY_DEBUG);
87
                    $errorMessages[] = $errorMessage;
88
                } else {
89
                    $printer->printv('E', $verbosity, 'red');
90
                    $errorMessages[] = $errorMessage;
91
                }
92
93
                continue;
94
            }
95
96
            if ($printer->getVerbosity() === IO::VERBOSITY_DEBUG) {
97
                $printer->printvln('✔ '.$requirement->getHelpText(), IO::VERBOSITY_DEBUG, 'green');
98
                $printer->printv('  ', IO::VERBOSITY_DEBUG);
99
            } else {
100
                $printer->printv('.', $verbosity, 'green');
101
            }
102
        }
103
104
        if ($printer->getVerbosity() !== IO::VERBOSITY_DEBUG && count($requirements) > 0) {
105
            $printer->printvln('', $verbosity);
106
        }
107
108
        if ($requirements->evaluateRequirements()) {
109
            $printer->block('OK', 'Your system is ready to run the application.', $verbosity, 'success');
110
        } else {
111
            $printer->block('ERROR', 'Your system is not ready to run the application.', $verbosity, 'error');
112
113
            $printer->title('Fix the following mandatory requirements:', $verbosity, 'red');
114
115
            foreach ($errorMessages as $errorMessage) {
116
                $printer->printv(' * '.$errorMessage, $verbosity);
117
            }
118
        }
119
120
        $printer->printvln('', $verbosity);
121
    }
122
123
    /**
124
     * @return RequirementCollection
125
     */
126
    private static function retrieveRequirements()
127
    {
128
        $config = require self::REQUIREMENTS_CONFIG;
129
130
        $requirements = new RequirementCollection();
131
132
        foreach ($config as $constraint) {
133
            call_user_func_array(array($requirements, 'addRequirement'), $constraint);
134
        }
135
136
        return $requirements;
137
    }
138
}
139