AwsS3ResolverFactory::create()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 52

Duplication

Lines 22
Ratio 42.31 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 22
loc 52
rs 9.0472
cc 4
nc 8
nop 3

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 Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Definition;
17
use Symfony\Component\DependencyInjection\Reference;
18
19
class AwsS3ResolverFactory extends AbstractResolverFactory
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function create(ContainerBuilder $container, $resolverName, array $config)
25
    {
26
        $awsS3ClientId = 'liip_imagine.cache.resolver.'.$resolverName.'.client';
27
        $awsS3ClientDefinition = new Definition('Aws\S3\S3Client');
28
        $awsS3ClientDefinition->setFactory(['Aws\S3\S3Client', 'factory']);
29
        $awsS3ClientDefinition->addArgument($config['client_config']);
30
        $container->setDefinition($awsS3ClientId, $awsS3ClientDefinition);
31
32
        $resolverDefinition = $this->getChildResolverDefinition();
33
        $resolverDefinition->replaceArgument(0, new Reference($awsS3ClientId));
34
        $resolverDefinition->replaceArgument(1, $config['bucket']);
35
        $resolverDefinition->replaceArgument(2, $config['acl']);
36
        $resolverDefinition->replaceArgument(3, $config['get_options']);
37
        $resolverDefinition->replaceArgument(4, $config['put_options']);
38
39
        $resolverId = 'liip_imagine.cache.resolver.'.$resolverName;
40
        $container->setDefinition($resolverId, $resolverDefinition);
41
42
        if (isset($config['cache_prefix'])) {
43
            $resolverDefinition->addMethodCall('setCachePrefix', [$config['cache_prefix']]);
44
        }
45
46 View Code Duplication
        if ($config['proxies']) {
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...
47
            $proxiedResolverId = 'liip_imagine.cache.resolver.'.$resolverName.'.proxied';
48
49
            $container->setDefinition($proxiedResolverId, $resolverDefinition);
50
51
            $proxyResolverDefinition = $this->getChildResolverDefinition('proxy');
52
            $proxyResolverDefinition->replaceArgument(0, new Reference($proxiedResolverId));
53
            $proxyResolverDefinition->replaceArgument(1, $config['proxies']);
54
55
            $container->setDefinition($resolverId, $proxyResolverDefinition);
56
        }
57
58 View Code Duplication
        if ($config['cache']) {
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...
59
            $cachedResolverId = 'liip_imagine.cache.resolver.'.$resolverName.'.cached';
60
61
            $container->setDefinition($cachedResolverId, $container->getDefinition($resolverId));
62
63
            $cacheResolverDefinition = $this->getChildResolverDefinition('cache');
64
            $cacheResolverDefinition->replaceArgument(0, new Reference($config['cache']));
65
            $cacheResolverDefinition->replaceArgument(1, new Reference($cachedResolverId));
66
67
            $container->setDefinition($resolverId, $cacheResolverDefinition);
68
        }
69
70
        $container->getDefinition($resolverId)->addTag('liip_imagine.cache.resolver', [
71
            'resolver' => $resolverName,
72
        ]);
73
74
        return $resolverId;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function getName()
81
    {
82
        return 'aws_s3';
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function addConfiguration(ArrayNodeDefinition $builder)
89
    {
90
        $builder
91
            ->children()
92
                ->scalarNode('bucket')
93
                    ->isRequired()
94
                    ->cannotBeEmpty()
95
                ->end()
96
                ->scalarNode('cache')
97
                    ->defaultValue(false)
98
                ->end()
99
                ->scalarNode('acl')
100
                    ->defaultValue('public-read')
101
                    ->cannotBeEmpty()
102
                ->end()
103
                ->scalarNode('cache_prefix')
104
                    ->defaultValue(null)
105
                ->end()
106
                ->arrayNode('client_config')
107
                    ->isRequired()
108
                    ->prototype('variable')
109
                        ->treatNullLike([])
110
                    ->end()
111
                ->end()
112
                ->arrayNode('get_options')
113
                    ->useAttributeAsKey('key')
114
                        ->prototype('scalar')
115
                    ->end()
116
                ->end()
117
                ->arrayNode('put_options')
118
                    ->useAttributeAsKey('key')
119
                        ->prototype('scalar')
120
                    ->end()
121
                ->end()
122
                ->arrayNode('proxies')
123
                    ->defaultValue([])
124
                    ->useAttributeAsKey('name')
125
                        ->prototype('scalar')
126
                    ->end()
127
                ->end()
128
            ->end();
129
    }
130
}
131