SimpleFactoryTrait::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Dazzle\Util\Factory;
4
5
use Dazzle\Throwable\Exception\Logic\IllegalCallException;
6
use Dazzle\Throwable\Exception\Logic\IllegalFieldException;
7
8
trait SimpleFactoryTrait
9
{
10
    /**
11
     * @var mixed[]
12
     */
13
    protected $params;
14
15
    /**
16
     * @var callable|null
17
     */
18
    protected $definition;
19
20
    /**
21
     *
22
     */
23 46
    public function __construct()
24
    {
25 46
        $this->params = [];
26 46
        $this->definition = null;
27 46
    }
28
29
    /**
30
     *
31
     */
32 46
    public function __destruct()
33
    {
34 46
        unset($this->params);
35 46
        unset($this->definition);
36 46
    }
37
38
    /**
39
     * @see SimpleFactoryInterface::bindParam
40
     */
41 14
    public function bindParam($name, $value)
42
    {
43 14
        $this->params[$name] = $value;
44
45 14
        return $this;
46
    }
47
48
    /**
49
     * @see SimpleFactoryInterface::unbindParam
50
     */
51 6
    public function unbindParam($name)
52
    {
53 6
        unset($this->params[$name]);
54
55 6
        return $this;
56
    }
57
58
    /**
59
     * @see SimpleFactoryInterface::getParam
60
     */
61 6 View Code Duplication
    public function getParam($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...
62
    {
63 6
        if (!array_key_exists($name, $this->params))
64
        {
65 2
            throw new IllegalFieldException("Factory does not posses param [$name].");
66
        }
67
68 4
        return $this->invoke($this->params[$name]);
69
    }
70
71
    /**
72
     * @see SimpleFactoryInterface::hasParam
73
     */
74 10
    public function hasParam($name)
75
    {
76 10
        return array_key_exists($name, $this->params);
77
    }
78
79
    /**
80
     * @see SimpleFactoryInterface::getParams
81
     */
82 4
    public function getParams()
83
    {
84 4
        return $this->params;
85
    }
86
87
    /**
88
     * @see SimpleFactoryInterface::define
89
     */
90 10
    public function define(callable $factoryMethod)
91
    {
92 10
        $this->definition = $factoryMethod;
93
94 10
        return $this;
95
    }
96
97
    /**
98
     * @see SimpleFactoryInterface::getDefinition
99
     */
100 4
    public function getDefinition()
101
    {
102 4
        if (!isset($this->definition))
103
        {
104 2
            throw new IllegalFieldException("SimpleFactory does not posses definition.");
105
        }
106
107 2
        return $this->definition;
108
    }
109
110
    /**
111
     * @see SimpleFactoryInterface::hasDefinition
112
     */
113 6
    public function hasDefinition()
114
    {
115 6
        return isset($this->definition);
116
    }
117
118
    /**
119
     * @see SimpleFactoryInterface::create
120
     */
121 4
    public function create($args = [])
122
    {
123 4
        if ($this->definition === null)
124
        {
125 2
            throw new IllegalCallException("Factory does not posses required definition.");
126
        }
127
128 2
        return call_user_func_array($this->definition, (array) $args);
129
    }
130
131
    /**
132
     * @param mixed $value
133
     * @return mixed
134
     */
135 4
    private function invoke($value)
136
    {
137 4
        return is_callable($value) ? $value() : $value;
138
    }
139
}
140