InstanceRecipe::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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
use Lit\Air\Psr\ContainerException;
10
11
/**
12
 * Recipe that calls $container->instantiate to resolve.
13
 *
14
 * Since `instantiate` method only create instance, so this recipe will create multiple instance when resolved multiple
15
 * times. Can be decorated by SingletonDecorator to makes it a singleton.
16
 */
17
class InstanceRecipe extends AbstractRecipe
18
{
19
    /**
20
     * @var null|string
21
     */
22
    protected $className;
23
    /**
24
     * @var array
25
     */
26
    protected $extra;
27
28 1
    public function __construct(?string $className = null, array $extra = [])
29
    {
30 1
        $this->className = $className;
31 1
        $this->extra = $extra;
32 1
    }
33
34 1
    public function resolve(Container $container)
35
    {
36 1
        $className = $this->className;
37 1
        if ($className === null || !class_exists($className)) {
38
            throw new ContainerException('unknown class name');
39
        }
40
41 1
        return Factory::of($container)->instantiate(
42 1
            /** @scrutinizer ignore-type */            $className,
43 1
            $this->extra
44
        );
45
    }
46
}
47