Completed
Push — master ( 2aa0cc...efafc0 )
by mcfog
03:24 queued 55s
created

AutowireRecipe::resolve()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.1825

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 16
ccs 8
cts 11
cp 0.7272
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.1825
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