Memoize   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 32
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 9 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