Completed
Push — master ( d3edd8...61bf89 )
by
unknown
02:52
created

MethodDefinition::setParametersCollectionOrder()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.0909

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 11
cts 13
cp 0.8462
rs 8.439
c 0
b 0
f 0
cc 5
eloc 14
nc 8
nop 1
crap 5.0909
1
<?php declare(strict_types = 1);
2
/**
3
 * Created by PhpStorm.
4
 * User: root
5
 * Date: 02.08.16
6
 * Time: 0:46.
7
 */
8
namespace samsonframework\container\definition;
9
10
use samsonframework\container\definition\exception\ParameterDefinitionAlreadyExistsException;
11
use samsonframework\container\definition\exception\ParameterDefinitionNotFoundException;
12
13
/**
14
 * Class MethodDefinition
15
 *
16
 * @package samsonframework\container\definition
17
 */
18
class MethodDefinition extends AbstractDefinition implements MethodBuilderInterface
19
{
20
    /** @var  string Method name */
21
    protected $methodName;
22
    /** @var ParameterDefinition[] Collection of parameter collection */
23
    protected $parametersCollection = [];
24
    /** @var int Method modifiers */
25
    protected $modifiers = 0;
26
    /** @var bool Flag that method is public */
27
    protected $isPublic = false;
28
29
    /**
30
     * Define arguments
31
     *
32
     * @param string $parameterName
33
     * @return ParameterBuilderInterface
34
     * @throws ParameterDefinitionAlreadyExistsException
35
     */
36 13
    public function defineParameter($parameterName): ParameterBuilderInterface
37
    {
38 13
        if (array_key_exists($parameterName, $this->parametersCollection)) {
39 1
            throw new ParameterDefinitionAlreadyExistsException();
40
        }
41
42 13
        $parameter = new ParameterDefinition($this);
43 13
        $parameter->setParameterName($parameterName);
44
45 13
        $this->parametersCollection[$parameterName] = $parameter;
46
47 13
        return $parameter;
48
    }
49
50
    /**
51
     * @return string
52
     */
53 2
    public function getMethodName(): string
54
    {
55 2
        return $this->methodName;
56
    }
57
58
    /**
59
     * @return boolean
60
     */
61 3
    public function isPublic(): bool
62
    {
63 3
        return $this->isPublic;
64
    }
65
66
    /**
67
     * @param boolean $isPublic
68
     * @return MethodDefinition
69
     */
70 4
    public function setIsPublic(bool $isPublic): MethodDefinition
71
    {
72 4
        $this->isPublic = $isPublic;
73
74 4
        return $this;
75
    }
76
77
    /**
78
     * @param string $methodName
79
     * @return MethodDefinition
80
     */
81 16
    public function setMethodName(string $methodName): MethodDefinition
82
    {
83 16
        $this->methodName = $methodName;
84
85 16
        return $this;
86
    }
87
88
    /**
89
     * @return int
90
     */
91 1
    public function getModifiers(): int
92
    {
93 1
        return $this->modifiers;
94
    }
95
96
    /**
97
     * @param int $modifiers
98
     * @return MethodDefinition
99
     */
100 4
    public function setModifiers(int $modifiers): MethodDefinition
101
    {
102 4
        $this->modifiers = $modifiers;
103
104 4
        return $this;
105
    }
106
107
    /**
108
     * @return ParameterDefinition[]
109
     */
110 4
    public function getParametersCollection(): array
111
    {
112 4
        return $this->parametersCollection;
113
    }
114
115
    /**
116
     * Set correct order of parameter definitions
117
     *
118
     * @param array $order Correct ordered parameter names
119
     * @throws \InvalidArgumentException
120
     */
121 4
    public function setParametersCollectionOrder(array $order)
122
    {
123 4
        $orderedList = [];
124
        // Sort by template
125 4
        foreach ($order as $parameterName) {
126 4
            foreach ($this->parametersCollection as $parameterDefinition) {
127 4
                if ($parameterName === $parameterDefinition->getParameterName()) {
128 4
                    $orderedList[$parameterName] = $parameterDefinition;
129
                    // Go to next parameter
130 4
                    break;
131
                }
132
            }
133
        }
134
135
        // Check if correct parameters
136 4
        $parametersCount = count($this->parametersCollection);
137 4
        if (count($orderedList) !== $parametersCount) {
138
            throw new \InvalidArgumentException(sprintf(
139
                'Count of ordered list "%s" not equal to parameter collection count "%s"',
140
                count($orderedList),
141
                $parametersCount
142
            ));
143
        }
144
145
        // Set ordered list
146 4
        $this->parametersCollection = $orderedList;
147 4
    }
148
149
    /**
150
     * Has parameter definition
151
     *
152
     * @param string $parameterName
153
     * @return bool
154
     */
155 4
    public function hasParameter(string $parameterName): bool
156
    {
157 4
        return array_key_exists($parameterName, $this->parametersCollection);
158
    }
159
160
    /**
161
     * Get property definition
162
     *
163
     * @param $parameterName
164
     * @return ParameterDefinition
165
     * @throws ParameterDefinitionNotFoundException
166
     */
167 4
    public function getParameter($parameterName): ParameterDefinition
168
    {
169 4
        if (!$this->hasParameter($parameterName)) {
170
            throw new ParameterDefinitionNotFoundException();
171
        }
172 4
        return $this->parametersCollection[$parameterName];
173
    }
174
175
    /**
176
     * Get existing or define new parameter
177
     *
178
     * @param string $parameterName
179
     * @return ParameterDefinition
180
     * @throws ParameterDefinitionNotFoundException
181
     * @throws ParameterDefinitionAlreadyExistsException
182
     */
183 4
    public function setupParameter(string $parameterName): ParameterDefinition
184
    {
185
        // Get existing parameter
186 4
        if ($this->hasParameter($parameterName)) {
187 4
            return $this->getParameter($parameterName);
188
        } else { // Or define new parameter
189 1
            return $this->defineParameter($parameterName);
190
        }
191
    }
192
}
193