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 |
||
35 | class UecodeQPushExtension extends Extension |
||
36 | { |
||
37 | 1 | public function load(array $configs, ContainerBuilder $container) |
|
38 | { |
||
39 | 1 | $configuration = new Configuration(); |
|
40 | 1 | $config = $this->processConfiguration($configuration, $configs); |
|
41 | |||
42 | 1 | $loader = new YamlFileLoader( |
|
43 | $container, |
||
44 | 1 | new FileLocator(__DIR__.'/../Resources/config') |
|
45 | ); |
||
46 | |||
47 | 1 | $loader->load('parameters.yml'); |
|
48 | 1 | $loader->load('services.yml'); |
|
49 | |||
50 | 1 | $registry = $container->getDefinition('uecode_qpush.registry'); |
|
51 | 1 | $cache = $config['cache_service'] ?: 'uecode_qpush.file_cache'; |
|
52 | |||
53 | 1 | foreach ($config['queues'] as $queue => $values) { |
|
54 | |||
55 | // Adds logging property to queue options |
||
56 | 1 | $values['options']['logging_enabled'] = $config['logging_enabled']; |
|
57 | |||
58 | 1 | $provider = $values['provider']; |
|
59 | 1 | $class = null; |
|
60 | 1 | $client = null; |
|
61 | |||
62 | 1 | switch ($config['providers'][$provider]['driver']) { |
|
63 | 1 | case 'aws': |
|
64 | 1 | $class = $container->getParameter('uecode_qpush.provider.aws'); |
|
65 | 1 | $client = $this->createAwsClient( |
|
66 | 1 | $config['providers'][$provider], |
|
67 | $container, |
||
68 | $provider |
||
69 | ); |
||
70 | 1 | break; |
|
71 | 1 | case 'ironmq': |
|
72 | 1 | $class = $container->getParameter('uecode_qpush.provider.ironmq'); |
|
73 | 1 | $client = $this->createIronMQClient( |
|
74 | 1 | $config['providers'][$provider], |
|
75 | $container, |
||
76 | $provider |
||
77 | ); |
||
78 | 1 | break; |
|
79 | 1 | case 'sync': |
|
80 | $class = $container->getParameter('uecode_qpush.provider.sync'); |
||
81 | $client = $this->createSyncClient(); |
||
82 | break; |
||
83 | 1 | case 'custom': |
|
84 | $class = $container->getParameter('uecode_qpush.provider.custom'); |
||
85 | $client = $this->createCustomClient($config['providers'][$provider]['service']); |
||
86 | break; |
||
87 | 1 | case 'file': |
|
88 | 1 | $class = $container->getParameter('uecode_qpush.provider.file'); |
|
89 | 1 | $values['options']['path'] = $config['providers'][$provider]['path']; |
|
90 | 1 | break; |
|
91 | case 'doctrine': |
||
92 | $class = $container->getParameter('uecode_qpush.provider.doctrine'); |
||
93 | $client = $this->createDoctrineClient($config['providers'][$provider]); |
||
94 | break; |
||
95 | } |
||
96 | |||
97 | 1 | $definition = new Definition( |
|
98 | 1 | $class, [$queue, $values['options'], $client, new Reference($cache), new Reference('logger')] |
|
99 | ); |
||
100 | |||
101 | 1 | $definition->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 | 1 | 'priority' => 255 |
|
108 | ] |
||
109 | ) |
||
110 | 1 | ->addTag( |
|
111 | 1 | 'uecode_qpush.event_listener', |
|
112 | [ |
||
113 | 1 | 'event' => "{$queue}.message_received", |
|
114 | 1 | 'method' => "onMessageReceived", |
|
115 | 'priority' => -255 |
||
116 | ] |
||
117 | ); |
||
118 | |||
119 | 1 | if (!empty($values['options']['queue_name']) |
|
120 | 1 | && $config['providers'][$provider]['driver'] == 'aws' |
|
121 | ) { |
||
122 | $definition->addTag( |
||
123 | 'uecode_qpush.event_listener', |
||
124 | [ |
||
125 | 'event' => "{$values['options']['queue_name']}.on_notification", |
||
126 | 'method' => "onNotification", |
||
127 | 'priority' => 255 |
||
128 | ] |
||
129 | ); |
||
130 | } |
||
131 | |||
132 | 1 | $name = sprintf('uecode_qpush.%s', $queue); |
|
133 | 1 | $container->setDefinition($name, $definition); |
|
134 | |||
135 | 1 | $registry->addMethodCall('addProvider', [$queue, new Reference($name)]); |
|
136 | } |
||
137 | 1 | } |
|
138 | |||
139 | /** |
||
140 | * Creates a definition for the AWS provider |
||
141 | * |
||
142 | * @param array $config A Configuration array for the client |
||
143 | * @param ContainerBuilder $container The container |
||
144 | * @param string $name The provider key |
||
145 | * |
||
146 | * @return Reference |
||
147 | */ |
||
148 | 1 | private function createAwsClient($config, ContainerBuilder $container, $name) |
|
199 | |||
200 | /** |
||
201 | * Creates a definition for the IronMQ provider |
||
202 | * |
||
203 | * @param array $config A Configuration array for the provider |
||
204 | * @param ContainerBuilder $container The container |
||
205 | * @param string $name The provider key |
||
206 | * |
||
207 | * @return Reference |
||
208 | */ |
||
209 | 1 | private function createIronMQClient($config, ContainerBuilder $container, $name) |
|
210 | { |
||
211 | 1 | $service = sprintf('uecode_qpush.provider.%s', $name); |
|
212 | |||
213 | 1 | if (!$container->hasDefinition($service)) { |
|
214 | |||
215 | 1 | if (!class_exists('IronMQ\IronMQ')) { |
|
216 | throw new \RuntimeException( |
||
217 | 'You must require "iron-io/iron_mq" to use the Iron MQ provider.' |
||
218 | ); |
||
219 | } |
||
220 | |||
221 | 1 | $ironmq = new Definition('IronMQ\IronMQ'); |
|
222 | 1 | $ironmq->setArguments([ |
|
223 | [ |
||
224 | 1 | 'token' => $config['token'], |
|
225 | 1 | 'project_id' => $config['project_id'], |
|
226 | 1 | 'host' => sprintf('%s.iron.io', $config['host']), |
|
227 | 1 | 'port' => $config['port'], |
|
228 | 1 | 'api_version' => $config['api_version'] |
|
229 | ] |
||
230 | ]); |
||
231 | |||
232 | 1 | $container->setDefinition($service, $ironmq) |
|
233 | 1 | ->setPublic(false); |
|
234 | } |
||
235 | |||
236 | 1 | return new Reference($service); |
|
237 | } |
||
238 | |||
239 | private function createSyncClient() |
||
243 | |||
244 | private function createDoctrineClient($config) |
||
248 | |||
249 | /** |
||
250 | * @param string $serviceId |
||
251 | * |
||
252 | * @return Reference |
||
253 | */ |
||
254 | private function createCustomClient($serviceId) |
||
258 | |||
259 | /** |
||
260 | * Returns the Extension Alias |
||
261 | * |
||
262 | * @return string |
||
263 | */ |
||
264 | 1 | public function getAlias() |
|
268 | } |
||
269 |
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.