ServiceDefinition::getClass()   A
last analyzed

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