Completed
Push — master ( d8d25b...3f99c9 )
by Quentin
59s
created

LoaderCompilerPass::process()   F

Complexity

Conditions 16
Paths 482

Size

Total Lines 78
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 210.3806

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 78
ccs 5
cts 57
cp 0.0877
rs 3.4172
cc 16
eloc 43
nc 482
nop 1
crap 210.3806

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Majora\Bundle\FrameworkExtraBundle\DependencyInjection\Compiler;
4
5
use Majora\Framework\Loader\Bridge\Doctrine\AbstractDoctrineLoader;
6
use Majora\Framework\Loader\LoaderInterface;
7
use Majora\Framework\Model\CollectionableInterface;
8
use Majora\Framework\Model\LazyPropertiesInterface;
9
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\Reference;
12
13
/**
14
 * Compiler pass to register loaders setUp.
15
 */
16
class LoaderCompilerPass implements CompilerPassInterface
17
{
18
    /**
19
     * {@inheritdoc}
20
     *
21
     * Processes "majora.loader" tags
22
     */
23 2
    public function process(ContainerBuilder $container)
24
    {
25 2
        $loaderTags = $container->findTaggedServiceIds('majora.loader');
26
27 2
        foreach ($loaderTags as $loaderId => $tags) {
28
            $loaderDefinition = $container->getDefinition($loaderId);
29
            $loaderReflection = new \ReflectionClass($loaderDefinition->getClass());
30
31
            foreach ($tags as $attributes) {
32
                $method = $loaderReflection->implementsInterface(LoaderInterface::class) ?
33
                    'configureMetadata' : ($loaderReflection->hasMethod('setUp') ?
34
                        'setUp' : ''
35
                    )
36
                ;
37
                if (isset($attributes['entityClass']) || isset($attributes['entityCollection'])) {
38
                    @trigger_error('"entityClass" and "entityCollection" attributes for tag "majora.loader" are deprecated and will be removed in 2.0. Please "entity" and "collection" instead.', E_USER_DEPRECATED);
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
39
                }
40
                $entityClass = isset($attributes['entity']) ? $attributes['entity'] : $attributes['entityClass'];
41
                $collectionClass = isset($attributes['collection']) ? $attributes['collection'] : $attributes['entityCollection'];
42
                $entityReflection = new \ReflectionClass($entityClass);
43
44
                // configureMetadata() call configuration
45
                if ($method) {
46
                    if (!$entityReflection->implementsInterface(CollectionableInterface::class)) {
47
                        throw new \InvalidArgumentException(sprintf(
48
                            'Cannot support "%s" class into "%s" : managed items have to be %s.',
49
                            $entityClass,
50
                            $loaderDefinition->getClass(),
51
                            CollectionableInterface::class
52
                        ));
53
                    }
54
                    $arguments = array(
55
                        $entityClass,
56
                        array_map(
57
                            function ($property) { return $property->getName(); },
58
                            $entityReflection->getProperties()
59
                        ),
60
                        $collectionClass,
61
                    );
62
63
                    $loaderDefinition->addMethodCall($method, $arguments);
64
                }
65
66
                // Doctrine case
67
                if ($loaderReflection->isSubclassOf(AbstractDoctrineLoader::class)) {
68
69
                    // "repository" attribute key only supported for doctrine loaders
70
                    // Repository is injected through mutator to avoid circular references
71
                    // with Doctrine events and connection
72
                    if (isset($attributes['repository'])) {
73
                        $loaderDefinition->addMethodCall(
74
                            'setEntityRepository',
75
                            array(new Reference($attributes['repository']))
76
                        );
77
                    }
78
79
                    // for Doctrine, loaders cannot self enable objects lazy loading
80
                    // due to general event trigger into all listener for each entites
81
                    // so we have to check class / attribute and register service into event proxy
82
                    if ($container->hasDefinition('majora.doctrine.event_proxy') && !empty($attributes['lazy'])) {
83
                        if (!$entityReflection->implementsInterface(LazyPropertiesInterface::class)) {
84
                            throw new \InvalidArgumentException(sprintf(
85
                                'Class %s has to implement %s to be able to lazy load her properties.',
86
                                $entityClass,
87
                                LazyPropertiesInterface::class
88
                            ));
89
                        }
90
                        $container->getDefinition('majora.doctrine.event_proxy')
91
                            ->addMethodCall('registerDoctrineLazyLoader', array(
92
                                $entityClass,
93
                                new Reference($loaderId),
94
                            ))
95
                        ;
96
                    }
97
                }
98
            }
99 1
        }
100 2
    }
101
}
102