Completed
Push — master ( 3b79f3...426227 )
by Kirill
03:11
created

Registry   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 90
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 9 2
A __construct() 0 3 1
A getBuilder() 0 6 1
A build() 0 3 1
A getMapping() 0 10 3
A getContainer() 0 3 1
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\Adapters\Webonyx;
11
12
use GraphQL\Type\Definition\Directive;
13
use GraphQL\Type\Definition\Type;
14
use Railt\Adapters\Webonyx\Builders\TypeBuilder;
15
use Railt\Container\ContainerInterface;
0 ignored issues
show
Bug introduced by
The type Railt\Container\ContainerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Railt\Reflection\Contracts\Definitions;
0 ignored issues
show
Bug introduced by
The type Railt\Reflection\Contracts\Definitions was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Railt\Reflection\Contracts\Definitions\TypeDefinition;
0 ignored issues
show
Bug introduced by
The type Railt\Reflection\Contrac...initions\TypeDefinition was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
/**
20
 * Class Registry
21
 */
22
class Registry
23
{
24
    private const BUILDER_MAPPINGS = [
25
        Definitions\ObjectDefinition::class    => Builders\ObjectBuilder::class,
0 ignored issues
show
Bug introduced by
The type Railt\Reflection\Contrac...itions\ObjectDefinition was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
        Definitions\InterfaceDefinition::class => Builders\InterfaceBuilder::class,
0 ignored issues
show
Bug introduced by
The type Railt\Reflection\Contrac...ons\InterfaceDefinition was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
        Definitions\DirectiveDefinition::class => Builders\DirectiveBuilder::class,
0 ignored issues
show
Bug introduced by
The type Railt\Reflection\Contrac...ons\DirectiveDefinition was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
        Definitions\ScalarDefinition::class    => Builders\ScalarBuilder::class,
0 ignored issues
show
Bug introduced by
The type Railt\Reflection\Contrac...itions\ScalarDefinition was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
    ];
30
31
    /**
32
     * @var array|Type[]
33
     */
34
    private $types = [];
35
36
    /**
37
     * @var ContainerInterface
38
     */
39
    private $container;
40
41
    /**
42
     * Registry constructor.
43
     * @param ContainerInterface $container
44
     */
45
    public function __construct(ContainerInterface $container)
46
    {
47
        $this->container = $container;
48
    }
49
50
    /**
51
     * @return ContainerInterface
52
     */
53
    public function getContainer(): ContainerInterface
54
    {
55
        return $this->container;
56
    }
57
58
    /**
59
     * @param TypeDefinition $definition
60
     * @return Type|Directive
61
     * @throws \InvalidArgumentException
62
     */
63
    public function get(TypeDefinition $definition)
64
    {
65
        $name = $definition->getName();
66
67
        if (! \array_key_exists($name, $this->types)) {
68
            $this->types[$name] = $this->build($definition);
69
        }
70
71
        return $this->types[$name];
72
    }
73
74
    /**
75
     * @param TypeDefinition $definition
76
     * @return Type|Directive
77
     * @throws \InvalidArgumentException
78
     */
79
    private function build(TypeDefinition $definition)
80
    {
81
        return $this->getBuilder($definition)->build();
82
    }
83
84
    /**
85
     * @param TypeDefinition $definition
86
     * @return TypeBuilder
87
     * @throws \InvalidArgumentException
88
     */
89
    private function getBuilder(TypeDefinition $definition): TypeBuilder
90
    {
91
        /** @var TypeBuilder $builder */
92
        $builder = $this->getMapping($definition);
93
94
        return new $builder($definition, $this);
95
    }
96
97
    /**
98
     * @param TypeDefinition $definition
99
     * @return string
100
     * @throws \InvalidArgumentException
101
     */
102
    private function getMapping(TypeDefinition $definition): string
103
    {
104
        foreach (self::BUILDER_MAPPINGS as $contract => $builder) {
105
            if ($definition instanceof $contract) {
106
                return $builder;
107
            }
108
        }
109
110
        $error = 'Can not find an allowable Builder for the %s';
111
        throw new \InvalidArgumentException(\sprintf($error, $definition));
112
    }
113
}
114