Completed
Push — master ( 2017d8...712de9 )
by Keith
02:20
created

UecodeQPushExtension::createAwsClient()   C

Complexity

Conditions 9
Paths 6

Size

Total Lines 51
Code Lines 30

Duplication

Lines 10
Ratio 19.61 %

Code Coverage

Tests 24
CRAP Score 11.0605

Importance

Changes 6
Bugs 0 Features 3
Metric Value
c 6
b 0
f 3
dl 10
loc 51
ccs 24
cts 34
cp 0.7059
rs 6.2727
cc 9
eloc 30
nc 6
nop 3
crap 11.0605

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
 * Copyright 2014 Underground Elephant
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 *
18
 * @package     qpush-bundle
19
 * @copyright   Underground Elephant 2014
20
 * @license     Apache License, Version 2.0
21
 */
22
23
namespace Uecode\Bundle\QPushBundle\DependencyInjection;
24
25
use Uecode\Bundle\QPushBundle\DependencyInjection\Configuration;
26
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
27
use Symfony\Component\DependencyInjection\Definition;
28
use Symfony\Component\DependencyInjection\Reference;
29
use Symfony\Component\DependencyInjection\ContainerBuilder;
30
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
31
use Symfony\Component\Config\FileLocator;
32
33
/**
34
 * @author Keith Kirk <[email protected]>
35
 */
