Passed
Push — master ( 3863d1...041130 )
by Michael
07:19
created

ObjectMapperCompilerPass   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 95.45%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 0
cbo 3
dl 0
loc 53
ccs 21
cts 22
cp 0.9545
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 5 1
A processMappingHandlers() 0 11 2
A processLinkRepositories() 0 19 4
1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Bundle\JsonApiBundle\DependencyInjection\Compiler;
5
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
class ObjectMapperCompilerPass implements CompilerPassInterface
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15 1
    public function process(ContainerBuilder $container)
16
    {
17 1
        $this->processMappingHandlers($container);
18 1
        $this->processLinkRepositories($container);
19 1
    }
20
21
    /**
22
     * Process mapping handlers
23
     *
24
     * @param ContainerBuilder $container
25
     */
26 1
    protected function processMappingHandlers(ContainerBuilder $container)
27
    {
28 1
        $definition = $container->findDefinition('mrtn_json_api.object_mapper');
29 1
        $extensions = $container->findTaggedServiceIds('mrtn_json_api.object_mapper.handler');
30
31 1
        foreach ($extensions as $id => $tags) {
32 1
            $definition->addMethodCall('addHandler', [
33 1
                new Reference($id)
34
            ]);
35
        }
36 1
    }
37
38
    /**
39
     * Process links repositories
40
     *
41
     * @param ContainerBuilder $container
42
     */
43 1
    protected function processLinkRepositories(ContainerBuilder $container)
44
    {
45 1
        $definition   = $container->findDefinition('mrtn_json_api.object_mapper.link_repository_provider');
46 1
        $repositories = $container->findTaggedServiceIds('mrtn_json_api.object_mapper.link_repository');
47
48 1
        foreach ($repositories as $id => $tags) {
49 1
            foreach ($tags as $tag)
50
            {
51 1
                if (! isset($tag['alias'])) {
52
                    throw new \LogicException('Alias must be defined for a "link-repository" tag');
53
                }
54
55 1
                $definition->addMethodCall('registerRepository', [
56 1
                    trim($tag['alias']),
57 1
                    new Reference($id)
58
                ]);
59
            }
60
        }
61
    }
62
}