Passed
Pull Request — master (#2)
by Riikka
02:22
created

CallableEntry   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getCacheParameters() 0 3 1
A createFromCacheParameters() 0 7 1
A getValue() 0 3 1
A isFactory() 0 3 1
1
<?php
2
3
namespace Simply\Container\Entry;
4
5
use Psr\Container\ContainerInterface;
6
7
/**
8
 * Container entry that uses a callable to determine the value for the entry.
9
 *
10
 * @author Riikka Kalliomäki <[email protected]>
11
 * @copyright Copyright (c) 2018 Riikka Kalliomäki
12
 * @license http://opensource.org/licenses/mit-license.php MIT License
13
 */
14
class CallableEntry implements EntryInterface
15
{
16
    /** @var callable The callable used to determine the value for the entry */
17
    private $callable;
18
19
    /**
20
     * CallableEntry constructor.
21
     * @param callable $callable The callable used to determine the value for the entry
22
     */
23 3
    public function __construct(callable $callable)
24
    {
25 3
        $this->callable = $callable;
26 3
    }
27
28
    /** {@inheritdoc} */
29 2
    public static function createFromCacheParameters(array $parameters): EntryInterface
30
    {
31
        /** @var CallableEntry $entry */
32 2
        $entry = (new \ReflectionClass(static::class))->newInstanceWithoutConstructor();
33 2
        $entry->callable = $parameters[0];
34
35 2
        return $entry;
36
    }
37
38
    /** {@inheritdoc} */
39 2
    public function getCacheParameters(): array
40
    {
41 2
        return [$this->callable];
42
    }
43
44
    /** {@inheritdoc} */
45 3
    public function isFactory(): bool
46
    {
47 3
        return false;
48
    }
49
50
    /** {@inheritdoc} */
51 3
    public function getValue(ContainerInterface $container)
52
    {
53 3
        return ($this->callable)($container);
54
    }
55
}
56