AbstractBuilder::configureParameters()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
1
<?php
2
/*
3
 * This file is part of the Abstract builder package, an RunOpenCode project.
4
 *
5
 * (c) 2017 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\AbstractBuilder;
11
12
use RunOpenCode\AbstractBuilder\Exception\BadMethodCallException;
13
use RunOpenCode\AbstractBuilder\Exception\InvalidArgumentException;
14
use RunOpenCode\AbstractBuilder\Exception\RuntimeException;
15
16
/**
17
 * Class AbstractBuilder
18
 *
19
 * Prototype implementation of class builder pattern.
20
 *
21
 * @package RunOpenCode\AbstractBuilder
22
 */
23
abstract class AbstractBuilder implements \ArrayAccess
24
{
25
    /**
26
     * A placeholder for constructor arguments.
27
     *
28
     * @var array
29
     */
30
    protected $_builder_placeholder_data_87cd3fb3_4fde_49d1_a91f_6411e0862c32;
31
32
    /**
33
     * AbstractBuilder constructor.
34
     *
35
     * @throws \RunOpenCode\AbstractBuilder\Exception\RuntimeException
36
     */
37 15
    public function __construct()
38
    {
39 15
        $this->_builder_placeholder_data_87cd3fb3_4fde_49d1_a91f_6411e0862c32 = $this->configureParameters();
40
41 15
        if (0 === count($this->_builder_placeholder_data_87cd3fb3_4fde_49d1_a91f_6411e0862c32)) {
42 1
            throw new RuntimeException('Builder expects at least one parameter to be defined.');
43
        }
44 14
    }
45
46
    /**
47
     * Builds new building class instance from provided arguments.
48
     *
49
     * @return object
50
     */
51 5
    public function build()
52
    {
53 5
        $reflector = new \ReflectionClass($this->getObjectFqcn());
54 5
        return $reflector->newInstanceArgs(array_values($this->_builder_placeholder_data_87cd3fb3_4fde_49d1_a91f_6411e0862c32));
55
    }
56
57
    /**
58
     * Set building class constructor arguments from array.
59
     *
60
     * @param array $values Values for constructor arguments of building class.
61
     * @return AbstractBuilder $this Fluent interface
62
     */
63 3
    public function fromArray(array $values)
64
    {
65 3
        foreach ($values as $key => $value) {
66 3
            $this->{$key} = $value;
67
        }
68
69 3
        return $this;
70
    }
71
72
    /**
73
     * Get all building class constructor arguments as array.
74
     *
75
     * @return array
76
     */
77 1
    public function toArray(array $keys = [])
78
    {
79 1
        $data = [];
80
81 1
        $keys = (0 === count($keys)) ? array_keys($this->_builder_placeholder_data_87cd3fb3_4fde_49d1_a91f_6411e0862c32) : $keys;
82
83 1
        foreach ($keys as $key) {
84 1
            $data[$key] = $this->{$key};
85
        }
86
87 1
        return $data;
88
    }
89
90
    /**
91
     * Set building class constructor argument.
92
     *
93
     * @param string $name Argument name.
94
     * @param mixed $value Argument value.
95
     *
96
     * @throws \RunOpenCode\AbstractBuilder\Exception\InvalidArgumentException
97
     */
98 6 View Code Duplication
    public function __set($name, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
    {
100 6
        if (!array_key_exists($name, $this->_builder_placeholder_data_87cd3fb3_4fde_49d1_a91f_6411e0862c32)) {
101 1
            throw new InvalidArgumentException(sprintf('Unknown property "%s" in "%s".', $name, get_class($this)));
102
        }
103
104 5
        if (method_exists($this, ($setter =  sprintf('set%s', ucfirst($name))))) {
105 1
            $this->{$setter}($value);
106 1
            return;
107
        }
108
109 4
        $this->__doSet($name, $value);
110 4
    }
111
112
    /**
113
     * Get building class constructor argument.
114
     *
115
     * @param string $name Argument name.
116
     * @return mixed Argument value.
117
     *
118
     * @throws \RunOpenCode\AbstractBuilder\Exception\InvalidArgumentException
119
     */
120 3 View Code Duplication
    public function __get($name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
    {
122 3
        if (!array_key_exists($name, $this->_builder_placeholder_data_87cd3fb3_4fde_49d1_a91f_6411e0862c32)) {
123
            throw new InvalidArgumentException(sprintf('Unknown property "%s" in "%s".', $name, get_class($this)));
124
        }
125
126 3
        if (method_exists($this, ($getter =  sprintf('get%s', ucfirst($name))))) {
127 3
            return $this->{$getter}();
128
        }
129
130 2
        return $this->__doGet($name);
131
    }
132
133
    /**
134
     * Check if building class constructor argument is defined.
135
     *
136
     * @param string $name Argument name.
137
     * @return bool TRUE if argument is defined.
138
     */
139 1
    public function __isset($name)
140
    {
141 1
        return array_key_exists($name, $this->_builder_placeholder_data_87cd3fb3_4fde_49d1_a91f_6411e0862c32);
142
    }
143
144
    /**
145
     * Get/set building class constructor argument.
146
     *
147
     * @param string $name A method name.
148
     * @param array $arguments A method arguments.
149
     *
150
     * @return $this|mixed Fluent interface or argument value, depending on method name.
151
     *
152
     * @throws \RunOpenCode\AbstractBuilder\Exception\BadMethodCallException
153
     */
154 5
    public function __call($name, array $arguments)
155
    {
156 5
        $property = lcfirst(substr($name, 3));
157
158
        if (
159 5
            !array_key_exists($property, $this->_builder_placeholder_data_87cd3fb3_4fde_49d1_a91f_6411e0862c32)
160
            ||
161 5
            (strpos($name, 'set') !== 0 && strpos($name, 'get') !== 0)
162
        ) {
163 1
            throw new BadMethodCallException(sprintf('Unknown method "%s" in "%s".', $name, get_class($this)));
164
        }
165
166 4 View Code Duplication
        if (count($arguments) !== 1 && strpos($name, 'set') === 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
167
            throw new BadMethodCallException(sprintf('Method "%s" in "%s" expects exactly one parameter.', $name, get_class($this)));
168
        }
169
170 4 View Code Duplication
        if (count($arguments) !== 0 && strpos($name, 'get') === 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
171
            throw new BadMethodCallException(sprintf('Method "%s" in "%s" does not use any parameter.', $name, get_class($this)));
172
        }
173
174 4
        if (strpos($name, 'get') === 0) {
175 1
            return $this->_builder_placeholder_data_87cd3fb3_4fde_49d1_a91f_6411e0862c32[$property];
176
        }
177
178 4
        $this->_builder_placeholder_data_87cd3fb3_4fde_49d1_a91f_6411e0862c32[$property] = $arguments[0];
179
180 4
        return $this;
181
    }
182
183
    /**
184
     * Function call to builder object instance will produce building class.
185
     *
186
     * @return object
187
     */
188 1
    public function __invoke()
189
    {
190 1
        return $this->build();
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     */
196 3
    public function offsetExists($offset)
197
    {
198 3
        return array_key_exists($offset, $this->_builder_placeholder_data_87cd3fb3_4fde_49d1_a91f_6411e0862c32);
199
    }
200
201
    /**
202
     * {@inheritdoc}
203
     */
204 2
    public function offsetGet($offset)
205
    {
206 2
        if (method_exists($this, ($getter = sprintf('get%s', ucfirst($offset))))) {
207 2
            return $this->{$getter}();
208
        }
209
210 1
        return $this->_builder_placeholder_data_87cd3fb3_4fde_49d1_a91f_6411e0862c32[$offset];
211
    }
212
213
    /**
214
     * {@inheritdoc}
215
     *
216
     * @throws \RunOpenCode\AbstractBuilder\Exception\BadMethodCallException
217
     */
218 3
    public function offsetSet($offset, $value)
219
    {
220 3
        if (null === $offset) {
221
            throw new InvalidArgumentException('Property name for array access of builder parameters must be provided, NULL given.');
222
        }
223
224 3
        if (!$this->offsetExists($offset)) {
225 1
            throw new RuntimeException(sprintf('Undefined property "%s" provided.', $offset));
226
        }
227
228 2
        if (method_exists($this, ($setter =  sprintf('set%s', ucfirst($offset))))) {
229 1
            $this->{$setter}($value);
230 1
            return;
231
        }
232
233 1
        $this->_builder_placeholder_data_87cd3fb3_4fde_49d1_a91f_6411e0862c32[$offset] = $value;
234 1
    }
235
236
    /**
237
     * Unused, throws an exception.
238
     *
239
     * @param mixed $offset
240
     *
241
     * @throws \RunOpenCode\AbstractBuilder\Exception\BadMethodCallException
242
     */
243 1
    public function offsetUnset($offset)
244
    {
245 1
        throw new BadMethodCallException('It is not allowed to unset builder property.');
246
    }
247
248
    /**
249
     * Produces new builder.
250
     *
251
     * @return static
252
     *
253
     * @throws \RunOpenCode\AbstractBuilder\Exception\RuntimeException
254
     */
255 14
    public static function createBuilder()
256
    {
257 14
        return new static();
258
    }
259
260
    /**
261
     * Force get value from value storage, without consulting getter.
262
     *
263
     * Use this method to get raw value of parameter storage when creating getter method.
264
     *
265
     * @param string $name
266
     *
267
     * @return mixed
268
     */
269 3 View Code Duplication
    protected function __doGet($name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
270
    {
271 3
        if (!array_key_exists($name, $this->_builder_placeholder_data_87cd3fb3_4fde_49d1_a91f_6411e0862c32)) {
272
            throw new InvalidArgumentException(sprintf('Unknown property "%s" in "%s".', $name, get_class($this)));
273
        }
274
275 3
        return $this->_builder_placeholder_data_87cd3fb3_4fde_49d1_a91f_6411e0862c32[$name];
276
    }
277
278
    /**
279
     * Force set value to value storage, without consulting setter.
280
     *
281
     * Use this method to set raw value of parameter storage when creating setter method.
282
     *
283
     * @param string $name
284
     * @param mixed $value
285
     *
286
     * @return $this Fluent interface.
287
     * @throws \RunOpenCode\AbstractBuilder\Exception\InvalidArgumentException
288
     */
289 5 View Code Duplication
    protected function __doSet($name, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
290
    {
291 5
        if (!array_key_exists($name, $this->_builder_placeholder_data_87cd3fb3_4fde_49d1_a91f_6411e0862c32)) {
292
            throw new InvalidArgumentException(sprintf('Unknown property "%s" in "%s".', $name, get_class($this)));
293
        }
294
295 5
        $this->_builder_placeholder_data_87cd3fb3_4fde_49d1_a91f_6411e0862c32[$name] = $value;
296 5
        return $this;
297
    }
298
299
    /**
300
     * Configure builder parameters that will be passed to building class constructor.
301
     *
302
     * @return array
303
     */
304
    abstract protected function configureParameters();
305
306
    /**
307
     * Get full qualified class name of class which instance ought to be constructed.
308
     *
309
     * @return string
310
     */
311
    abstract protected function getObjectFqcn();
312
}
313