Memoize::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Fundic\Factory\Decorator;
6
7
use Fundic\Factory\ValueFactory;
8
use Psr\Container\ContainerInterface;
9
10
final class Memoize implements ValueFactory
11
{
12
    /**
13
     * @var ValueFactory
14
     */
15
    private $inner;
16
17
    /**
18
     * @var mixed
19
     */
20
    private $result;
21
22
    /**
23
     * @var bool
24
     */
25
    private $alreadyComputed = false;
26
27
    public function __construct(ValueFactory $inner)
28
    {
29
        $this->inner = $inner;
30
    }
31
32
    public function __invoke(ContainerInterface $container, string $name)
33
    {
34
        if (!$this->alreadyComputed) {
35
            $this->result = ($this->inner)($container, $name);
36
            $this->alreadyComputed = true;
37
        }
38
39
        return $this->result;
40
    }
41
}
42