Passed
Push — master ( e52344...df819b )
by Rafael
09:21
created

InterfaceExtensionResolver   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 9
dl 0
loc 48
c 0
b 0
f 0
ccs 18
cts 19
cp 0.9474
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A __construct() 0 3 1
C configure() 0 22 7
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Definition\Extension;
12
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
use Ynlo\GraphQLBundle\Definition\DefinitionInterface;
15
use Ynlo\GraphQLBundle\Definition\HasExtensionsInterface;
16
use Ynlo\GraphQLBundle\Definition\InterfaceDefinition;
17
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint;
18
use Ynlo\GraphQLBundle\Extension\ExtensionInterface;
19
use Ynlo\GraphQLBundle\Util\ClassUtils;
20
21
/**
22
 * InterfaceExtensionResolver
23
 */
24
class InterfaceExtensionResolver extends AbstractDefinitionExtension
25
{
26
    /**
27
     * @var ContainerInterface
28
     */
29
    protected $container;
30
31
    /**
32
     * @param ContainerInterface $container
33
     */
34 1
    public function __construct(ContainerInterface $container)
35
    {
36 1
        $this->container = $container;
37 1
    }
38
39
    /**
40
     * {@inheritDoc}
41
     */
42 1
    public function getName(): string
43
    {
44 1
        return 'extension';
45
    }
46
47
    /**
48
     * {@inheritDoc}
49
     */
50 1
    public function configure(DefinitionInterface $definition, Endpoint $endpoint, array $config)
51
    {
52 1
        if (!$definition instanceof InterfaceDefinition || !$definition->getImplementors()) {
53 1
            return;
54
        }
55
56 1
        $bundleNamespace = ClassUtils::relatedBundleNamespace($definition->getClass());
57 1
        $extensionClass = ClassUtils::applyNamingConvention($bundleNamespace, 'Extension', null, $definition->getName().'Extension');
58 1
        if (class_exists($extensionClass)) {
59 1
            if ($this->container->has($extensionClass)) {
60
                $extension = $this->container->get($extensionClass);
61
            } else {
62 1
                $extension = new $extensionClass();
63
            }
64
65
            /** @var ExtensionInterface $extension */
66 1
            $priority = $extension->getPriority();
67
68 1
            foreach ($definition->getImplementors() as $implementor) {
69 1
                $object = $endpoint->getType($implementor);
70 1
                if ($object instanceof HasExtensionsInterface) {
71 1
                    $object->addExtension($extensionClass, $priority);
72
                }
73
            }
74
        }
75 1
    }
76
}
77