Passed
Push — master ( 0a48c5...20fa94 )
by Michael
01:52
created

JsonApiExtension::getAlias()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Bundle\JsonApiBundle\DependencyInjection;
5
6
use Symfony\Component\Config\FileLocator;
7
use Symfony\Component\Config\Loader\LoaderInterface;
8
use Symfony\Component\DependencyInjection\DefinitionDecorator;
9
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
10
use Symfony\Component\DependencyInjection\Reference;
11
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
12
use Symfony\Component\DependencyInjection\ContainerBuilder;
13
14
class JsonApiExtension extends Extension
15
{
16
    /**
17
     * Configuration loader
18
     *
19
     * @var LoaderInterface
20
     */
21
    protected $loader;
22
23
    /**
24
     * JsonApiExtension constructor.
25
     *
26
     * @param LoaderInterface $loader
27
     */
28 2
    public function __construct(LoaderInterface $loader = null)
29
    {
30 2
        $this->loader = $loader;
31 2
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 2
    public function load(array $configs, ContainerBuilder $container)
37
    {
38 2
        $configuration = new JsonApiConfiguration();
39 2
        $config = $this->processConfiguration($configuration, $configs);
40
41 2
        $loader = $this->loader ?? new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
42
43 2
        $loader->load('services.yml');
44 2
        $loader->load('hydrator.yml');
45 2
        $loader->load('mapper.yml');
46 2
        $loader->load('http_client.yml');
47
48 2
        if (isset($config['mappers'])) {
49 2
            $this->createMappers($config['mappers'], $container);
50
        }
51 2
    }
52
53
    /**
54
     * Create mappers
55
     *
56
     * @param array            $config
57
     * @param ContainerBuilder $container
58
     */
59 2
    protected function createMappers(array $config, ContainerBuilder $container)
60
    {
61 2
        $handlers = $this->findMappingHandlers($container);
62
63 2
        foreach ($config as $name => $mapperDefinition)
64
        {
65 2
            $mapper = new DefinitionDecorator('mrtn_json_api.object_mapper.abstract');
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Depend...ion\DefinitionDecorator has been deprecated with message: The DefinitionDecorator class is deprecated since version 3.3 and will be removed in 4.0. Use the Symfony\Component\DependencyInjection\ChildDefinition class instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
66 2
            $mapper->addTag('mrtn_json_api.object_mapper', ['alias' => $name]);
67
68 2 View Code Duplication
            foreach ($mapperDefinition['handlers'] as $handlerName)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
            {
70 2
                if (! isset($handlers[$handlerName])) {
71
                    throw new \LogicException(sprintf('Mapping handler with name "%s" has not been registered as a service.', $handlerName));
72
                }
73
74 2
                $mapper->addMethodCall('addHandler', [
75 2
                    new Reference($handlers[$handlerName])
76
                ]);
77
            }
78
79 2
            $container->setDefinition('mrtn_json_api.object_mapper.' . $name, $mapper);
80
        }
81 2
    }
82
83
    /**
84
     * Find mapping handler registered in container
85
     *
86
     * @param  ContainerBuilder $container
87
     * @return array
88
     */
89 2
    protected function findMappingHandlers(ContainerBuilder $container): array
90
    {
91 2
        $handlers = $container->findTaggedServiceIds('mrtn_json_api.object_mapper.handler');
92
93 2
        $found = [];
94
95 2
        foreach ($handlers as $id => $tags)
96
        {
97 2
            foreach ($tags as $tag)
98
            {
99 2
                if (! isset($tag['alias'])) {
100
                    continue;
101
                }
102
103 2
                $found[$tag['alias']] = $id;
104
            }
105
        }
106
107 2
        return $found;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 1
    public function getAlias()
114
    {
115 1
        return 'mrtn_json_api';
116
    }
117
}