ConfigurableTrait::setOptions()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2568
c 0
b 0
f 0
ccs 15
cts 15
cp 1
cc 5
nc 5
nop 2
crap 5
1
<?php
2
/**
3
 * @author Dmitry Gladyshev <[email protected]>
4
 * @date 17/08/2016 10:47
5
 */
6
7
namespace Yandex\Direct;
8
9
use Yandex\Direct\Exception\InvalidArgumentException;
10
11
/**
12
 * Class ConfigurableTrait
13
 * @package Yandex\Direct
14
 */
15
16
trait ConfigurableTrait
17
{
18
    /**
19
     * @param array $options
20
     * @param bool $ignoreMissingOptions
21
     * @throws InvalidArgumentException
22
     */
23 86
    public function setOptions(array $options, $ignoreMissingOptions = false)
24
    {
25 86
        foreach ($options as $option => $value) {
26 82
            $setter = 'set' . ucfirst($option);
27
28 82
            if (method_exists($this, $setter)) {
29 78
                $this->$setter($value);
30 78
                continue;
31
            }
32
33 6
            if (property_exists($this, $option)) {
34 4
                $this->$option = $value;
35 4
                continue;
36
            }
37
38 2
            if (!$ignoreMissingOptions) {
39 1
                throw new InvalidArgumentException(
40 1
                    "Property `{$option}` not found in class `" . __CLASS__ . "`."
41 1
                );
42
            }
43 85
        }
44 85
    }
45
}
46