Completed
Push — master ( 0a7932...aee286 )
by Maksim
13s
created

AwsS3ResolverFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 110
Duplicated Lines 20 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 10
Bugs 3 Features 3
Metric Value
wmc 7
c 10
b 3
f 3
lcom 0
cbo 8
dl 22
loc 110
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
B create() 22 57 5
B addConfiguration() 0 35 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Liip\ImagineBundle\DependencyInjection\Factory\Resolver;
4
5
use Symfony\Component\DependencyInjection\ContainerBuilder;
6
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
7
use Symfony\Component\DependencyInjection\Definition;
8
use Symfony\Component\DependencyInjection\DefinitionDecorator;
9
use Symfony\Component\DependencyInjection\Reference;
10
11
class AwsS3ResolverFactory implements ResolverFactoryInterface
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function create(ContainerBuilder $container, $resolverName, array $config)
17
    {
18
        $awsS3ClientId = 'liip_imagine.cache.resolver.'.$resolverName.'.client';
19
        $awsS3ClientDefinition = new Definition('Aws\S3\S3Client');
20
        if (method_exists($awsS3ClientDefinition, 'setFactory')) {
21
            $awsS3ClientDefinition->setFactory(array('Aws\S3\S3Client', 'factory'));
22
        } else {
23
            // to be removed when dependency on Symfony DependencyInjection is bumped to 2.6
24
            $awsS3ClientDefinition->setFactoryClass('Aws\S3\S3Client');
0 ignored issues
show
Bug introduced by
The method setFactoryClass() does not exist on Symfony\Component\DependencyInjection\Definition. Did you maybe mean setFactory()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
25
            $awsS3ClientDefinition->setFactoryMethod('factory');
0 ignored issues
show
Bug introduced by
The method setFactoryMethod() does not exist on Symfony\Component\DependencyInjection\Definition. Did you maybe mean setFactory()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
26
        }
27
        $awsS3ClientDefinition->addArgument($config['client_config']);
28
        $container->setDefinition($awsS3ClientId, $awsS3ClientDefinition);
29
30
        $resolverDefinition = new DefinitionDecorator('liip_imagine.cache.resolver.prototype.aws_s3');
31
        $resolverDefinition->replaceArgument(0, new Reference($awsS3ClientId));
32
        $resolverDefinition->replaceArgument(1, $config['bucket']);
33
        $resolverDefinition->replaceArgument(2, $config['acl']);
34
        $resolverDefinition->replaceArgument(3, array_replace($config['url_options'], $config['get_options']));
35
        $resolverDefinition->replaceArgument(4, $config['put_options']);
36
        $resolverId = 'liip_imagine.cache.resolver.'.$resolverName;
37
        $container->setDefinition($resolverId, $resolverDefinition);
38
39
        if (isset($config['cache_prefix'])) {
40
            $resolverDefinition->addMethodCall('setCachePrefix', array($config['cache_prefix']));
41
        }
42
43 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...
44
            $proxiedResolverId = 'liip_imagine.cache.resolver.'.$resolverName.'.proxied';
45
46
            $container->setDefinition($proxiedResolverId, $resolverDefinition);
47
48
            $proxyResolverDefinition = new DefinitionDecorator('liip_imagine.cache.resolver.prototype.proxy');
49
            $proxyResolverDefinition->replaceArgument(0, new Reference($proxiedResolverId));
50
            $proxyResolverDefinition->replaceArgument(1, $config['proxies']);
51
52
            $container->setDefinition($resolverId, $proxyResolverDefinition);
53
        }
54
55 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...
56
            $cachedResolverId = 'liip_imagine.cache.resolver.'.$resolverName.'.cached';
57
58
            $container->setDefinition($cachedResolverId, $container->getDefinition($resolverId));
59
60
            $cacheResolverDefinition = new DefinitionDecorator('liip_imagine.cache.resolver.prototype.cache');
61
            $cacheResolverDefinition->replaceArgument(0, new Reference($config['cache']));
62
            $cacheResolverDefinition->replaceArgument(1, new Reference($cachedResolverId));
63
64
            $container->setDefinition($resolverId, $cacheResolverDefinition);
65
        }
66
67
        $container->getDefinition($resolverId)->addTag('liip_imagine.cache.resolver', array(
68
            'resolver' => $resolverName,
69
        ));
70
71
        return $resolverId;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function getName()
78
    {
79
        return 'aws_s3';
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function addConfiguration(ArrayNodeDefinition $builder)
86
    {
87
        $builder
88
            ->children()
89
                ->scalarNode('bucket')->isRequired()->cannotBeEmpty()->end()
90
                ->scalarNode('cache')->defaultValue(false)->end()
91
                ->scalarNode('acl')->defaultValue('public-read')->cannotBeEmpty()->end()
92
                ->scalarNode('cache_prefix')->defaultValue(null)->end()
93
                ->arrayNode('client_config')
94
                    ->isRequired()
95
                    ->prototype('variable')
96
                        ->treatNullLike(array())
97
                    ->end()
98
                ->end()
99
                /* @deprecated Use `get_options` instead */
100
                ->arrayNode('url_options')
101
                    ->useAttributeAsKey('key')
102
                    ->prototype('scalar')->end()
103
                ->end()
104
                ->arrayNode('get_options')
105
                    ->useAttributeAsKey('key')
106
                    ->prototype('scalar')->end()
107
                ->end()
108
                ->arrayNode('put_options')
109
                    ->useAttributeAsKey('key')
110
                    ->prototype('scalar')->end()
111
                ->end()
112
                ->arrayNode('proxies')
113
                    ->defaultValue(array())
114
                    ->useAttributeAsKey('name')
115
                    ->prototype('scalar')->end()
116
                ->end()
117
            ->end()
118
        ;
119
    }
120
}
121