Completed
Push — assert-equals-canonicalize ( 968a1f...79e195 )
by
unknown
29:44 queued 09:39
created

ExternalStorageRegistryPass::process()   C

Complexity

Conditions 12
Paths 30

Size

Total Lines 79

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
nc 30
nop 1
dl 0
loc 79
rs 6.0315
c 0
b 0
f 0

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
/**
4
 * File containing the ExternalStorageRegistryPass 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;
10
11
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
12
use Symfony\Component\DependencyInjection\ContainerBuilder;
13
use Symfony\Component\DependencyInjection\Reference;
14
use LogicException;
15
16
/**
17
 * This compiler pass will register eZ Publish external storage handlers and gateways.
18
 */
19
class ExternalStorageRegistryPass implements CompilerPassInterface
20
{
21
    /**
22
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
23
     *
24
     * @throws \LogicException
25
     */
26
    public function process(ContainerBuilder $container)
27
    {
28
        if (!$container->hasDefinition('ezpublish.persistence.external_storage_registry.factory')) {
29
            return;
30
        }
31
32
        $externalStorageRegistryFactoryDefinition = $container->getDefinition(
33
            'ezpublish.persistence.external_storage_registry.factory'
34
        );
35
36
        // Gateways for external storage handlers.
37
        // Alias attribute is the corresponding field type string.
38
        $externalStorageGateways = [];
39
        // Referencing the services by alias (field type string)
40
        foreach ($container->findTaggedServiceIds('ezpublish.fieldType.externalStorageHandler.gateway') as $id => $attributes) {
41
            foreach ($attributes as $attribute) {
42
                if (!isset($attribute['alias'])) {
43
                    throw new LogicException('ezpublish.fieldType.externalStorageHandler.gateway service tag needs an "alias" attribute to identify the field type. None given.');
44
                }
45
46
                if (!isset($attribute['identifier'])) {
47
                    throw new LogicException('ezpublish.fieldType.externalStorageHandler.gateway service tag needs an "identifier" attribute to identify the gateway. None given.');
48
                }
49
50
                $externalStorageGateways[$attribute['alias']] = [
51
                    'id' => $id,
52
                    'identifier' => $attribute['identifier'],
53
                ];
54
            }
55
        }
56
57
        // External storage handlers for field types that need them.
58
        // Alias attribute is the field type string.
59
        foreach ($container->findTaggedServiceIds('ezpublish.fieldType.externalStorageHandler') as $id => $attributes) {
60
            foreach ($attributes as $attribute) {
61
                if (!isset($attribute['alias'])) {
62
                    throw new LogicException('ezpublish.fieldType.externalStorageHandler service tag needs an "alias" attribute to identify the field type. None given.');
63
                }
64
65
                // If the storage handler is gateway based, then we need to add a corresponding gateway to it.
66
                // Will throw a LogicException if no gateway is defined for this field type.
67
                $storageHandlerDef = $container->findDefinition($id);
68
                $storageHandlerClass = $storageHandlerDef->getClass();
69
                if (preg_match('/^%([^%\s]+)%$/', $storageHandlerClass, $match)) {
70
                    $storageHandlerClass = $container->getParameter($match[1]);
71
                }
72
73
                if (
74
                    is_subclass_of(
75
                        $storageHandlerClass,
76
                        'eZ\\Publish\\Core\\FieldType\\GatewayBasedStorage'
77
                    )
78
                ) {
79
                    if (!isset($externalStorageGateways[$attribute['alias']])) {
80
                        throw new LogicException(
81
                            "External storage handler '$id' for field type {$attribute['alias']} needs a storage gateway but none was given.
82
                        Consider defining a storage gateway as a service for this field type and add the 'ezpublish.fieldType.externalStorageHandler.gateway tag'"
83
                        );
84
                    }
85
86
                    $storageHandlerDef->addMethodCall(
87
                        'addGateway',
88
                        [
89
                            $externalStorageGateways[$attribute['alias']]['identifier'],
90
                            new Reference($externalStorageGateways[$attribute['alias']]['id']),
91
                        ]
92
                    );
93
                }
94
95
                $externalStorageRegistryFactoryDefinition->addMethodCall(
96
                    'registerExternalStorageHandler',
97
                    [
98
                        $id,
99
                        $attribute['alias'],
100
                    ]
101
                );
102
            }
103
        }
104
    }
105
}
106