36
class UecodeQPushExtension extends Extension
37
{
38 1
    public function load(array $configs, ContainerBuilder $container)
39
    {
40 1
        $configuration = new Configuration();
41 1
        $config = $this->processConfiguration($configuration, $configs);
42
43 1
        $loader = new YamlFileLoader(
44 1
            $container,
45 1
            new FileLocator(__DIR__.'/../Resources/config')
46 1
        );
47
48 1
        $loader->load('parameters.yml');
49 1
        $loader->load('services.yml');
50
51 1
        $registry = $container->getDefinition('uecode_qpush.registry');
52 1
        $cache    = $config['cache_service'] ?: 'uecode_qpush.file_cache';
53
54 1
        foreach ($config['queues'] as $queue => $values) {
55
56
            // Adds logging property to queue options
57 1
            $values['options']['logging_enabled'] = $config['logging_enabled'];
58
59 1
            $provider   = $values['provider'];
60 1
            $class      = null;
61 1
            $client     = null;
62
63 1
            switch ($config['providers'][$provider]['driver']) {
64 1
                case 'aws':
65 1
                    $class  = $container->getParameter('uecode_qpush.provider.aws');
66 1
                    $client = $this->createAwsClient(
67 1
                        $config['providers'][$provider],
68 1
                        $container,
69
                        $provider
70 1
                    );
71 1
                    break;
72 1
                case 'ironmq':
73 1
                    $class  = $container->getParameter('uecode_qpush.provider.ironmq');
74 1
                    $client = $this->createIronMQClient(
75 1
                        $config['providers'][$provider],
76 1
                        $container,
77
                        $provider
78 1
                    );
79 1
                    break;
80 1
                case 'sync':
81
                    $class  = $container->getParameter('uecode_qpush.provider.sync');
82
                    $client = $this->createSyncClient();
83
                    break;
84 1
                case 'custom':
85
                    $class  = $container->getParameter('uecode_qpush.provider.custom');
86
                    $client = $this->createCustomClient($config['providers'][$provider]['service']);
87
                    break;
88 1
                case 'file':
89 1
                    $class = $container->getParameter('uecode_qpush.provider.file');
90 1
                    $values['options']['path'] = $config['providers'][$provider]['path'];
91 1
                    break;
92 1
            }
93
94 1
            $definition = new Definition(
95 1
                $class, [$queue, $values['options'], $client, new Reference($cache), new Reference('logger')]
96 1
            );
97
98 1
            $name = sprintf('uecode_qpush.%s', $queue);
99
100 1
            $container->setDefinition($name, $definition)
101 1
                ->addTag('monolog.logger', ['channel' => 'qpush'])
102 1
                ->addTag(
103 1
                    'uecode_qpush.event_listener',
104
                    [
105 1
                        'event' => "{$queue}.on_notification",
106 1
                        'method' => "onNotification",
107
                        'priority' => 255
108 1
                    ]
109 1
                )
110 1
                ->addTag(
111 1
                    'uecode_qpush.event_listener',
112
                    [
113 1
                        'event' => "{$queue}.message_received",
114 1
                        'method' => "onMessageReceived",
115
                        'priority' => -255
116 1
                    ]
117 1
                )
118
            ;
119
120 1
            $registry->addMethodCall('addProvider', [$queue, new Reference($name)]);
121 1
        }
122 1
    }
123
124
    /**
125
     * Creates a definition for the AWS provider
126
     *
127
     * @param array            $config    A Configuration array for the client
128
     * @param ContainerBuilder $container The container
129
     * @param string           $name      The provider key
130
     *
131
     * @return Reference
132
     */
133 1
    private function createAwsClient($config, ContainerBuilder $container, $name)
134
    {
135 1
        $service = sprintf('uecode_qpush.provider.%s', $name);
136
137 1
        if (!$container->hasDefinition($service)) {
138
139 1
            $aws2 = class_exists('Aws\Common\Aws');
140 1
            $aws3 = class_exists('Aws\Sdk');
141 1
            if (!$aws2 && !$aws3) {
142
                throw new \RuntimeException(
143
                    'You must require "aws/aws-sdk-php" to use the AWS provider.'
144
                );
145
            }
146
147
            $awsConfig = [
148 1
                'region' => $config['region']
149 1
            ];
150
151 1
            $aws = new Definition('Aws\Common\Aws');
152 1
            $aws->setFactory(['Aws\Common\Aws', 'factory']);
153 1
            $aws->setArguments([$awsConfig]);
154
155 1
            if ($aws2) {
156 1
                $aws = new Definition('Aws\Common\Aws');
157 1
                $aws->setFactory(['Aws\Common\Aws', 'factory']);
158
159 1 View Code Duplication
                if (!empty($config['key']) && !empty($config['secret'])) {
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...
160 1
                    $awsConfig['key']    = $config['key'];
161 1
                    $awsConfig['secret'] = $config['secret'];
162 1
                }
163
164 1
            } else {
165
                $aws = new Definition('Aws\Sdk');
166
167 View Code Duplication
                if (!empty($config['key']) && !empty($config['secret'])) {
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...
168
                    $awsConfig['credentials'] = [
169
                        'key'    => $config['key'],
170
                        'secret' => $config['secret']
171
                    ];
172
                }
173
                $awsConfig['version']  = 'latest';
174
            }
175
176 1
            $aws->setArguments([$awsConfig]);
177
178 1
            $container->setDefinition($service, $aws)
179 1
                ->setPublic(false);
180 1
        }
181
182 1
        return new Reference($service);
183
    }
184
185
    /**
186
     * Creates a definition for the IronMQ provider
187
     *
188
     * @param array            $config    A Configuration array for the provider
189
     * @param ContainerBuilder $container The container
190
     * @param string           $name      The provider key
191
     *
192
     * @return Reference
193
     */
194 1
    private function createIronMQClient($config, ContainerBuilder $container, $name)
195
    {
196 1
        $service = sprintf('uecode_qpush.provider.%s', $name);
197
198 1
        if (!$container->hasDefinition($service)) {
199
200 1
            if (!class_exists('IronMQ\IronMQ')) {
201
                throw new \RuntimeException(
202
                    'You must require "iron-io/iron_mq" to use the Iron MQ provider.'
203
                );
204
            }
205
206 1
            $ironmq = new Definition('IronMQ\IronMQ');
207 1
            $ironmq->setArguments([
208
                [
209 1
                    'token'         => $config['token'],
210 1
                    'project_id'    => $config['project_id'],
211 1
                    'host'          => sprintf('%s.iron.io', $config['host']),
212 1
                    'port'          => $config['port'],
213 1
                    'api_version'   => $config['api_version']
214 1
                ]
215 1
            ]);
216
217 1
            $container->setDefinition($service, $ironmq)
218 1
                ->setPublic(false);
219 1
        }
220
221 1
        return new Reference($service);
222
    }
223
224
    private function createSyncClient()
225
    {
226
        return new Reference('event_dispatcher');
227
    }
228
229
    /**
230
     * @param string $serviceId
231
     *
232
     * @return Reference
233
     */
234
    private function createCustomClient($serviceId)
235
    {
236
        return new Reference($serviceId);
237
    }
238
239
    /**
240
     * Returns the Extension Alias
241
     *
242
     * @return string
243
     */
244 1
    public function getAlias()
245
    {
246 1
        return 'uecode_qpush';
247
    }
248
}
249