Completed
Push — master ( c780b9...d0dcc2 )
by Andy
01:32
created

Definition::fromYaml()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 5
nop 1
dl 0
loc 24
ccs 13
cts 13
cp 1
crap 6
rs 8.5125
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
    protected $class;
11
    /** @var bool */
12
    protected $lazy = false;
13
    /** @var array */
14
    protected $arguments = [];
15
    /** @var MethodCall[] */
16
    protected $methodCalls = [];
17
18
    /**
19
     * @param array $yaml
20
     *
21
     * @return Definition
22
     *
23
     * @throws InvalidDefinitionException
24
     */
25 12
    public static function fromYaml(array $yaml)
26
    {
27 12
        if (!isset($yaml['class'])) {
28 1
            throw new InvalidDefinitionException("Missing required 'class' argument. Must be a FQCN.");
29
        }
30
31 11
        $definition = new self();
32
33
        $definition
34 11
            ->setClass($yaml['class'])
35 11
            ->setLazy(isset($yaml['lazy']) ? $yaml['lazy'] : false);
36
37 11
        if (isset($yaml['arguments'])) {
38 11
            $definition->setArguments($yaml['arguments']);
39
        }
40
41 11
        if (isset($yaml['calls'])) {
42 11
            foreach ($yaml['calls'] as $call) {
43 11
                $methodCall = MethodCall::fromYaml($call);
44 11
                $definition->addMethodCall($methodCall);
45
            }
46
        }
47
48 11
        return $definition;
49
    }
50
51
    /**
52
     * @return string
53
     */
54 11
    public function getClass()
55
    {
56 11
        return $this->class;
57
    }
58
59
    /**
60
     * @param string $class
61
     * @return Definition
62
     */
63 11
    public function setClass($class)
64
    {
65 11
        $this->class = $class;
66 11
        return $this;
67
    }
68
69
    /**
70
     * @return bool
71
     */
72 11
    public function isLazy()
73
    {
74 11
        return $this->lazy;
75
    }
76
77
    /**
78
     * @param bool $lazy
79
     * @return Definition
80
     */
81 11
    public function setLazy($lazy)
82
    {
83 11
        $this->lazy = (bool)$lazy;
84 11
        return $this;
85
    }
86
87
    /**
88
     * @return array
89
     */
90 11
    public function getArguments()
91
    {
92 11
        return $this->arguments;
93
    }
94
95
    /**
96
     * @param array $arguments
97
     *
98
     * @return Definition
99
     */
100 11
    public function setArguments(array $arguments)
101
    {
102 11
        $this->arguments = $arguments;
103 11
        return $this;
104
    }
105
106
    /**
107
     * @param MethodCall $methodCall
108
     *
109
     * @return Definition
110
     */
111 11
    public function addMethodCall(MethodCall $methodCall)
112
    {
113 11
        $this->methodCalls[] = $methodCall;
114
115 11
        return $this;
116
    }
117
118
    /**
119
     * @return MethodCall[]
120
     */
121 11
    public function getMethodCalls()
122
    {
123 11
        return $this->methodCalls;
124
    }
125
126
    /**
127
     * @param MethodCall[] $methodCalls
128
     * @return Definition
129
     */
130
    public function setMethodCalls(array $methodCalls)
131
    {
132
        foreach ($methodCalls as $methodCall) {
133
            $this->addMethodCall($methodCall);
134
        }
135
136
        return $this;
137
    }
138
}
139