Completed
Push — ezp_30745 ( 35083c )
by
unknown
19:23
created

ExternalStorageRegistryPass::process()   D

Complexity

Conditions 14
Paths 113

Size

Total Lines 142

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 142
c 0
b 0
f 0
cc 14
nc 113
nop 1
rs 4.9266

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
    public const EXTERNAL_STORAGE_HANDLER_SERVICE_TAG = 'ezplatform.field_type.external_storage_handler';
22
    public const EXTERNAL_STORAGE_HANDLER_GATEWAY_SERVICE_TAG = 'ezplatform.field_type.external_storage_handler.gateway';
23
24
    public const DEPRECATED_EXTERNAL_STORAGE_HANDLER_SERVICE_TAG = 'ezpublish.fieldType.externalStorageHandler';
25
    public const DEPRECATED_EXTERNAL_STORAGE_HANDLER_GATEWAY_SERVICE_TAG = 'ezpublish.fieldType.externalStorageHandler.gateway';
26
27
    /**
28
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
29
     *
30
     * @throws \LogicException
31
     */
32
    public function process(ContainerBuilder $container)
33
    {
34
        if (!$container->hasDefinition('ezpublish.persistence.external_storage_registry.factory')) {
35
            return;
36
        }
37
38
        $externalStorageRegistryFactoryDefinition = $container->getDefinition(
39
            'ezpublish.persistence.external_storage_registry.factory'
40
        );
41
42
        // Gateways for external storage handlers.
43
        // Alias attribute is the corresponding field type string.
44
        $externalStorageGateways = [];
45
46
        $deprecatedExternalStorageHandlerGatewayTags = $container->findTaggedServiceIds(
47
            self::DEPRECATED_EXTERNAL_STORAGE_HANDLER_GATEWAY_SERVICE_TAG
48
        );
49
50
        foreach ($deprecatedExternalStorageHandlerGatewayTags as $deprecatedExternalStorageHandlerGatewayTag) {
51
            @trigger_error(
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...
52
                sprintf(
53
                    '`%s` service tag is deprecated and will be removed in eZ Platform 4.0. Please use `%s` instead.',
54
                    self::DEPRECATED_EXTERNAL_STORAGE_HANDLER_GATEWAY_SERVICE_TAG,
55
                    self::EXTERNAL_STORAGE_HANDLER_GATEWAY_SERVICE_TAG
56
                ),
57
                E_USER_DEPRECATED
58
            );
59
        }
60
61
        $externalStorageHandlerGatewayTags = array_merge(
62
            $deprecatedExternalStorageHandlerGatewayTags,
63
            $container->findTaggedServiceIds(
64
                self::EXTERNAL_STORAGE_HANDLER_GATEWAY_SERVICE_TAG
65
            )
66
        );
67
68
        // Referencing the services by alias (field type string)
69
        foreach ($externalStorageHandlerGatewayTags as $id => $attributes) {
70
            foreach ($attributes as $attribute) {
71
                if (!isset($attribute['alias'])) {
72
                    throw new LogicException(
73
                        sprintf(
74
                            '%s or %s service tag needs an "alias" attribute to identify the field type. None given.',
75
                            self::DEPRECATED_EXTERNAL_STORAGE_HANDLER_GATEWAY_SERVICE_TAG,
76
                            self::EXTERNAL_STORAGE_HANDLER_GATEWAY_SERVICE_TAG
77
                        )
78
                    );
79
                }
80
81
                if (!isset($attribute['identifier'])) {
82
                    throw new LogicException(
83
                        sprintf(
84
                            '%s or %s service tag needs an "identifier" attribute to identify the gateway. None given.',
85
                            self::DEPRECATED_EXTERNAL_STORAGE_HANDLER_GATEWAY_SERVICE_TAG,
86
                            self::EXTERNAL_STORAGE_HANDLER_GATEWAY_SERVICE_TAG
87
                        )
88
                    );
89
                }
90
91
                $externalStorageGateways[$attribute['alias']] = [
92
                    'id' => $id,
93
                    'identifier' => $attribute['identifier'],
94
                ];
95
            }
96
        }
97
98
        $deprecatedExternalStorageHandlerTags = $container->findTaggedServiceIds(
99
            self::DEPRECATED_EXTERNAL_STORAGE_HANDLER_SERVICE_TAG
100
        );
101
102
        foreach ($deprecatedExternalStorageHandlerTags as $deprecatedExternalStorageHandlerTag) {
103
            @trigger_error(
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...
104
                sprintf(
105
                    '`%s` service tag is deprecated and will be removed in eZ Platform 4.0. Please use `%s` instead.',
106
                    self::DEPRECATED_EXTERNAL_STORAGE_HANDLER_SERVICE_TAG,
107
                    self::EXTERNAL_STORAGE_HANDLER_SERVICE_TAG
108
                ),
109
                E_USER_DEPRECATED
110
            );
111
        }
112
113
        $externalStorageHandlerTags = array_merge(
114
            $deprecatedExternalStorageHandlerTags,
115
            $container->findTaggedServiceIds(
116
                self::EXTERNAL_STORAGE_HANDLER_SERVICE_TAG
117
            )
118
        );
119
120
        // External storage handlers for field types that need them.
121
        // Alias attribute is the field type string.
122
        foreach ($externalStorageHandlerTags as $id => $attributes) {
123
            foreach ($attributes as $attribute) {
124
                if (!isset($attribute['alias'])) {
125
                    throw new LogicException(
126
                        sprintf(
127
                            '%s or %s service tag needs an "alias" attribute to identify the field type. None given.',
128
                            self::DEPRECATED_EXTERNAL_STORAGE_HANDLER_SERVICE_TAG,
129
                            self::EXTERNAL_STORAGE_HANDLER_SERVICE_TAG
130
                        )
131
                    );
132
                }
133
134
                // If the storage handler is gateway based, then we need to add a corresponding gateway to it.
135
                // Will throw a LogicException if no gateway is defined for this field type.
136
                $storageHandlerDef = $container->findDefinition($id);
137
                $storageHandlerClass = $storageHandlerDef->getClass();
138
                if (preg_match('/^%([^%\s]+)%$/', $storageHandlerClass, $match)) {
139
                    $storageHandlerClass = $container->getParameter($match[1]);
140
                }
141
142
                if (
143
                    is_subclass_of(
144
                        $storageHandlerClass,
145
                        'eZ\\Publish\\Core\\FieldType\\GatewayBasedStorage'
146
                    )
147
                ) {
148
                    if (!isset($externalStorageGateways[$attribute['alias']])) {
149
                        throw new LogicException(
150
                            "External storage handler '$id' for field type {$attribute['alias']} needs a storage gateway but none was given.
151
                            Consider defining a storage gateway as a service for this field type and add the 'ezplatform.field_type.external_storage_handler.gateway tag'"
152
                        );
153
                    }
154
155
                    $storageHandlerDef->addMethodCall(
156
                        'addGateway',
157
                        [
158
                            $externalStorageGateways[$attribute['alias']]['identifier'],
159
                            new Reference($externalStorageGateways[$attribute['alias']]['id']),
160
                        ]
161
                    );
162
                }
163
164
                $externalStorageRegistryFactoryDefinition->addMethodCall(
165
                    'registerExternalStorageHandler',
166
                    [
167
                        $id,
168
                        $attribute['alias'],
169
                    ]
170
                );
171
            }
172
        }
173
    }
174
}
175