Completed
Pull Request — master (#28)
by Jamal
04:53
created

LoaderCompilerPass   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 8.93%

Importance

Changes 4
Bugs 0 Features 3
Metric Value
wmc 17
c 4
b 0
f 3
lcom 0
cbo 3
dl 0
loc 88
ccs 5
cts 56
cp 0.0893
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
F process() 0 80 17
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
        $loaderCollectionDefinition = $container->findDefinition('majora.loader.collection');
26
        $loaderTags = $container->findTaggedServiceIds('majora.loader');
27 2
28
        foreach ($loaderTags as $loaderId => $tags) {
29
            $loaderDefinition = $container->getDefinition($loaderId);
30
            $loaderReflection = new \ReflectionClass($loaderDefinition->getClass());
31
32
            foreach ($tags as $attributes) {
33
                $method = $loaderReflection->implementsInterface(LoaderInterface::class) ?
34
                    'configureMetadata' : ($loaderReflection->hasMethod('setUp') ?
35
                        'setUp' : ''
36
                    )
37
                ;
38
                if (isset($attributes['entityClass']) || isset($attributes['entityCollection'])) {
39
                    @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...
40
                }
41
                $entityClass = isset($attributes['entity']) ? $attributes['entity'] : $attributes['entityClass'];
42
                $collectionClass = isset($attributes['collection']) ? $attributes['collection'] : $attributes['entityCollection'];
43
                $entityReflection = new \ReflectionClass($entityClass);
44
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
                    if (isset($attributes['repository'])) {
64
                        @trigger_error('Repository injection tag "majora.loader" is deprecated and will be removed in 2.0. Please inject it by constructor.', E_USER_DEPRECATED);
0 ignored issues
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...
65
                        $arguments[] = new Reference($attributes['repository']);
66
                    }
67
68
                    $loaderDefinition->addMethodCall($method, $arguments);
69
                }
70
71
                // for doctrine, loaders cannot self enable objects lazy loading
72
                // so we have to check class / attribute and register service into event proxy
73
                if ($container->hasDefinition('majora.doctrine.event_proxy')
74
                    && !empty($attributes['lazy'])
75
                    && $loaderReflection->isSubclassOf(AbstractDoctrineLoader::class)
76
                ) {
77
                    if (!$entityReflection->implementsInterface(LazyPropertiesInterface::class)) {
78
                        throw new \InvalidArgumentException(sprintf(
79
                            'Class %s has to implement %s to be able to lazy load her properties.',
80
                            $entityClass,
81
                            LazyPropertiesInterface::class
82
                        ));
83
                    }
84
                    $container->getDefinition('majora.doctrine.event_proxy')
85
                        ->addMethodCall('registerDoctrineLazyLoader', array(
86
                            $entityClass,
87
                            new Reference($loaderId),
88
                        ))
89
                    ;
90
                }
91 1
92 2
                if (!isset($attributes['alias'])) {
93
                    throw new \InvalidArgumentException(sprintf('Alias not define for "%s" loader.', $loaderId));
94
                }
95
96
                $loaderCollectionDefinition->addMethodCall(
97
                    'add',
98
                    array($attributes['alias'], new Reference($loaderId))
99
                );
100
            }
101
        }
102
    }
103
}
104