GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 4e243a...9b019b )
by Anton
02:15
created

HostSelector   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 76.92%

Importance

Changes 0
Metric Value
dl 0
loc 69
ccs 20
cts 26
cp 0.7692
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A select() 0 10 3
A getAll() 0 8 2
A getByHostnames() 0 5 1
A getByRoles() 0 17 5
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
class HostSelector
11
{
12
    private $hosts;
13
14 5
    public function __construct(HostCollection $hosts)
15
    {
16 5
        $this->hosts = $hosts;
17 5
    }
18
19
    /**
20
     * @param string $hosts
21
     * @param string $roles
22
     * @return Host[]
23
     */
24
    public function select($hosts, $roles)
25
    {
26
        if (!empty($hosts)) {
27
            return $this->getByHostnames($hosts);
28
        }
29
        if (!empty($roles)) {
30
            return $this->getByRoles($roles);
31
        }
32
        return $this->getAll();
33
    }
34
35
    /**
36
     * @return Host[]
37
     */
38 1
    public function getAll()
39
    {
40 1
        $hosts = [];
41 1
        foreach ($this->hosts as $host) {
42 1
            $hosts[] = $host;
43
        }
44 1
        return $hosts;
45
    }
46
47
    /**
48
     * @param string $hostnames
49
     * @return Host[]
50
     */
51 1
    public function getByHostnames(string $hostnames)
52
    {
53 1
        $hostnames = Range::expand(array_map('trim', explode(',', $hostnames)));
54 1
        return array_map([$this->hosts, 'get'], $hostnames);
55
    }
56
57
    /**
58
     * @param string $roles
59
     * @return Host[]
60
     */
61 2
    public function getByRoles(string $roles)
62
    {
63 2
        if (is_string($roles)) {
64 2
            $roles = array_map('trim', explode(',', $roles));
65
        }
66
67 2
        $hosts = [];
68 2
        foreach ($this->hosts as $host) {
69 2
            foreach ($host->get('roles', []) as $role) {
70 1
                if (in_array($role, $roles, true)) {
71 1
                    $hosts[] = $host;
72
                }
73
            }
74
        }
75
76 2
        return $hosts;
77
    }
78
}
79