Completed
Push — master ( 1bae27...fe1a3e )
by Andreas
02:24
created

ServiceDefinition::isAlias()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace TomPHP\ContainerConfigurator;
4
5
use Assert\Assertion;
6
use InvalidArgumentException;
7
use TomPHP\ContainerConfigurator\Exception\InvalidConfigException;
8
9
final class ServiceDefinition
10
{
11
    /**
12
     * @var string
13
     */
14
    private $name;
15
16
    /**
17
     * @var string
18
     */
19
    private $class;
20
21
    /**
22
     * @var bool
23
     */
24
    private $isSingleton;
25
26
    /**
27
     * @var bool
28
     */
29
    private $isFactory;
30
31
    /**
32
     * @var bool
33
     */
34
    private $isAlias;
35
36
    /**
37
     * @var array
38
     */
39
    private $arguments;
40
41
    /**
42
     * @var array
43
     */
44
    private $methods;
45
46
    /**
47
     * @param string $name
48
     * @param array  $config
49
     * @param bool   $singletonDefault
50
     *
51
     * @throws InvalidArgumentException
52
     * @throws InvalidConfigException
53
     */
54
    public function __construct($name, array $config, $singletonDefault = false)
55
    {
56
        Assertion::string($name);
57
        Assertion::boolean($singletonDefault);
58
59
        $this->name        = $name;
60
        $this->class       = $this->className($name, $config);
61
        $this->isSingleton = isset($config['singleton']) ? $config['singleton'] : $singletonDefault;
62
        $this->isFactory   = isset($config['factory']);
63
        $this->isAlias     = isset($config['service']);
64
        $this->arguments   = isset($config['arguments']) ? $config['arguments'] : [];
65
        $this->methods     = isset($config['methods']) ? $config['methods'] : [];
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    public function getName()
72
    {
73
        return $this->name;
74
    }
75
76
    /**
77
     * @return string
78
     */
79
    public function getClass()
80
    {
81
        return $this->class;
82
    }
83
84
    /**
85
     * @return bool
86
     */
87
    public function isSingleton()
88
    {
89
        return $this->isSingleton;
90
    }
91
92
    /**
93
     * @return bool
94
     */
95
    public function isFactory()
96
    {
97
        return $this->isFactory;
98
    }
99
100
    /**
101
     * @return bool
102
     */
103
    public function isAlias()
104
    {
105
        return $this->isAlias;
106
    }
107
108
    /**
109
     * @return array
110
     */
111
    public function getArguments()
112
    {
113
        return $this->arguments;
114
    }
115
116
    /**
117
     * @return array
118
     */
119
    public function getMethods()
120
    {
121
        return $this->methods;
122
    }
123
124
    /**
125
     * @param string $name
126
     * @param array  $config
127
     *
128
     * @throws InvalidConfigException
129
     *
130
     * @return string
131
     */
132
    private function className($name, array $config)
133
    {
134
        if (isset($config['class']) && isset($config['factory'])) {
135
            throw InvalidConfigException::fromNameWhenClassAndFactorySpecified($name);
136
        }
137
138
        if (isset($config['class']) && isset($config['service'])) {
139
            throw InvalidConfigException::fromNameWhenClassAndServiceSpecified($name);
140
        }
141
142
        if (isset($config['factory']) && isset($config['service'])) {
143
            throw InvalidConfigException::fromNameWhenFactoryAndServiceSpecified($name);
144
        }
145
146
        if (isset($config['service'])) {
147
            return $config['service'];
148
        }
149
150
        if (isset($config['class'])) {
151
            return $config['class'];
152
        }
153
154
        if (isset($config['factory'])) {
155
            return $config['factory'];
156
        }
157
158
        return $name;
159
    }
160
}
161