HostSelector::getHosts()   B
last analyzed

Complexity

Conditions 11
Paths 60

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 11.0151

Importance

Changes 0
Metric Value
cc 11
nc 60
nop 1
dl 0
loc 46
ccs 19
cts 20
cp 0.95
crap 11.0151
rs 7.3166
c 0
b 0
f 0

How to fix   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
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Host;
9
10
use Deployer\Exception\Exception;
11
12
class HostSelector
13
{
14
    /**
15
     * @var HostCollection|Host[]
16
     */
17
    private $hosts;
18
19
    /**
20
     * @var string
21
     */
22
    private $defaultStage;
23
24 31
    public function __construct(HostCollection $hosts, $defaultStage = null)
25
    {
26 31
        $this->hosts = $hosts;
27 31
        $this->defaultStage = $defaultStage;
28 31
    }
29
30
    /**
31
     * @param string $stage
32
     * @return Host[]
33
     * @throws Exception
34
     */
35 27
    public function getHosts($stage)
36
    {
37 27
        $hosts = [];
38
39
        // Get a default stage if no stage given
40 27
        if (empty($stage)) {
41 23
            $stage = $this->defaultStage;
42
        }
43
44 27
        if (!empty($stage)) {
45
            // Look for hosts which has stage with current stage name
46 4
            foreach ($this->hosts as $host) {
47
                // If host does not have any stage, skip them
48 3
                if ($stage === $host->get('stage', false)) {
49 3
                    $hosts[$host->getHostname()] = $host;
50
                }
51
            }
52
53
            // If still is empty, try to find host by name
54 4
            if (empty($hosts)) {
55 3
                if ($this->hosts->has($stage)) {
56 1
                    $hosts = [$stage => $this->hosts->get($stage)];
57
                } else {
58
                    // Nothing found.
59 4
                    throw new Exception("Hostname or stage `$stage` was not found.");
60
                }
61
            }
62
        } else {
63
            // Otherwise run on all hosts what does not specify stage
64 23
            foreach ($this->hosts as $host) {
65 22
                if (!$host->has('stage')) {
66 22
                    $hosts[$host->getHostname()] = $host;
67
                }
68
            }
69
        }
70
71 25
        if (empty($hosts)) {
72 1
            if (count($this->hosts) === 0) {
73 1
                $hosts = ['localhost' => new Localhost()];
74
            } else {
75
                throw new Exception('You need to specify at least one host or stage.');
76
            }
77
        }
78
79 25
        return $hosts;
80
    }
81
82
    /**
83
     * @param $hostnames
84
     * @return Host[]
85
     */
86 1
    public function getByHostnames($hostnames)
87
    {
88 1
        $hostnames = Range::expand(array_map('trim', explode(',', $hostnames)));
89 1
        return array_map([$this->hosts, 'get'], $hostnames);
90
    }
91
92
    /**
93
     * @param array|string $roles
94
     * @return Host[]
95
     */
96 3
    public function getByRoles($roles)
97
    {
98 3
        if (is_string($roles)) {
99 1
            $roles = array_map('trim', explode(',', $roles));
100
        }
101
102 3
        $hosts = [];
103 3
        foreach ($this->hosts as $host) {
104 3
            foreach ($host->get('roles', []) as $role) {
105 2
                if (in_array($role, $roles, true)) {
106 3
                    $hosts[$host->getHostname()] = $host;
107
                }
108
            }
109
        }
110
111 3
        return $hosts;
112
    }
113
}
114