Services   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 18
c 1
b 0
f 1
dl 0
loc 88
ccs 20
cts 20
cp 1
rs 10
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getByNames() 0 3 1
A getByName() 0 3 2
A parseServices() 0 14 5
A count() 0 7 1
A parseNamedService() 0 3 1
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\File\Definitions;
6
7
use Countable;
8
use Ktomk\Pipelines\File\ParseException;
9
10
/**
11
 * Class Services
12
 *
13
 * @package Ktomk\Pipelines\File\Definitions
14
 */
15
class Services implements Countable
16
{
17
    /**
18
     * @var array
19
     */
20
    private $array;
21
22
    /**
23
     * @var array|Service[]
24
     */
25
    private $services = array();
26
27
    /**
28
     * Services constructor.
29
     *
30
     * @param array $array
31
     */
32 5
    public function __construct(array $array)
33
    {
34 5
        $this->parseServices($array);
35
36 3
        $this->array = $array;
37
    }
38
39
    /**
40
     * @param string $serviceName
41
     *
42
     * @return null|Service
43
     */
44 1
    public function getByName($serviceName)
45
    {
46 1
        return isset($this->services[$serviceName]) ? $this->services[$serviceName] : null;
47
    }
48
49
    /**
50
     * get array of services by names
51
     *
52
     * if a service is not found it will not be returned
53
     *
54
     * @param string[] $serviceNames names of services to obtain
55
     *
56
     * @return Service[]
57
     */
58 1
    public function getByNames(array $serviceNames)
59
    {
60 1
        return array_intersect_key($this->services, array_flip($serviceNames));
61
    }
62
63 2
    #[\ReturnTypeWillChange]
64
    /**
65
     * @return int
66
     */
67
    public function count()
68
    {
69 2
        return count($this->services);
70
    }
71
72
    /**
73
     * @param array $array
74
     *
75
     * @return void
76
     */
77 5
    private function parseServices(array $array)
78
    {
79 5
        foreach ($array as $name => $service) {
80 4
            if (!is_string($name)) {
81 1
                throw new ParseException(sprintf('Invalid service definition name: %s', var_export($name, true)));
82
            }
83 3
            if (!is_array($service)) {
84 1
                throw new ParseException(sprintf('Invalid service definition "%s"', $name));
85
            }
86
            // docker service is internal, for pipelines no need here to handle it
87 2
            if ('docker' === $name) {
88 1
                continue;
89
            }
90 1
            $this->services[$name] = $this->parseNamedService($name, $service);
91
        }
92
    }
93
94
    /**
95
     * @param string $name
96
     * @param array $service
97
     *
98
     * @return Service
99
     */
100 1
    private function parseNamedService($name, array $service)
101
    {
102 1
        return new Service($name, $service);
103
    }
104
}
105