SelfTestController   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 15

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 15
dl 0
loc 163
rs 9.1666
c 4
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllTestsAction() 0 16 2
B getAutoConfigAction() 0 26 5
A testClassToSlug() 0 12 2
A prepareTests() 0 17 1
1
<?php
2
3
/**
4
 * This file is part of tenside/core-bundle.
5
 *
6
 * (c) Christian Schiffler <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * This project is provided in good faith and hope to be usable by anyone.
12
 *
13
 * @package    tenside/core-bundle
14
 * @author     Christian Schiffler <[email protected]>
15
 * @copyright  2015 Christian Schiffler <[email protected]>
16
 * @license    https://github.com/tenside/core-bundle/blob/master/LICENSE MIT
17
 * @link       https://github.com/tenside/core-bundle
18
 * @filesource
19
 */
20
21
namespace Tenside\CoreBundle\Controller;
22
23
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
24
use Symfony\Component\HttpFoundation\JsonResponse;
25
use Tenside\Core\SelfTest\Cli\SelfTestCliCanFork;
26
use Tenside\Core\SelfTest\Cli\SelfTestCliOsChecks;
27
use Tenside\Core\SelfTest\Php\SelfTestEnvPopulated;
28
use Tenside\CoreBundle\Annotation\ApiDescription;
29
use Tenside\Core\SelfTest\Cli\SelfTestCanSpawnProcesses;
30
use Tenside\Core\SelfTest\Cli\SelfTestCliArguments;
31
use Tenside\Core\SelfTest\Cli\SelfTestCliRuntime;
32
use Tenside\Core\SelfTest\Generic\SelfTestCalledViaHttps;
33
use Tenside\Core\SelfTest\Generic\SelfTestFileOwnerMatches;
34
use Tenside\Core\SelfTest\Php\SelfTestAllowUrlFopenEnabled;
35
use Tenside\Core\SelfTest\Php\SelfTestSuhosin;
36
use Tenside\Core\SelfTest\SelfTest;
37
38
/**
39
 * This class provides the self test entry points.
40
 */
41
class SelfTestController extends AbstractController
42
{
43
    /**
44
     * Retrieve the results of all tests.
45
     *
46
     * @return JsonResponse
47
     *
48
     * @ApiDoc(
49
     *   section="selftest",
50
     *   statusCodes = {
51
     *     200 = "When everything worked out ok"
52
     *   },
53
     *   authentication = true,
54
     *   authenticationRoles = {
55
     *     "ROLE_MANIPULATE_REQUIREMENTS"
56
     *   }
57
     * )
58
     * @ApiDescription(
59
     *   response={
60
     *     "results" = {
61
     *       "actualType" = "collection",
62
     *       "subType" = "object",
63
     *       "description" = "The test results.",
64
     *       "children" = {
65
     *         "name" = {
66
     *           "dataType" = "string",
67
     *           "description" = "The name of the test"
68
     *         },
69
     *         "state" = {
70
     *           "dataType" = "choice",
71
     *           "description" = "The test result state.",
72
     *           "format" = "[FAIL|SKIPPED|SUCCESS|WARNING]"
73
     *         },
74
     *         "message" = {
75
     *           "dataType" = "string",
76
     *           "description" = "The detailed message of the test result."
77
     *         },
78
     *         "explain" = {
79
     *           "dataType" = "string",
80
     *           "description" = "Optional description that could hint any problems and/or explain the error further."
81
     *         }
82
     *       }
83
     *     }
84
     *   }
85
     * )
86
     */
87
    public function getAllTestsAction()
88
    {
89
        $tester = $this->prepareTests();
90
91
        $data = ['results' => []];
92
        foreach ($tester->perform() as $result) {
93
            $data['results'][] = [
94
                'name'    => $this->testClassToSlug($result->getTestClass()),
95
                'state'   => $result->getState(),
96
                'message' => $result->getMessage(),
97
                'explain' => $result->getExplain(),
98
            ];
99
        }
100
101
        return JsonResponse::create($data);
102
    }
103
104
    /**
105
     * Retrieve the automatic generated tenside configuration.
106
     *
107
     * The automatic configuration consists of several values.
108
     *
109
     * @return JsonResponse
110
     *
111
     * @ApiDoc(
112
     *   section="selftest",
113
     *   statusCodes = {
114
     *     200 = "When everything worked out ok"
115
     *   },
116
     *   authentication = true,
117
     *   authenticationRoles = {
118
     *     "ROLE_MANIPULATE_REQUIREMENTS"
119
     *   }
120
     * )
121
     * @ApiDescription(
122
     *   response={
123
     *     "php_cli" = {
124
     *       "dataType" = "string",
125
     *       "description" = "The PHP interpreter to run on command line."
126
     *     },
127
     *     "php_cli_arguments" = {
128
     *       "dataType" = "string",
129
     *       "description" = "Command line arguments to add."
130
     *     }
131
     *   }
132
     * )
133
     */
134
    public function getAutoConfigAction()
135
    {
136
        $tester = $this->prepareTests();
137
        $tester->perform();
138
139
        $config = $tester->getAutoConfig();
140
        $result = [];
141
142
        if ($phpCli = $config->getPhpCliBinary()) {
143
            $result['php_cli'] = $phpCli;
144
        }
145
        if ($phpArguments = $config->getPhpCliArguments()) {
146
            $result['php_cli_arguments'] = $phpArguments;
147
        }
148
        if ($phpEnvironment = $config->getPhpCliEnvironment()) {
149
            $result['php_cli_environment'] = $phpEnvironment;
150
        }
151
152
        if ($phpEnvironment = $config->isForceToBackgroundEnabled()) {
0 ignored issues
show
Unused Code introduced by
$phpEnvironment is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
153
            $result['php_force_background'] = true;
154
        }
155
156
        $result['php_can_fork'] = $config->isForkingAvailable();
157
158
        return JsonResponse::create($result);
159
    }
160
161
    /**
162
     * Create a slug from a test class.
163
     *
164
     * @param string $className The class name to convert.
165
     *
166
     * @return string
167
     */
168
    private function testClassToSlug($className)
169
    {
170
        $className = basename(str_replace('\\', '/', $className));
171
172
        if ('SelfTest' === substr($className, 0, 8)) {
173
            $className = substr($className, 8);
174
        }
175
176
        $className = strtolower(substr(preg_replace('#([A-Z])#', '-$1', $className), 1));
177
178
        return $className;
179
    }
180
181
    /**
182
     * Prepare the tests.
183
     *
184
     * @return SelfTest
185
     */
186
    private function prepareTests()
187
    {
188
        $tester = new SelfTest();
189
190
        $tester->addTest(new SelfTestCalledViaHttps());
191
        $tester->addTest(new SelfTestFileOwnerMatches());
192
        $tester->addTest(new SelfTestAllowUrlFopenEnabled());
193
        $tester->addTest(new SelfTestSuhosin());
194
        $tester->addTest(new SelfTestEnvPopulated());
195
        $tester->addTest(new SelfTestCanSpawnProcesses());
196
        $tester->addTest(new SelfTestCliRuntime());
197
        $tester->addTest(new SelfTestCliArguments());
198
        $tester->addTest(new SelfTestCliCanFork());
199
        $tester->addTest(new SelfTestCliOsChecks());
200
201
        return $tester;
202
    }
203
}
204