Passed
Branch test (6b8015)
by Tom
02:37
created

Services::parseServices()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 4
c 1
b 0
f 1
nc 3
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 3
rs 10
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 3
    public function __construct(array $array)
33
    {
34 3
        $this->parseServices($array);
35
36 2
        $this->array = $array;
37 2
    }
38
39
    /**
40
     * @param string $service
41
     *
42
     * @return null|Service
43
     */
44 1
    public function get($service)
45
    {
46 1
        return isset($this->services[$service]) ? $this->services[$service] : null;
47
    }
48
49
    /**
50
     * @return int
51
     */
52 1
    public function count()
53
    {
54 1
        return count($this->services);
55
    }
56
57
    /**
58
     * @param array $array
59
     *
60
     * @return void
61
     */
62 3
    private function parseServices(array $array)
63
    {
64 3
        foreach ($array as $name => $service) {
65 2
            if (!is_string($name)) {
66 1
                throw new ParseException(sprintf('Invalid service definition name: %s', var_export($name, true)));
67
            }
68 1
            $this->services[$name] = $this->parseNamedService($name, $service);
69
        }
70 2
    }
71
72
    /**
73
     * @param string $name
74
     * @param array $service
75
     *
76
     * @return Service
77
     */
78 1
    private function parseNamedService($name, array $service)
79
    {
80 1
        return new Service($name, $service);
81
    }
82
}
83