Passed
Push — master ( d7bd96...cddd2f )
by Michael
06:38
created

JsonApiExtension::createEventDispatcherDecorator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 2
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\Definition;
9
use Symfony\Component\DependencyInjection\DefinitionDecorator;
10
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
11
use Symfony\Component\DependencyInjection\Reference;
12
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
15
class JsonApiExtension extends Extension
16
{
17
    const ALIAS = 'mrtn_json_api';
18
19
    /**
20
     * Configuration loader
21
     *
22
     * @var LoaderInterface
23
     */
24
    protected $loader;
25
26
    /**
27
     * JsonApiExtension constructor.
28
     *
29
     * @param LoaderInterface $loader
30
     */
31 3
    public function __construct(LoaderInterface $loader = null)
32
    {
33 3
        $this->loader = $loader;
34 3
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 3
    public function load(array $configs, ContainerBuilder $container)
40
    {
41 3
        $configuration = new JsonApiConfiguration();
42 3
        $config = $this->processConfiguration($configuration, $configs);
43
44 3
        $loader = $this->loader ?? new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
45
46 3
        $loader->load('services.yml');
47 3
        $loader->load('hydrator.yml');
48 3
        $loader->load('mapper.yml');
49 3
        $loader->load('http_client.yml');
50
51 3
        if (! empty($config['mappers'])) {
52 2
            $this->createMappers($config['mappers'], $container);
53
        }
54
55 3
        if (! empty($config['resource_clients'])) {
56 2
            $this->createResourceClients($config['resource_clients'], $container);
57
        }
58 3
    }
59
60
    /**
61
     * Create mappers
62
     *
63
     * @param array            $config
64
     * @param ContainerBuilder $container
65
     */
66 2
    protected function createMappers(array $config, ContainerBuilder $container)
67
    {
68 2
        $handlers = $this->findMappingHandlers($container);
69
70 2
        foreach ($config as $name => $mapperDefinition)
71
        {
72 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...
73 2
            $mapper->addTag('mrtn_json_api.object_mapper', ['alias' => $name]);
74
75 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...
76
            {
77 2
                if (! isset($handlers[$handlerName])) {
78
                    throw new \LogicException(sprintf('Mapping handler with name "%s" has not been registered as a service.', $handlerName));
79
                }
80
81 2
                $mapper->addMethodCall('addHandler', [
82 2
                    new Reference($handlers[$handlerName])
83
                ]);
84
            }
85
86 2
            $container->setDefinition('mrtn_json_api.object_mapper.' . $name, $mapper);
87
        }
88 2
    }
89
90
    /**
91
     * Find mapping handler registered in container
92
     *
93
     * @param  ContainerBuilder $container
94
     * @return array
95
     */
96 2
    protected function findMappingHandlers(ContainerBuilder $container): array
97
    {
98 2
        $handlers = $container->findTaggedServiceIds('mrtn_json_api.object_mapper.handler');
99
100 2
        $found = [];
101
102 2
        foreach ($handlers as $id => $tags)
103
        {
104 2
            foreach ($tags as $tag)
105
            {
106 2
                if (! isset($tag['alias'])) {
107
                    continue;
108
                }
109
110 2
                $found[$tag['alias']] = $id;
111
            }
112
        }
113
114 2
        return $found;
115
    }
116
117
    /**
118
     * Create resources-based clients
119
     *
120
     * @param array            $config
121
     * @param ContainerBuilder $container
122
     */
123 2
    protected function createResourceClients(array $config, ContainerBuilder $container)
124
    {
125 2
        $repositoryClass = $container->getParameter('mrtn_json_api.route_repository.class');
126 2
        $clientClass     = $container->getParameter('mrtn_json_api.resource_client.class');
127
128 2
        foreach ($config as $name => $definition)
129
        {
130 2
            $this->createEventDispatcherDecorator($container, $name);
131
132 2
            $routes = $this->createRoutesDefinition($definition['resources']);
133
134 2
            $repository = new Definition($repositoryClass, [$definition['base_url'], $routes]);
135 2
            $repository->setPublic(false);
136
137 2
            $client = new Definition($clientClass, [
138 2
                new Reference('mrtn_json_api.http_client.decorator.event_dispatcher.' . $name),
139 2
                new Reference('mrtn_json_api.route_repository.' . $name)
140
            ]);
141
142 2
            $container->setDefinition('mrtn_json_api.route_repository.' . $name, $repository);
143 2
            $container->setDefinition('mrtn_json_api.resource_client.' . $name, $client);
144
        }
145 2
    }
146
147
    /**
148
     * Create event dispatcher decorator for resource-based http-client
149
     *
150
     * @param ContainerBuilder $container
151
     * @param string           $name
152
     */
153 2
    protected function createEventDispatcherDecorator(ContainerBuilder $container, string $name)
154
    {
155 2
        $class = $container->getParameter('mrtn_json_api.http_client.decorator.event_dispatcher.class');
156
157 2
        $decorator = new Definition($class, [
158 2
            new Reference('mrtn_json_api.http_client'),
159 2
            new Reference('event_dispatcher'),
160 2
            sprintf('mrtn_json_api.resource_client.%s.request', $name),
161 2
            sprintf('mrtn_json_api.resource_client.%s.response', $name),
162 2
            sprintf('mrtn_json_api.resource_client.%s.exception', $name)
163
        ]);
164
165 2
        $decorator->setPublic(false);
166
167 2
        $container->setDefinition('mrtn_json_api.http_client.decorator.event_dispatcher.' . $name, $decorator);
168 2
    }
169
170
    /**
171
     * Create definition of routes for a route repository by a collection of endpoints
172
     *
173
     * @param  array $resources
174
     * @return array
175
     */
176 2
    protected function createRoutesDefinition(array $resources): array
177
    {
178 2
        $definition = [];
179
180 2
        foreach ($resources as $name => $resource)
181
        {
182 2
            $methods = array_keys($resource['methods']);
183 2
            $methods = array_map('strtoupper', $methods);
184
185 2
            $definition[$name] = [
186 2
                'path'    => trim($resource['path']),
187 2
                'methods' => array_map('trim', $methods)
188
            ];
189
        }
190
191 2
        return $definition;
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     */
197 1
    public function getAlias()
198
    {
199 1
        return self::ALIAS;
200
    }
201
}