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 |
||
| 27 | class SubscriberRegistration |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var ModelManager |
||
| 31 | */ |
||
| 32 | private $modelManager; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var Config |
||
| 36 | */ |
||
| 37 | private $config; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var Enlight_Components_Db_Adapter_Pdo_Mysql |
||
| 41 | */ |
||
| 42 | private $db; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @TODO: Subscribers should not depend on the Bootstrap class. If you see a possible solution refactor it please. |
||
| 46 | * |
||
| 47 | * @var \Shopware_Plugins_Backend_SwagConnect_Bootstrap |
||
| 48 | */ |
||
| 49 | private $pluginBootstrap; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var \Enlight_Event_EventManager |
||
| 53 | */ |
||
| 54 | private $eventManager; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var SDK |
||
| 58 | */ |
||
| 59 | private $SDK; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var ConnectFactory |
||
| 63 | */ |
||
| 64 | private $connectFactory; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var Helper |
||
| 68 | */ |
||
| 69 | private $helper; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * This property saves all product updates and will be inserted back later |
||
| 73 | * |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | private $productUpdates = []; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var Lifecycle |
||
| 80 | */ |
||
| 81 | private $lifecycle; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var Container |
||
| 85 | */ |
||
| 86 | private $container; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param Config $config |
||
| 90 | * @param ModelManager $modelManager |
||
| 91 | * @param Enlight_Components_Db_Adapter_Pdo_Mysql $db |
||
| 92 | * @param \Shopware_Plugins_Backend_SwagConnect_Bootstrap $pluginBootstrap |
||
| 93 | * @param \Enlight_Event_EventManager $eventManager |
||
| 94 | * @param SDK $SDK |
||
| 95 | * @param ConnectFactory $connectFactory |
||
| 96 | * @param Helper $helper |
||
| 97 | * @param Container $container |
||
| 98 | */ |
||
| 99 | View Code Duplication | public function __construct( |
|
| 100 | Config $config, |
||
| 101 | ModelManager $modelManager, |
||
| 102 | Enlight_Components_Db_Adapter_Pdo_Mysql $db, |
||
| 103 | \Shopware_Plugins_Backend_SwagConnect_Bootstrap $pluginBootstrap, |
||
| 104 | \Enlight_Event_EventManager $eventManager, |
||
| 105 | SDK $SDK, |
||
| 106 | ConnectFactory $connectFactory, |
||
| 107 | Helper $helper, |
||
| 108 | Container $container |
||
| 109 | ) { |
||
| 110 | $this->config = $config; |
||
| 111 | $this->modelManager = $modelManager; |
||
| 112 | $this->db = $db; |
||
| 113 | $this->pluginBootstrap = $pluginBootstrap; |
||
| 114 | $this->eventManager = $eventManager; |
||
| 115 | $this->SDK = $SDK; |
||
| 116 | $this->connectFactory = $connectFactory; |
||
| 117 | $this->helper = $helper; |
||
| 118 | $this->container = $container; |
||
| 119 | } |
||
| 120 | |||
| 121 | public function registerSubscribers() |
||
| 122 | { |
||
| 123 | try { |
||
| 124 | $verified = $this->config->getConfig('apiKeyVerified', false); |
||
| 125 | } catch (\Exception $e) { |
||
| 126 | // if the config table is not available, just assume, that the update |
||
| 127 | // still needs to be installed |
||
| 128 | $verified = false; |
||
| 129 | } |
||
| 130 | |||
| 131 | $newSubscribers = $this->getNewSubscribers(); |
||
| 132 | |||
| 133 | // Some subscribers may only be used, if the SDK is verified |
||
| 134 | if ($verified) { |
||
| 135 | $newSubscribers = array_merge($newSubscribers, [ |
||
| 136 | new \ShopwarePlugins\Connect\Subscribers\BasketWidget( |
||
| 137 | $this->pluginBootstrap->getBasketHelper(), |
||
| 138 | $this->helper |
||
| 139 | ), |
||
| 140 | new Checkout( |
||
| 141 | $this->modelManager, |
||
| 142 | $this->eventManager, |
||
| 143 | $this->connectFactory->getSDK(), |
||
| 144 | $this->connectFactory->getBasketHelper(), |
||
| 145 | $this->connectFactory->getHelper() |
||
| 146 | ), |
||
| 147 | new \ShopwarePlugins\Connect\Subscribers\Dispatches( |
||
| 148 | $this->helper, |
||
| 149 | $this->pluginBootstrap->Path(), |
||
| 150 | $this->container->get('snippets') |
||
| 151 | ), |
||
| 152 | new \ShopwarePlugins\Connect\Subscribers\Javascript(), |
||
| 153 | new \ShopwarePlugins\Connect\Subscribers\Less() |
||
| 154 | ]); |
||
| 155 | // These subscribers are used if the api key is not valid |
||
| 156 | } else { |
||
| 157 | $newSubscribers = array_merge($newSubscribers, [ |
||
| 158 | new \ShopwarePlugins\Connect\Subscribers\DisableConnectInFrontend( |
||
| 159 | $this->pluginBootstrap->Path(), $this->container->get('db') |
||
| 160 | ), |
||
| 161 | new \ShopwarePlugins\Connect\Subscribers\TemplateExtension( |
||
| 162 | $this->pluginBootstrap->Path(), |
||
| 163 | $this->container->get('snippets'), |
||
| 164 | $this->SDK, |
||
| 165 | $this->helper |
||
| 166 | ), |
||
| 167 | new \ShopwarePlugins\Connect\Subscribers\Voucher( |
||
| 168 | $this->helper, |
||
| 169 | $this->connectFactory->getBasketHelper(), |
||
| 170 | $this->container->get('snippets') |
||
| 171 | ), |
||
| 172 | ]); |
||
| 173 | } |
||
| 174 | |||
| 175 | foreach ($newSubscribers as $newSubscriber) { |
||
| 176 | $this->eventManager->addSubscriber($newSubscriber); |
||
| 177 | } |
||
| 178 | |||
| 179 | $this->modelManager->getEventManager()->addEventListener( |
||
| 180 | [\Doctrine\ORM\Events::onFlush, \Doctrine\ORM\Events::postFlush], |
||
| 181 | $this |
||
| 182 | ); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @return array |
||
| 187 | */ |
||
| 188 | private function getNewSubscribers() |
||
| 189 | { |
||
| 190 | return [ |
||
| 191 | new \ShopwarePlugins\Connect\Subscribers\Article( |
||
| 192 | new PDO($this->db->getConnection()), |
||
| 193 | $this->modelManager, |
||
| 194 | $this->connectFactory->getConnectExport(), |
||
| 195 | $this->helper, |
||
| 196 | $this->config, |
||
| 197 | $this->connectFactory->getSDK(), |
||
| 198 | $this->container->get('snippets'), |
||
| 199 | $this->pluginBootstrap->Path() |
||
| 200 | ), |
||
| 201 | new \ShopwarePlugins\Connect\Subscribers\ArticleList( |
||
| 202 | $this->pluginBootstrap->Path(), |
||
| 203 | $this->container->get('snippets') |
||
| 204 | ), |
||
| 205 | new \ShopwarePlugins\Connect\Subscribers\Category( |
||
| 206 | $this->container->get('dbal_connection'), |
||
| 207 | $this->createProductStreamService() |
||
| 208 | ), |
||
| 209 | new \ShopwarePlugins\Connect\Subscribers\Connect( |
||
| 210 | $this->config, |
||
| 211 | $this->SDK, |
||
| 212 | $this->container->get('snippets'), |
||
| 213 | $this->pluginBootstrap->Path() |
||
| 214 | ), |
||
| 215 | new ControllerPath( |
||
| 216 | $this->pluginBootstrap->Path(), |
||
| 217 | $this->container, |
||
| 218 | $this->container->get('snippets') |
||
| 219 | ), |
||
| 220 | new \ShopwarePlugins\Connect\Subscribers\CronJob( |
||
| 221 | $this->SDK, |
||
| 222 | $this->connectFactory->getConnectExport(), |
||
| 223 | $this->config, |
||
| 224 | $this->helper |
||
| 225 | ), |
||
| 226 | new CustomerGroup( |
||
| 227 | $this->modelManager, |
||
| 228 | new Logger(Shopware()->Db()) |
||
| 229 | ), |
||
| 230 | $this->getLifecycleSubscriber(), |
||
| 231 | new \ShopwarePlugins\Connect\Subscribers\OrderDocument(), |
||
| 232 | new \ShopwarePlugins\Connect\Subscribers\PaymentSubscriber( |
||
| 233 | $this->pluginBootstrap->Path(), |
||
| 234 | $this->helper |
||
| 235 | ), |
||
| 236 | new \ShopwarePlugins\Connect\Subscribers\ProductStreams( |
||
| 237 | $this->connectFactory->getConnectExport(), |
||
| 238 | new Config($this->modelManager), |
||
| 239 | $this->helper, |
||
| 240 | $this->SDK, |
||
| 241 | $this->pluginBootstrap->Path(), |
||
| 242 | $this->container->get('snippets') |
||
| 243 | ), |
||
| 244 | new \ShopwarePlugins\Connect\Subscribers\Property( |
||
| 245 | $this->modelManager, |
||
| 246 | $this->pluginBootstrap->Path(), |
||
| 247 | $this->container->get('snippets') |
||
| 248 | ), |
||
| 249 | new \ShopwarePlugins\Connect\Subscribers\Search( |
||
| 250 | $this->modelManager, |
||
| 251 | $this->pluginBootstrap->Path(), |
||
| 252 | $this->container->get('snippets') |
||
| 253 | ), |
||
| 254 | new \ShopwarePlugins\Connect\Subscribers\ServiceContainer( |
||
| 255 | $this->modelManager, |
||
| 256 | $this->db, |
||
| 257 | $this->container |
||
| 258 | ), |
||
| 259 | new \ShopwarePlugins\Connect\Subscribers\Supplier( |
||
| 260 | $this->pluginBootstrap->Path(), |
||
| 261 | $this->container->get('snippets'), |
||
| 262 | $this->container->get('dbal_connection') |
||
| 263 | ), |
||
| 264 | ]; |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Generate changes for updated Articles and Details. |
||
| 269 | * On postFlush all related entities are updated and product can |
||
| 270 | * be fetched from DB correctly. |
||
| 271 | * |
||
| 272 | * @param \Doctrine\ORM\Event\PostFlushEventArgs $eventArgs |
||
| 273 | */ |
||
| 274 | public function postFlush(\Doctrine\ORM\Event\PostFlushEventArgs $eventArgs) |
||
| 275 | { |
||
| 276 | foreach ($this->productUpdates as $entity) { |
||
| 277 | $this->getLifecycleSubscriber()->handleChange($entity); |
||
| 278 | } |
||
| 279 | |||
| 280 | $this->productUpdates = []; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return Lifecycle |
||
| 285 | */ |
||
| 286 | private function getLifecycleSubscriber() |
||
| 287 | { |
||
| 288 | if (!$this->lifecycle) { |
||
| 289 | $this->lifecycle = new Lifecycle( |
||
| 290 | $this->modelManager, |
||
| 291 | $this->config->getConfig('autoUpdateProducts', 1), |
||
| 292 | $this->helper, |
||
| 293 | $this->SDK, |
||
| 294 | $this->config, |
||
| 295 | $this->connectFactory->getConnectExport() |
||
| 296 | ); |
||
| 297 | } |
||
| 298 | |||
| 299 | return $this->lifecycle; |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Collect updated Articles and Details |
||
| 304 | * Lifecycle events don't work correctly, because products will be fetched via query builder, |
||
| 305 | * but related entities like price are not updated yet. |
||
| 306 | * |
||
| 307 | * @param \Doctrine\ORM\Event\OnFlushEventArgs $eventArgs |
||
| 308 | */ |
||
| 309 | public function onFlush(\Doctrine\ORM\Event\OnFlushEventArgs $eventArgs) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @return ProductStreamService |
||
| 329 | */ |
||
| 330 | View Code Duplication | private function createProductStreamService() |
|
| 331 | { |
||
| 343 | } |
||
| 344 |
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.