Completed
Push — master ( a3be27...5b21b3 )
by Andy
01:12
created

Definition::getClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Palmtree\Container\Definition;
4
5
use Palmtree\Container\Exception\InvalidDefinitionException;
6
7
class Definition
8
{
9
    /** @var string */
10
    private $class;
11
    /** @var bool */
12
    private $lazy = false;
13
    /** @var bool */
14
    private $public = true;
15
    /** @var array */
16
    private $arguments = [];
17
    /** @var MethodCall[] */
18
    private $methodCalls = [];
19
    /** @var array */
20
    private $factory;
21
22
    /**
23
     * @param array $yaml
24
     *
25
     * @return Definition
26
     *
27
     * @throws InvalidDefinitionException
28
     */
29 23
    public static function fromYaml(array $yaml)
30
    {
31 23
        if (!isset($yaml['class']) && !isset($yaml['factory'])) {
32 1
            throw new InvalidDefinitionException("Missing required 'class' argument. Must be a FQCN.");
33
        }
34
35 22
        $definition = new self();
36
37 22
        if (isset($yaml['class'])) {
38 22
            $definition->setClass($yaml['class']);
39
        }
40
41 22
        if (isset($yaml['factory'])) {
42 21
            $definition->setFactory($yaml['factory']);
43
        }
44
45 22
        $definition->setLazy(isset($yaml['lazy']) ? $yaml['lazy'] : false);
46 22
        $definition->setPublic(isset($yaml['public']) ? $yaml['public'] : true);
47
48 22
        if (isset($yaml['arguments'])) {
49 21
            $definition->setArguments($yaml['arguments']);
50
        }
51
52 22
        if (isset($yaml['calls'])) {
53 21
            foreach ($yaml['calls'] as $call) {
54 21
                $methodCall = MethodCall::fromYaml($call);
55 21
                $definition->addMethodCall($methodCall);
56
            }
57
        }
58
59 22
        return $definition;
60
    }
61
62
    /**
63
     * @return string
64
     */
65 22
    public function getClass()
66
    {
67 22
        return $this->class;
68
    }
69
70
    /**
71
     * @param string $class
72
     *
73
     * @return Definition
74
     */
75 22
    public function setClass($class)
76
    {
77 22
        $this->class = $class;
78
79 22
        return $this;
80
    }
81
82
    /**
83
     * @return bool
84
     */
85 22
    public function isLazy()
86
    {
87 22
        return $this->lazy;
88
    }
89
90
    /**
91
     * @param bool $lazy
92
     *
93
     * @return Definition
94
     */
95 22
    public function setLazy($lazy)
96
    {
97 22
        $this->lazy = (bool)$lazy;
98
99 22
        return $this;
100
    }
101
102
    /**
103
     * @return bool
104
     */
105 22
    public function isPublic()
106
    {
107 22
        return $this->public;
108
    }
109
110
    /**
111
     * @param bool $public
112
     */
113 22
    public function setPublic($public)
114
    {
115 22
        $this->public = $public;
116 22
    }
117
118
    /**
119
     * @return array
120
     */
121 22
    public function getArguments()
122
    {
123 22
        return $this->arguments;
124
    }
125
126
    /**
127
     * @param array $arguments
128
     *
129
     * @return Definition
130
     */
131 21
    public function setArguments(array $arguments)
132
    {
133 21
        $this->arguments = $arguments;
134
135 21
        return $this;
136
    }
137
138
    /**
139
     * @param MethodCall $methodCall
140
     *
141
     * @return Definition
142
     */
143 21
    public function addMethodCall(MethodCall $methodCall)
144
    {
145 21
        $this->methodCalls[] = $methodCall;
146
147 21
        return $this;
148
    }
149
150
    /**
151
     * @return MethodCall[]
152
     */
153 22
    public function getMethodCalls()
154
    {
155 22
        return $this->methodCalls;
156
    }
157
158
    /**
159
     * @param MethodCall[] $methodCalls
160
     *
161
     * @return Definition
162
     */
163
    public function setMethodCalls(array $methodCalls)
164
    {
165
        foreach ($methodCalls as $methodCall) {
166
            $this->addMethodCall($methodCall);
167
        }
168
169
        return $this;
170
    }
171
172
    /**
173
     * @return array
174
     */
175 22
    public function getFactory()
176
    {
177 22
        return $this->factory;
178
    }
179
180
    /**
181
     * @param array|string $factory
182
     *
183
     * @return Definition
184
     */
185 21
    public function setFactory($factory)
186
    {
187 21
        if (is_string($factory)) {
188 21
            $factory = explode(':', $factory, 2);
189
        }
190
191 21
        $this->factory = $factory;
192
193 21
        return $this;
194
    }
195
}
196