GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( dda63a...3f35d1 )
by Hong
02:45
created

Factory::getObjectMethod()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.2
cc 4
eloc 9
nc 3
nop 2
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Di
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Di\Factory;
16
17
use Phossa2\Di\Container;
18
use Phossa2\Di\Message\Message;
19
use Phossa2\Di\Traits\FactoryTrait;
20
use Phossa2\Shared\Base\ObjectAbstract;
21
use Phossa2\Di\Exception\LogicException;
22
use Phossa2\Di\Interfaces\FactoryInterface;
23
24
/**
25
 * Factory
26
 *
27
 * Wrapper of factorying methods for container
28
 *
29
 * @package Phossa2\Di
30
 * @author  Hong Zhang <[email protected]>
31
 * @see     FactoryInterface
32
 * @version 2.0.0
33
 * @since   2.0.0 added
34
 */
35
class Factory extends ObjectAbstract implements FactoryInterface
36
{
37
    use FactoryTrait;
38
39
    /**
40
     * @param  Container $container
41
     * @access public
42
     */
43
    public function __construct(Container $container)
44
    {
45
        $this->master = $container;
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     */
51
    public function createInstance(/*# string */ $rawId, array $arguments)
52
    {
53
        // get resolved definition
54
        $def = $this->getDefinition($rawId, $arguments);
55
56
        // arguments
57
        $args = isset($def['args']) ? $def['args'] : [];
58
59
        if (is_string($def['class'])) {
60
            $obj = $this->constructObject($def['class'], $args);
61
62
        } else {
63
            $obj = $this->executeCallable($def['class'], $args);
64
        }
65
66
        // execute after-creation methods
67
        $this->afterCreation($obj, $def);
68
69
        return $obj;
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75
    public function executeCallable($callable, array $arguments = [])
76
    {
77
        // not callable
78
        if (!is_callable($callable)) {
79
            return $callable;
80
        }
81
82
        if (!empty($arguments)) {
83
            $params = $this->getCallableParameters($callable);
84
            $args = $this->matchArguments($params, $arguments);
85
            return call_user_func_array($callable, $args);
86
        } else {
87
            return call_user_func($callable);
88
        }
89
    }
90
91
    /**
92
     * {@inheritDoc}
93
     */
94
    public function executeMethodBatch(array $methods, $object = null)
95
    {
96
        foreach ($this->mergeMethods($methods) as $method) {
97
            $this->executeMethod($method, $object);
98
        }
99
    }
100
101
    /**
102
     * if $object provided, build callable like [$object, $method] and execute it.
103
     *
104
     * method:
105
     *
106
     * - callable
107
     *
108
     * - array ['function', [ arguments...]]
109
     *
110
     * - array [callable, [ arguments ...]]
111
     *
112
     * - array ['method', [ arguments ...]]
113
     *   will be converted to [[$object, 'method'], [ ... ]]
114
     *
115
     * @param  array|callable method
116
     * @param  object|null $object to construct callable
117
     * @throws LogicException if something goes wrong
118
     * @access protected
119
     */
120
    protected function executeMethod($method, $object = null)
121
    {
122
        // is callable
123
        if (is_callable($method)) {
124
            return $this->executeCallable($method);
125
126
        // is [ method, arguments ]
127
        } elseif (isset($method[0])) {
128
            return $this->executeCallable(
129
                $this->getObjectMethod($object, $method[0]), // callable
130
                isset($method[1]) ? $method[1] : [] // arguments
131
            );
132
        }
133
    }
134
135
    /**
136
     * Things to do after an object created.
137
     *
138
     * @param  object $object
139
     * @param  array $definition service definition for $object
140
     * @access protected
141
     */
142
    protected function afterCreation($object, array $definition)
143
    {
144
        // execute methods of THIS object
145
        if (isset($definition['methods'])) {
146
            $this->executeMethodBatch($definition['methods'], $object);
147
        }
148
149
        // execute common methods for all objects
150
        if (!isset($definition['skip']) || !$definition['skip']) {
151
            $this->executeCommonBatch($object);
152
        }
153
    }
154
155
    /**
156
     * Execute common methods defined in 'di.common' for all objects
157
     *
158
     * Methods are in the form of
159
     *
160
     *   [ interfaceOrClassname, [methodOrCallable, ArgumentsArray]],
161
     *   [ testCallable($obj, $container), [methodOrCallable, ArgumentsArray],
162
     *   ...
163
     *
164
     * @param  object $object
165
     * @return $this
166
     * @access protected
167
     */
168
    protected function executeCommonBatch($object)
169
    {
170
        foreach ($this->getCommonMethods() as $method) {
171
            $tester = $method[0];
172
            $runner = $method[1];
173
            if (call_user_func_array($tester, [$object, $this->master])) {
174
                $this->executeMethod($runner, $object);
175
            }
176
        }
177
        return $this;
178
    }
179
180
    /**
181
     * Get common methods
182
     *
183
     * @return array
184
     * @access protected
185
     */
186
    protected function getCommonMethods()/*# : array */
187
    {
188
        // get di.common node
189
        $methods = $this->mergeMethods(
190
            $this->master->getResolver()->getInSection('', 'common')
191
        );
192
193
        // fix tester
194
        foreach ($methods as $i => $pair) {
195
            if (is_string($pair[0])) {
196
                $tester = $pair[0];
197
                $methods[$i][0] = function($obj) use ($tester) {
198
                    return is_a($obj, $tester);
199
                };
200
            }
201
        }
202
203
        return $methods;
204
    }
205
206
    /**
207
     * Merge different sections of a node
208
     *
209
     * convert
210
     *   `['section1' => [[1], [2]], 'section2' => [[3], [4]]]`
211
     *
212
     * to
213
     *   `[[1], [2], [3], [4]]`
214
     *
215
     * @param  array|null $nodeData
216
     * @return array
217
     * @access protected
218
     */
219
    protected function mergeMethods($nodeData)/*# : array */
220
    {
221
        // no merge
222
        if (empty($nodeData) || isset($nodeData[0])) {
223
            return (array) $nodeData;
224
        }
225
226
        // in sections
227
        $result = [];
228
        foreach ($nodeData as $data) {
229
            $result = array_merge($result, $data);
230
        }
231
        return $result;
232
    }
233
234
    /**
235
     * Returns [$object, $method] if it is a callable, otherwise returns $method
236
     *
237
     * @param  mixed $object
238
     * @param  mixed $method
239
     * @return bool
240
     * @access protected
241
     */
242
    protected function getObjectMethod($object, $method)/*# : bool */
243
    {
244
        if (is_string($method) && method_exists($object, $method)) {
245
            return [$object, $method];
246
        } elseif (is_callable($method)) {
247
            return $method;
248
        } else {
249
            throw new LogicException(
250
                Message::get(Message::DI_CALLABLE_BAD, $method),
251
                Message::DI_CALLABLE_BAD
252
            );
253
        }
254
    }
255
}
256