Passed
Push — PSR-11-2 ( e86d47...dfb1a3 )
by Nikolaos
05:09
created

Lazy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
/**
4
 * This file is part of the Phalcon Framework.
5
 *
6
 * (c) Phalcon Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 *
11
 * Implementation of this file has been influenced by AuraPHP
12
 *
13
 * @link    https://github.com/auraphp/Aura.Di
14
 * @license https://github.com/auraphp/Aura.Di/blob/4.x/LICENSE
15
 */
16
17
declare(strict_types=1);
18
19
namespace Phalcon\Container\Injection;
20
21
/**
22
 * Returns the value of a callable when invoked (thereby invoking the callable).
23
 *
24
 * @property callable $callable
25
 * @property array    $parameters
26
 */
27
class Lazy implements LazyInterface
28
{
29
    /**
30
     * The callable to invoke.
31
     *
32
     * @var callable
33
     */
34
    protected $callable;
35
36
    /**
37
     * Arguments for the callable.
38
     *
39
     * @var array
40
     */
41
    protected $parameters;
42
43
    /**
44
     * Constructor.
45
     *
46
     * @param callable $callable The callable to invoke.
47
     * @param array    $parameters   Arguments for the callable.
48
     */
49 4
    public function __construct($callable, array $parameters = [])
50
    {
51 4
        $this->callable   = $callable;
52 4
        $this->parameters = $parameters;
53 4
    }
54
55
    /**
56
     * Invokes the closure to create the instance.
57
     *
58
     * @return object The object created by the closure.
59
     */
60 4
    public function __invoke()
61
    {
62
        // convert Lazy objects in the callable
63 4
        if (is_array($this->callable)) {
64 2
            $this->processArray();
65 3
        } elseif ($this->callable instanceof LazyInterface) {
66 1
            $this->callable = $this->callable->__invoke();
67
        }
68
69 4
        $this->processParameters();
70
71
        // make the call
72 4
        return call_user_func_array($this->callable, $this->parameters);
73
    }
74
75
    /**
76
     * Converts Lazy objects in the callable if array
77
     */
78 2
    private function processArray(): void
79
    {
80 2
        foreach ($this->callable as $key => $val) {
81 2
            if ($val instanceof LazyInterface) {
82 2
                $this->callable[$key] = $val();
83
            }
84
        }
85 2
    }
86
87
    /**
88
     * Converts Lazy objects in the parameters
89
     */
90 4
    private function processParameters(): void
91
    {
92
        // convert Lazy objects in the parameters
93 4
        foreach ($this->parameters as $key => $val) {
94 3
            if ($val instanceof LazyInterface) {
95 2
                $this->parameters[$key] = $val();
96
            }
97
        }
98 4
    }
99
}
100