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

LazyValue::__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
use InvalidArgumentException;
22
use Phalcon\Container\Resolver\Resolver;
23
24
/**
25
 * Returns an arbitrary value when invoked.
26
 *
27
 * @property string   $key
28
 * @property Resolver $resolver
29
 */
30
class LazyValue implements LazyInterface
31
{
32
    /**
33
     * The value key to retrieve.
34
     *
35
     * @var string
36
     */
37
    protected $key;
38
39
    /**
40
     * The Resolver that holds the values.
41
     *
42
     * @var Resolver
43
     */
44
    protected $resolver;
45
46
    /**
47
     * Constructor.
48
     *
49
     * @param Resolver $resolver The Resolver that holds the values.
50
     * @param string   $key      The value key to retrieve.
51
     */
52 3
    public function __construct(Resolver $resolver, string $key)
53
    {
54 3
        $this->resolver = $resolver;
55 3
        $this->key      = $key;
56 3
    }
57
58
    /**
59
     *
60
     * Returns the lazy value.
61
     *
62
     * @return mixed
63
     *
64
     */
65 3
    public function __invoke()
66
    {
67 3
        if (!$this->resolver->values()->has($this->key)) {
68 1
            throw new InvalidArgumentException(
69 1
                'Unknown key (' . $this->key . ') in container value'
70
            );
71
        }
72
73 2
        $value = $this->resolver->values()->get($this->key);
74
        // convert Lazy objects
75 2
        if ($value instanceof LazyInterface) {
76 1
            $value = $value();
77
        }
78
79 2
        return $value;
80
    }
81
}
82