AutowireRecipe   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 81.25%

Importance

Changes 0
Metric Value
wmc 4
eloc 17
dl 0
loc 38
ccs 13
cts 16
cp 0.8125
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A resolve() 0 16 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lit\Air\Recipe;
6
7
use Lit\Air\Factory;
8
use Lit\Air\Psr\Container;
9
10
/**
11
 * Recipe that calls $factory->produce to resolve. a.k.a autowire.
12
 */
13
class AutowireRecipe extends AbstractRecipe
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $className;
19
    /**
20
     * @var array
21
     */
22
    protected $extra;
23
    /**
24
     * @var bool
25
     */
26
    protected $cached;
27
28 2
    public function __construct(string $className, array $extra = [], bool $cached = true)
29
    {
30 2
        $this->className = $className;
31 2
        $this->extra = $extra;
32 2
        $this->cached = $cached;
33 2
    }
34
35 2
    public function resolve(Container $container)
36
    {
37 2
        $className = $this->className;
38 2
        if ($container->getRecipe($className) === $this) {
39
            // calling produce will cause infinite loop if the key is same as $classname, break it here.
40 1
            $instance = Factory::of($container)->instantiate($className, $this->extra);
41
            if ($this->cached) {
42
                $container->set($className, $instance);
43
            }
44
            return $instance;
45
        }
46
47 1
        return Factory::of($container)->produce(
48 1
            /** @scrutinizer ignore-type */            $className,
49 1
            $this->extra,
50 1
            $this->cached
51
        );
52
    }
53
}
54