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

AutowireRecipe::resolve()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 10
ccs 6
cts 7
cp 0.8571
rs 10
cc 3
nc 2
nop 2
crap 3.0261
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