Complex classes like ProductStreamService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ProductStreamService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class ProductStreamService |
||
| 16 | { |
||
| 17 | const DYNAMIC_STREAM = 1; |
||
| 18 | const STATIC_STREAM = 2; |
||
| 19 | const STATUS_EXPORT = 'export'; |
||
| 20 | const STATUS_DELETE = 'delete'; |
||
| 21 | const STATUS_ERROR = 'error'; |
||
| 22 | const STATUS_SYNCED = 'synced'; |
||
| 23 | const STATUS_PENDING = 'pending'; |
||
| 24 | const PRODUCT_LIMIT = 100; |
||
| 25 | |||
| 26 | //Available statuses for exported stream |
||
| 27 | const EXPORTED_STATUSES = [ |
||
| 28 | self::STATUS_EXPORT, |
||
| 29 | self::STATUS_SYNCED, |
||
| 30 | self::STATUS_ERROR, |
||
| 31 | ]; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var ProductStreamRepository |
||
| 35 | */ |
||
| 36 | private $productStreamRepository; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var ProductStreamAttributeRepository |
||
| 40 | */ |
||
| 41 | private $streamAttrRepository; |
||
| 42 | |||
| 43 | /** @var Config */ |
||
| 44 | private $config; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param ProductStreamRepository $productStreamRepository |
||
| 48 | * @param ProductStreamAttributeRepository $streamAttrRepository |
||
| 49 | * @param Config $config |
||
| 50 | */ |
||
| 51 | public function __construct( |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param $streamId |
||
| 63 | * @param bool $appendCurrent |
||
| 64 | * @return ProductStreamsAssignments |
||
| 65 | */ |
||
| 66 | public function prepareStreamsAssignments($streamId, $appendCurrent = true) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param $streamId |
||
| 88 | * @throws \Exception |
||
| 89 | * @return ProductStreamsAssignments |
||
| 90 | */ |
||
| 91 | public function getStreamAssignments($streamId) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param ProductStream $stream |
||
| 107 | * @return bool |
||
| 108 | */ |
||
| 109 | public function isConnectStream(ProductStream $stream) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param ProductStream $stream |
||
| 122 | * @return bool |
||
| 123 | */ |
||
| 124 | public function activateConnectProductsByStream(ProductStream $stream) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param $streamId |
||
| 131 | * @return mixed |
||
| 132 | */ |
||
| 133 | public function findStream($streamId) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param array $streamIds |
||
| 140 | * @return \Shopware\Models\ProductStream\ProductStream[] |
||
| 141 | */ |
||
| 142 | public function findStreams(array $streamIds) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Filter the stream ids, it will return only the exported ones |
||
| 149 | * |
||
| 150 | * @param array $streamIds |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | public function filterExportedStreams(array $streamIds) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param array $articleIds |
||
| 160 | * @return array |
||
| 161 | */ |
||
| 162 | public function collectRelatedStreamsAssignments(array $articleIds) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param ProductStreamsAssignments $assignments |
||
| 188 | * @param $streamId |
||
| 189 | * @param $articleId |
||
| 190 | * @return bool |
||
| 191 | */ |
||
| 192 | public function allowToRemove(ProductStreamsAssignments $assignments, $streamId, $articleId) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param ProductStream $stream |
||
| 209 | * @return array |
||
| 210 | */ |
||
| 211 | public function getArticlesIds(ProductStream $stream) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param ProductStream $productStream |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | public function isStatic(ProductStream $productStream) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param null $start |
||
| 235 | * @param null $limit |
||
| 236 | * @return array |
||
| 237 | */ |
||
| 238 | public function getList($start = null, $limit = null) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param $type |
||
| 261 | * @return array |
||
| 262 | */ |
||
| 263 | public function getAllExportedStreams($type) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param ProductStream $stream |
||
| 270 | * @return ProductStreamAttribute |
||
| 271 | */ |
||
| 272 | public function createStreamAttribute(ProductStream $stream) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param $streamId |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | public function countProductsInStaticStream($streamId) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param $streamId |
||
| 295 | * @return bool |
||
| 296 | */ |
||
| 297 | public function isStreamExported($streamId) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param $streamId |
||
| 304 | * @return \Doctrine\DBAL\Driver\Statement|int |
||
| 305 | */ |
||
| 306 | public function markProductsToBeRemovedFromStream($streamId) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @param $streamId |
||
| 313 | * @param array $articleIds |
||
| 314 | * @return array |
||
| 315 | */ |
||
| 316 | public function createStreamRelation($streamId, array $articleIds) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @return \Doctrine\DBAL\Driver\Statement|int |
||
| 323 | */ |
||
| 324 | public function removeMarkedStreamRelations() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @return array |
||
| 331 | */ |
||
| 332 | public function getConnectStreamIds() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @param int $streamId |
||
| 339 | * @param string $status |
||
| 340 | * @param string $exportMessage |
||
| 341 | */ |
||
| 342 | public function changeStatus($streamId, $status, $exportMessage = null) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @param $streamId |
||
| 360 | * @param $message |
||
| 361 | * @deprecated Use changeStatus instead. Will be removed in version 1.2.0 |
||
| 362 | */ |
||
| 363 | public function log($streamId, $message) |
||
| 371 | } |
||
| 372 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.