Passed
Push — master ( 02eddd...2aa0cc )
by mcfog
02:49 queued 18s
created

AutowireRecipe   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 4
eloc 11
dl 0
loc 27
ccs 10
cts 11
cp 0.9091
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resolve() 0 10 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\ContainerException;
9
use Psr\Container\ContainerInterface;
10
11
/**
12
 * Recipe that calls $factory->produce to resolve. a.k.a autowire.
13
 *
14
 * Since `produce` method will save the product under `$className` key in container and reuse it, it's singleton. If
15
 * you need multiple instance, use `InstanceRecipe` instead.
16
 */
17
class AutowireRecipe extends AbstractRecipe
18
{
19
    /**
20
     * @var null|string
21
     */
22
    protected $className;
23
    /**
24
     * @var array
25
     */
26
    protected $extra;
27
28 2
    public function __construct(?string $className = null, array $extra = [])
29
    {
30 2
        $this->className = $className;
31 2
        $this->extra = $extra;
32 2
    }
33
34 2
    public function resolve(ContainerInterface $container, ?string $id = null)
35
    {
36 2
        $className = $this->className ?? $id;
37 2
        if ($className === null || !class_exists($className)) {
38
            throw new ContainerException('unknown autowire class name');
39
        }
40
41 2
        return Factory::of($container)->produce(
42 2
            /** @scrutinizer ignore-type */ $className,
43 2
            $this->extra
44
        );
45
    }
46
}
47