Completed
Push — feature-20rc1 ( 008ae2 )
by Rob
16:55
created

AwsS3ResolverFactory::addConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 54
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 9.6716
c 0
b 0
f 0
cc 1
eloc 52
nc 1
nop 1

How to fix   Long Method   

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
 * This file is part of the `liip/LiipImagineBundle` project.
5
 *
6
 * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Liip\ImagineBundle\DependencyInjection\Factory\Resolver;
13
14
use Aws\S3\S3Client;
15
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Definition;
18
use Symfony\Component\DependencyInjection\Reference;
19
20
class AwsS3ResolverFactory extends AbstractResolverFactory
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function create(ContainerBuilder $container, string $name, array $config): string
26
    {
27
        $clientRef = $this->createReference($name, 'client');
28
        $clientDef = (new Definition(S3Client::class))
29
            ->setFactory([S3Client::class, 'factory'])
30
            ->addArgument($this->createClientConfigArguments($config['client_config']));
31
32
        $container->setDefinition($clientRef, $clientDef);
33
34
        $resolverKey = $this->getFactoryServiceName($name, null);
35
        $resolverDef = $this->createChildDefinition();
36
        $resolverDef->replaceArgument(0, $clientRef);
37
        $resolverDef->replaceArgument(1, $config['bucket']);
38
        $resolverDef->replaceArgument(2, $config['acl']);
39
        $resolverDef->replaceArgument(3, $config['get_options']);
40
        $resolverDef->replaceArgument(4, $config['put_options']);
41
        $resolverDef->replaceArgument(5, $config['del_options']);
42
43
        if (isset($config['cache_prefix'])) {
44
            $resolverDef->replaceArgument(6, $config['cache_prefix']);
45
        }
46
47
        $container->setDefinition($resolverKey, $resolverDef);
48
49
        if ($config['proxies']) {
50
            $proxyRef = $this->createReference($name, 'proxy');
51
            $container->setDefinition($proxyRef, $resolverDef);
52
53
            $proxyDef = $this->createChildDefinition('proxy');
54
            $proxyDef->replaceArgument(0, $proxyRef);
55
            $proxyDef->replaceArgument(1, $config['proxies']);
56
            $container->setDefinition($resolverKey, $proxyDef);
57
        }
58
59
        if ($config['cache']) {
60
            $cacheRef = $this->createReference($name, 'cache');
61
            $container->setDefinition($cacheRef, $container->getDefinition($resolverKey));
62
63
            $cacheDef = $this->createChildDefinition('cache');
64
            $cacheDef->replaceArgument(0, new Reference($config['cache']));
65
            $cacheDef->replaceArgument(1, new Reference($cacheRef));
66
            $container->setDefinition($resolverKey, $cacheDef);
67
        }
68
69
        $container->getDefinition($resolverKey)->addTag('liip_imagine.cache.resolver', [
70
            'resolver' => $name,
71
        ]);
72
73
        return $resolverKey;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function addConfiguration(ArrayNodeDefinition $builder): void
80
    {
81
        $builder
82
            ->children()
83
                ->scalarNode('bucket')
84
                    ->isRequired()
85
                    ->cannotBeEmpty()
86
                ->end()
87
                ->arrayNode('client_config')
88
                    ->isRequired()
89
                    ->children()
90
                        ->arrayNode('credentials')
91
                            ->variablePrototype()
92
                                ->isRequired()
93
                            ->end()
94
                        ->end()
95
                        ->scalarNode('version')
96
                            ->defaultValue('2016-03-01')
97
                        ->end()
98
                        ->scalarNode('region')->end()
99
                        ->arrayNode('extras')
100
                            ->arrayPrototype()->end()
101
                        ->end()
102
                    ->end()
103
                ->end()
104
                ->scalarNode('cache')
105
                    ->defaultValue(false)
106
                ->end()
107
                ->scalarNode('acl')
108
                    ->defaultValue('public-read')
109
                    ->cannotBeEmpty()
110
                ->end()
111
                ->scalarNode('cache_prefix')
112
                    ->defaultValue(null)
113
                ->end()
114
                ->arrayNode('get_options')
115
                    ->useAttributeAsKey('key')
116
                    ->prototype('scalar')->end()
117
                ->end()
118
                ->arrayNode('put_options')
119
                    ->useAttributeAsKey('key')
120
                    ->prototype('scalar')->end()
121
                ->end()
122
                ->arrayNode('del_options')
123
                    ->useAttributeAsKey('key')
124
                    ->prototype('scalar')->end()
125
                ->end()
126
                ->arrayNode('proxies')
127
                    ->defaultValue([])
128
                    ->useAttributeAsKey('name')
129
                    ->prototype('scalar')->end()
130
                ->end()
131
            ->end();
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function getName(): string
138
    {
139
        return 'aws_s3';
140
    }
141
142
    /**
143
     * @param array $config
144
     *
145
     * @return array
146
     */
147
    private function createClientConfigArguments(array $config): array
148
    {
149
        $arguments = [
150
            'credentials' => $config['credentials'],
151
        ];
152
153
        if (isset($config['version'])) {
154
            $arguments['version'] = $config['version'];
155
        }
156
157
        if (isset($config['region'])) {
158
            $arguments['region'] = $config['region'];
159
        }
160
161
        if (isset($config['extras']) && 0 < count($config['extras'])) {
162
            $arguments = array_merge($arguments, $config['extras']);
163
        }
164
165
        return $arguments;
166
    }
167
}
168