Passed
Pull Request — master (#24)
by Mathieu
08:50 queued 05:45
created

Service::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Suricate;
6
7
use InvalidArgumentException;
8
9
class Service implements Interfaces\IService
10
{
11
    protected $parametersList = [];
12
    protected $parametersValues = [];
13
14 78
    public function __construct()
15
    {
16
        // Service constructor
17 78
    }
18
19 60
    /**
20
     * Service getter
21 60
     *
22 59
     * @param string $variable
23 59
     * @return void
24
     * @throws InvalidArgumentException
25
     */
26 5
    public function __get($variable)
27
    {
28
        if (in_array($variable, $this->parametersList)) {
29 1
            if (isset($this->parametersValues[$variable])) {
30
                return $this->parametersValues[$variable];
31 1
            }
32 1
33 1
            return null;
34
        }
35
36
        throw new InvalidArgumentException(
37 65
            "Unknown configuration property " .
38
                get_called_class() .
39 65
                '->' .
40 64
                $variable
41
        );
42 64
    }
43
44
    /**
45 2
     * Service setter
46
     *
47 2
     * @param string $variable
48 2
     * @param mixed $value
49 2
     * @throws InvalidArgumentException
50
     */
51
    public function __set($variable, $value)
52
    {
53 38
        if (in_array($variable, $this->parametersList)) {
54
            $this->parametersValues[$variable] = $value;
55 38
56 38
            return $this;
57
        }
58
59 38
        throw new InvalidArgumentException(
60 38
            "Unknown configuration property " .
61
                get_called_class() .
62 11
                '->' .
63
                $variable
64 11
        );
65
    }
66
67 38
    /**
68
     * Service configurator
69 38
     *
70
     * @param array $parameters
71
     * @return void
72
     */
73
    public function configure($parameters = [])
74
    {
75
        foreach ($parameters as $key => $value) {
76
            $this->$key = $value;
77
        }
78
    }
79
80
    public function getParameter($parameter)
81
    {
82
        return $this->$parameter;
83
    }
84
}
85