Passed
Push — master ( 55fca5...85d75f )
by Kirill
04:44 queued 10s
created

Factory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of Attributes package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Attributes\Internal\Instantiator;
13
14
use Doctrine\Common\Annotations\NamedArgumentConstructorAnnotation;
15
16
class Factory implements InstantiatorInterface
17
{
18
    /**
19
     * @var DoctrineInstantiator
20
     */
21
    private $doctrine;
22
23
    /**
24
     * @var NamedArgumentsInstantiator
25
     */
26
    private $named;
27
28
    /**
29
     * Factory constructor.
30
     */
31
    public function __construct()
32
    {
33
        $this->doctrine = new DoctrineInstantiator();
34
        $this->named = new NamedArgumentsInstantiator();
35
    }
36
37
    /**
38
     * @param \ReflectionClass $attr
39
     * @param array $arguments
40
     * @param string $context
41
     * @return object
42
     */
43
    public function instantiate(\ReflectionClass $attr, array $arguments, string $context): object
44
    {
45
        if ($this->isNamedArguments($attr)) {
46
            return $this->named->instantiate($attr, $arguments, $context);
47
        }
48
49
        return $this->doctrine->instantiate($attr, $arguments, $context);
50
    }
51
52
    /**
53
     * @param \ReflectionClass $class
54
     * @return bool
55
     */
56
    private function isNamedArguments(\ReflectionClass $class): bool
57
    {
58
        return \is_subclass_of($class->getName(), NamedArgumentConstructorAnnotation::class);
59
    }
60
}