InstanceRecipe::resolve()   A
last analyzed

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