Completed
Push — EZP-29891 ( 916cf6...0402ff )
by
unknown
16:53
created

FieldValueConverterRegistryPass::process()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 37

Duplication

Lines 28
Ratio 75.68 %

Importance

Changes 0
Metric Value
cc 9
nc 8
nop 1
dl 28
loc 37
rs 7.7724
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the FieldValueConverterRegistryPass class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Base\Container\Compiler\Storage\Legacy;
10
11
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
12
use Symfony\Component\DependencyInjection\ContainerBuilder;
13
use Symfony\Component\DependencyInjection\Exception\LogicException;
14
use Symfony\Component\DependencyInjection\Reference;
15
16
/**
17
 * This compiler pass will register Legacy Storage field value converters.
18
 */
19
class FieldValueConverterRegistryPass implements CompilerPassInterface
20
{
21
    /**
22
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
23
     */
24
    public function process(ContainerBuilder $container)
25
    {
26
        if (!$container->hasDefinition('ezpublish.persistence.legacy.field_value_converter.registry')) {
27
            return;
28
        }
29
30
        $registry = $container->getDefinition('ezpublish.persistence.legacy.field_value_converter.registry');
31
32 View Code Duplication
        foreach ($container->findTaggedServiceIds('ezpublish.storageEngine.legacy.converter') as $id => $attributes) {
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...
33
            foreach ($attributes as $attribute) {
34
                if (!isset($attribute['alias'])) {
35
                    throw new LogicException('ezpublish.storageEngine.legacy.converter service tag needs an "alias" attribute to identify the field type. None given.');
36
                }
37
38
                if (isset($attribute['lazy']) && $attribute['lazy'] === true) {
39
                    if (!isset($attribute['callback'])) {
40
                        throw new LogicException("Converter service '$id' is marked as lazy but no callback is provided! Please provide a callback.");
41
                    }
42
43
                    $converter = $attribute['callback'];
44
                    if (strpos($converter, '::') === 0) {
45
                        $converter = $container->getDefinition($id)->getClass() . $converter;
46
                    }
47
                } else {
48
                    $converter = new Reference($id);
49
                }
50
51
                $registry->addMethodCall(
52
                    'register',
53
                    array(
54
                        $attribute['alias'],
55
                        $converter,
56
                    )
57
                );
58
            }
59
        }
60
    }
61
}
62