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

ProviderEntry::createFromCacheParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 1
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace Simply\Container\Entry;
4
5
use Psr\Container\ContainerInterface;
6
7
/**
8
 * Container entry that loads the value using a separate entry provider.
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 ProviderEntry implements EntryInterface
15
{
16
    /** @var string The provider class used to load the value */
17
    private $provider;
18
19
    /** @var string The method called for the provider class to load the value */
20
    private $method;
21
22
    /**
23
     * ProviderEntry constructor.
24
     * @param callable $providerMethod The provider callable provided as an array
25
     */
26 3
    public function __construct(callable $providerMethod)
27
    {
28 3
        if (!\is_array($providerMethod)) {
29 1
            throw new \InvalidArgumentException('The provider entry callable must be provided as an array');
30
        }
31
32 2
        [$provider, $method] = $providerMethod;
33
34 2
        $this->setProvider($provider);
35 2
        $this->setMethod($method);
36 2
    }
37
38
    /**
39
     * Sets the provider class based on the given provider instance.
40
     * @param object $provider The provider instance
41
     */
42 2
    private function setProvider(object $provider): void
43
    {
44 2
        $this->provider = \get_class($provider);
45 2
    }
46
47
    /**
48
     * Sets the method to call for the provider instance.
49
     * @param string $method The method to call for the provider
50
     */
51 2
    private function setMethod(string $method): void
52
    {
53 2
        $this->method = $method;
54 2
    }
55
56
    /** {@inheritdoc} */
57 1
    public static function createFromCacheParameters(array $parameters): EntryInterface
58
    {
59
        /** @var ProviderEntry $entry */
60 1
        $entry = (new \ReflectionClass(static::class))->newInstanceWithoutConstructor();
61
62
        [
63 1
            $entry->provider,
64 1
            $entry->method,
65 1
        ] = $parameters;
66
67 1
        return $entry;
68
    }
69
70
    /** {@inheritdoc} */
71 1
    public function getCacheParameters(): array
72
    {
73
        return [
74 1
            $this->provider,
75 1
            $this->method,
76
        ];
77
    }
78
79
    /** {@inheritdoc} */
80 2
    public function isFactory(): bool
81
    {
82 2
        return false;
83
    }
84
85
    /** {@inheritdoc} */
86 2
    public function getValue(ContainerInterface $container)
87
    {
88 2
        $provider = $container->get($this->provider);
89
90 2
        return $provider->{$this->method}($container);
91
    }
92
}
93