Passed
Push — master ( 4cb925...613187 )
by Marek
10:16
created

ParametersValidator::validate()   C

Complexity

Conditions 18
Paths 38

Size

Total Lines 77
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 40
CRAP Score 18.1101

Importance

Changes 0
Metric Value
dl 0
loc 77
c 0
b 0
f 0
ccs 40
cts 43
cp 0.9302
rs 5.1779
cc 18
eloc 47
nc 38
nop 1
crap 18.1101

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types = 1);
4
5
namespace AppBuilder\Application\Configuration;
6
7
use AppBuilder\Application\Module\Jira\Exception\InvalidJiraStatusException;
8
use AppBuilder\Application\Module\Jira\ValueObject\JiraTicketStatus;
9
10
class ParametersValidator
11
{
12
    /**
13
     * Checks wether application params are valid.
14
     * Uses regex to verify hosts and a filename.
15
     * Returns false if any param is invalid.
16
     */
17 22
    public static function validate(array $paramsArray) : bool
18
    {
19
        $validJiraHost      = '/^(http:\/\/)(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*'
20 22
            . '([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])\/(jira)$/';
21
        $validBitbucketHost = '/^(ssh:\/\/git@)(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*'
22 22
            . '([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9]):([0-9]{4})*\/(pvg)\/$/';
23 22
        $validFilename      = '/^\/([a-zA-Z0-9])*\.(txt)$/';
24 22
        if (empty($paramsArray['parameters']['jira.host'])) {
25 1
            return false;
26
        }
27 21
        if (preg_match($validJiraHost, $paramsArray['parameters']['jira.host']) === 0) {
28 1
            return false;
29
        }
30
31 20
        if (preg_match($validFilename, $paramsArray['parameters']['snapshot.filename']) === 0) {
32 3
            return false;
33
        }
34
35 17
        if (preg_match($validBitbucketHost, $paramsArray['parameters']['bitbucket.repository.ssh.host']) === 0) {
36 1
            return false;
37
        }
38
39 16
        if (empty($paramsArray['parameters']['jira.authentication.username'])) {
40 1
            return false;
41
        }
42 15
        if (empty($paramsArray['parameters']['jira.authentication.password'])) {
43 1
            return false;
44
        }
45 14
        if (empty($paramsArray['parameters']['jira.watch.projects'])) {
46 1
            return false;
47
        }
48
        try {
49 13
            JiraTicketStatus::createFromString($paramsArray['parameters']['jira.trigger.deploy.state']);
50 12
            foreach ($paramsArray['parameters']['jira.trigger.remove.states'] as $status) {
51 12
                JiraTicketStatus::createFromString($status);
52
            }
53 1
        } catch (InvalidJiraStatusException $e) {
54 1
            return false;
55
        }
56 12
        if (empty($paramsArray['parameters']['bitbucket.repository.ssh.host'])) {
57
            return false;
58
        }
59
60 12
        if (empty($paramsArray['parameters']['jira.search.max.results'])) {
61 1
            return false;
62
        }
63
64 11
        if (empty($paramsArray['parameters']['server.username'])) {
65 1
            return false;
66
        }
67
68 10
        if (empty($paramsArray['parameters']['snapshot.filename'])) {
69
            return false;
70
        }
71
72 10
        if (!is_dir($paramsArray['parameters']['server.user.project.homedir'])) {
73 3
            return false;
74
        }
75 7
        if (!is_dir($paramsArray['parameters']['server.vhost.dir'])) {
76 1
            return false;
77
        }
78
79 6
        if (!is_dir($paramsArray['parameters']['server.user.homedir'])) {
80 5
            return false;
81
        }
82
83
        $vhostPublic =
84 1
            $paramsArray['parameters']['server.vhost.dir']
85 1
            . $paramsArray['parameters']['server.username']
86 1
            . $paramsArray['parameters']['server.vhost.dir.public'];
87
88 1
        if (!is_dir($vhostPublic)) {
89 1
            return false;
90
        }
91
92
        return true;
93
    }
94
}
95