Completed
Push — master ( 65a611...b28387 )
by Pol
01:50
created

DynamicObjectsTrait   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 221
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 23
lcom 2
cbo 1
dl 0
loc 221
ccs 58
cts 58
cp 1
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A addDynamicProperty() 0 8 1
A addDynamicMethod() 0 8 1
A hasDynamicProperty() 0 4 1
A hasDynamicMethod() 0 4 1
A getDynamicProperty() 0 8 2
A getDynamicMethod() 0 5 2
A clearDynamicProperties() 0 4 1
A clearDynamicMethods() 0 4 1
A removeDynamicProperty() 0 4 1
A removeDynamicMethod() 0 4 1
A __call() 0 9 2
A __get() 0 15 3
A __set() 0 6 2
A doDynamicRequest() 0 12 4
1
<?php
2
3
namespace drupol\DynamicObjects;
4
5
use drupol\Memoize\MemoizeTrait;
6
7
/**
8
 * Trait DynamicObjectsTrait.
9
 *
10
 * @package drupol\DynamicObjects
11
 */
12
trait DynamicObjectsTrait
13
{
14
    use MemoizeTrait;
15
16
    /**
17
     * @var array
18
     */
19
    protected static $dynamicMethods = array();
20
21
    /**
22
     * @var array
23
     */
24
    protected static $dynamicProperties = array();
25
26
    /**
27
     * Add a dynamic property.
28
     *
29
     * @param string $name
30
     *   The property name.
31
     * @param mixed $value
32
     *   The property value.
33
     * @param bool $memoize
34
     *   Memoize parameter.
35
     */
36 5
    public static function addDynamicProperty($name, $value, $memoize = false)
37
    {
38 5
        static::$dynamicProperties[get_called_class()][$name] = [
39 5
          'name' => $name,
40 5
          'factory' => $value,
41 5
          'memoize' => $memoize,
42
        ];
43 5
    }
44
45
    /**
46
     * Add a dynamic method.
47
     *
48
     * @param $name
49
     *   The method name.
50
     * @param \Closure $func
51
     *   The method.
52
     * @param bool $memoize
53
     *   Memoize parameter.
54
     */
55 4
    public static function addDynamicMethod($name, \Closure $func, $memoize = false)
56
    {
57 4
        static::$dynamicMethods[get_called_class()][$name] = [
58 4
          'name' => $name,
59 4
          'factory' => $func,
60 4
          'memoize' => $memoize,
61
        ];
62 4
    }
63
64
    /**
65
     * Check if a dynamic property exists.
66
     *
67
     * @param string $name
68
     *   The property name.
69
     * @return bool
70
     *   True if the property exists, false otherwise.
71
     */
72 8
    public static function hasDynamicProperty($name)
73
    {
74 8
        return isset(static::$dynamicProperties[get_called_class()][$name]);
75
    }
76
77
    /**
78
     * Check if a dynamic method exists.
79
     *
80
     * @param string $name
81
     *   The property name.
82
     * @return bool
83
     *   True if the property exists, false otherwise.
84
     */
85 6
    public static function hasDynamicMethod($name)
86
    {
87 6
        return isset(static::$dynamicMethods[get_called_class()][$name]);
88
    }
89
90
    /**
91
     * Get a dynamic property.
92
     *
93
     * @param $name
94
     *   The property name.
95
     * @return mixed|null
96
     *   The property value if it exists, null otherwise.
97
     */
98 4
    public static function getDynamicProperty($name)
99
    {
100 4
        if (static::hasDynamicProperty($name)) {
101 3
            return static::$dynamicProperties[get_called_class()][$name];
102
        }
103
104 1
        return null;
105
    }
106
107
    /**
108
     * Get a dynamic method.
109
     *
110
     * @param $name
111
     *   The method name.
112
     * @return mixed|null
113
     *   The method if it exists, null otherwise.
114
     */
115 2
    public static function getDynamicMethod($name)
116
    {
117 2
        return ( static::hasDynamicMethod($name) ) ?
118 2
          static::$dynamicMethods[get_called_class()][$name] : null;
119
    }
120
121
    /**
122
     * Clear dynamic properties.
123
     */
124 1
    public static function clearDynamicProperties()
125
    {
126 1
        static::$dynamicProperties[get_called_class()] = array();
127 1
    }
128
129
    /**
130
     * Clear dynamic methods.
131
     */
132 1
    public static function clearDynamicMethods()
133
    {
134 1
        static::$dynamicMethods[get_called_class()] = array();
135 1
    }
136
137
    /**
138
     * Remove a dynamic property.
139
     *
140
     * @param string $name
141
     *   The property name.
142
     */
143 1
    public static function removeDynamicProperty($name)
144
    {
145 1
        unset(static::$dynamicProperties[get_called_class()][$name]);
146 1
    }
147
148
    /**
149
     * Remove a dynamic method.
150
     *
151
     * @param string $name
152
     *   The method name.
153
     */
154 1
    public static function removeDynamicMethod($name)
155
    {
156 1
        unset(static::$dynamicMethods[get_called_class()][$name]);
157 1
    }
158
159
    /**
160
     * Execute a closure.
161
     *
162
     * @param \Closure $func
163
     *   The closure.
164
     * @param array $parameters
165
     *   The closure's parameters.
166
     * @param bool $memoize
167
     *   The memoize parameter.
168
     *
169
     * @return mixed|null
170
     *   The return of the closure.
171
     *
172
     * @throws \Psr\SimpleCache\InvalidArgumentException
173
     */
174 4
    public function doDynamicRequest(\Closure $func, array $parameters = [], $memoize = false)
175
    {
176 4
        if (!class_exists('\drupol\Memoize\Memoize') || false === $memoize) {
177 4
            $memoize = false;
178
        }
179
180 4
        if (true === $memoize) {
181 2
            return $this->memoize($func, $parameters);
182
        }
183
184 4
        return call_user_func_array($func, $parameters);
185
    }
186
187
    /**
188
     * @param $method
189
     * @param array $parameters
190
     *
191
     * @return mixed
192
     * @throws \Psr\SimpleCache\InvalidArgumentException
193
     */
194 3
    public function __call($method, array $parameters = array())
195
    {
196 3
        if (static::hasDynamicMethod($method)) {
197 2
            $data = static::getDynamicMethod($method);
198 2
            return $this->doDynamicRequest($data['factory'], $parameters, $data['memoize']);
199
        }
200
201 1
        throw new \BadMethodCallException(sprintf('Undefined method: %s().', $method));
202
    }
203
204
    /**
205
     * {inheritdoc}
206
     */
207 4
    public function __get($property)
208
    {
209 4
        if (static::hasDynamicProperty($property)) {
210 3
            $data = static::getDynamicProperty($property);
211
212 3
            $return = $data['factory'];
213 3
            if (is_callable($data['factory'])) {
214 2
                $return = $this->doDynamicRequest($data['factory'], [], $data['memoize']);
215
            }
216
217 3
            return $return;
218
        }
219
220 1
        throw new \DomainException(sprintf('Undefined property: %s().', $property));
221
    }
222
223
    /**
224
     * {inheritdoc}
225
     */
226 3
    public function __set($property, $value)
227
    {
228 3
        if (static::hasDynamicProperty($property)) {
229 1
            static::addDynamicProperty($property, $value);
230
        }
231 3
    }
232
}
233