| Total Complexity | 40 |
| Total Lines | 556 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Category 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.
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 Category, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Category implements CategoryInterface |
||
| 17 | { |
||
| 18 | use CategoryTrait; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var int |
||
| 22 | * |
||
| 23 | * @ORM\Id |
||
| 24 | * @ORM\GeneratedValue |
||
| 25 | * @ORM\Column(type="integer") |
||
| 26 | **/ |
||
| 27 | protected $id; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var int |
||
| 31 | * |
||
| 32 | * @ORM\Column(type="integer", unique=true) |
||
| 33 | */ |
||
| 34 | protected $externalId; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string|null |
||
| 38 | * |
||
| 39 | * @ORM\Column(nullable=true, type="string", length=191) |
||
| 40 | */ |
||
| 41 | protected $b2bGroupId; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var \DateTimeImmutable|null |
||
| 45 | * |
||
| 46 | * @ORM\Column(nullable=true, type="datetime_immutable") |
||
| 47 | */ |
||
| 48 | protected $createdDate; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var int|null |
||
| 52 | * |
||
| 53 | * @ORM\Column(nullable=true, type="integer") |
||
| 54 | */ |
||
| 55 | protected $customInfoLayout; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var int|null |
||
| 59 | * |
||
| 60 | * @ORM\Column(nullable=true, type="integer") |
||
| 61 | */ |
||
| 62 | protected $customListLayout; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var int|null |
||
| 66 | * |
||
| 67 | * @ORM\Column(nullable=true, type="integer") |
||
| 68 | */ |
||
| 69 | protected $defaultParentId; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var \DateTimeImmutable|null |
||
| 73 | * |
||
| 74 | * @ORM\Column(nullable=true, type="datetime_immutable") |
||
| 75 | */ |
||
| 76 | protected $editedDate; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var int|null |
||
| 80 | * |
||
| 81 | * @ORM\Column(nullable=true, type="integer") |
||
| 82 | */ |
||
| 83 | protected $infoLayout; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var int|null |
||
| 87 | * |
||
| 88 | * @ORM\Column(nullable=true, type="integer") |
||
| 89 | */ |
||
| 90 | protected $internalId; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var int|null |
||
| 94 | * |
||
| 95 | * @ORM\Column(nullable=true, type="integer") |
||
| 96 | */ |
||
| 97 | protected $listLayout; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var bool|null |
||
| 101 | * |
||
| 102 | * @ORM\Column(nullable=true, type="boolean") |
||
| 103 | */ |
||
| 104 | protected $modified; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var array|null |
||
| 108 | * |
||
| 109 | * @ORM\Column(nullable=true, type="json") |
||
| 110 | */ |
||
| 111 | protected $parentIdList; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var array|null |
||
| 115 | * |
||
| 116 | * @ORM\Column(nullable=true, type="json") |
||
| 117 | */ |
||
| 118 | protected $segmentIdList; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @var Category[]|ArrayCollection |
||
| 122 | * |
||
| 123 | * @ORM\ManyToMany(mappedBy="parentCategories", targetEntity="Category") |
||
| 124 | */ |
||
| 125 | protected $childrenCategories; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var Category[]|ArrayCollection |
||
| 129 | * |
||
| 130 | * @ORM\JoinTable(name="loevgaard_dandomain_category_parents") |
||
| 131 | * @ORM\ManyToMany(cascade={"persist"}, inversedBy="childrenCategories", targetEntity="Category") |
||
| 132 | */ |
||
| 133 | protected $parentCategories; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @var Product[]||ArrayCollection |
||
| 137 | * |
||
| 138 | * @ORM\ManyToMany(mappedBy="categories", targetEntity="Product") |
||
| 139 | */ |
||
|
|
|||
| 140 | protected $products; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @var Segment[]|ArrayCollection |
||
| 144 | * |
||
| 145 | * @ORM\JoinTable(name="loevgaard_dandomain_category_segments") |
||
| 146 | * @ORM\ManyToMany(cascade={"persist"}, inversedBy="categories", targetEntity="Segment") |
||
| 147 | */ |
||
| 148 | protected $segments; |
||
| 149 | |||
| 150 | public function __construct() |
||
| 151 | { |
||
| 152 | $this->childrenCategories = new ArrayCollection(); |
||
| 153 | $this->parentCategories = new ArrayCollection(); |
||
| 154 | $this->products = new ArrayCollection(); |
||
| 155 | $this->segments = new ArrayCollection(); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Populates a variant based on the response from the Dandomain API |
||
| 160 | * |
||
| 161 | * See the properties here: |
||
| 162 | * http://4221117.shop53.dandomain.dk/admin/webapi/endpoints/v1_0/ProductDataService/help/operations/GetDataProduct |
||
| 163 | * |
||
| 164 | * @param \stdClass|array $data |
||
| 165 | */ |
||
| 166 | public function populateFromApiResponse($data) |
||
| 167 | { |
||
| 168 | $data = DandomainFoundation\objectToArray($data); |
||
| 169 | |||
| 170 | /* |
||
| 171 | $actualTexts = null; |
||
| 172 | if (is_array($data['texts'])) { |
||
| 173 | foreach ($data['texts'] as $text) { |
||
| 174 | if ($text->siteId != $this->defaultSiteId) { |
||
| 175 | continue; |
||
| 176 | } |
||
| 177 | |||
| 178 | $actualTexts = $text; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | */ |
||
| 182 | |||
| 183 | if ($data['createdDate']) { |
||
| 184 | $this->createdDate = DateTimeImmutable::createFromJson($data['createdDate']); |
||
| 185 | } |
||
| 186 | |||
| 187 | if ($data['editedDate']) { |
||
| 188 | $this->createdDate = DateTimeImmutable::createFromJson($data['editedDate']); |
||
| 189 | } |
||
| 190 | |||
| 191 | $this |
||
| 192 | ->setB2bGroupId($data['b2BGroupId']) |
||
| 193 | ->setCustomInfoLayout($data['customInfoLayout']) |
||
| 194 | ->setCustomListLayout($data['customListLayout']) |
||
| 195 | ->setDefaultParentId($data['defaultParentId']) |
||
| 196 | ->setExternalId($data['number']) |
||
| 197 | ->setInfoLayout($data['infoLayout']) |
||
| 198 | ->setInternalId($data['internalId']) |
||
| 199 | ->setListLayout($data['listLayout']) |
||
| 200 | ->setModified($data['modified']) |
||
| 201 | ->setParentIdList($data['parentIdList']) |
||
| 202 | ->setSegmentIdList($data['segmentIdList']) |
||
| 203 | ; |
||
| 204 | |||
| 205 | /* |
||
| 206 | if (is_array($data['parentIdList'])) { |
||
| 207 | foreach ($data['parentIdList'] as $parentId) { |
||
| 208 | $parentEntity = $this->objectManager->getRepository($this->entityClassName)->findOneBy([ |
||
| 209 | 'externalId' => (int) $parentId, |
||
| 210 | ]); |
||
| 211 | |||
| 212 | if (null !== $parentEntity) { |
||
| 213 | $this->addParentCategory($parentEntity); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | } |
||
| 217 | |||
| 218 | if (is_array($data['segmentIdList'])) { |
||
| 219 | foreach ($data['segmentIdList'] as $segmentId) { |
||
| 220 | $segment = $this->segmentSynchronizer->syncSegment($segmentId, $flush); |
||
| 221 | $this->addSegment($segment); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | if (($entity instanceof TranslatableInterface) && (is_array($data['texts']))) { |
||
| 226 | foreach ($data['texts'] as $text) { |
||
| 227 | $entity->translate($text->siteId)->setCategoryNumber($text->categoryNumber); |
||
| 228 | $entity->translate($text->siteId)->setDescription($text->description); |
||
| 229 | $entity->translate($text->siteId)->setExternalId($text->id); |
||
| 230 | $entity->translate($text->siteId)->setHidden($text->hidden); |
||
| 231 | $entity->translate($text->siteId)->setHiddenMobile($text->hiddenMobile); |
||
| 232 | $entity->translate($text->siteId)->setIcon($text->icon); |
||
| 233 | $entity->translate($text->siteId)->setImage($text->image); |
||
| 234 | $entity->translate($text->siteId)->setKeywords($text->Keywords); |
||
| 235 | $entity->translate($text->siteId)->setLink($text->link); |
||
| 236 | $entity->translate($text->siteId)->setMetaDescription($text->metaDescription); |
||
| 237 | $entity->translate($text->siteId)->setName($text->name); |
||
| 238 | $entity->translate($text->siteId)->setSiteId($text->siteId); |
||
| 239 | $entity->translate($text->siteId)->setSortOrder($text->sortOrder); |
||
| 240 | $entity->translate($text->siteId)->setString($text->string); |
||
| 241 | $entity->translate($text->siteId)->setTitle($text->title); |
||
| 242 | $entity->translate($text->siteId)->setUrlname($text->urlname); |
||
| 243 | |||
| 244 | $entity->mergeNewTranslations(); |
||
| 245 | } |
||
| 246 | } |
||
| 247 | */ |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @return int |
||
| 252 | */ |
||
| 253 | public function getId(): int |
||
| 254 | { |
||
| 255 | return (int)$this->id; |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param int $id |
||
| 260 | * @return Category |
||
| 261 | */ |
||
| 262 | public function setId(int $id) |
||
| 263 | { |
||
| 264 | $this->id = $id; |
||
| 265 | return $this; |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @return int |
||
| 270 | */ |
||
| 271 | public function getExternalId(): int |
||
| 272 | { |
||
| 273 | return (int)$this->externalId; |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param int $externalId |
||
| 278 | * @return Category |
||
| 279 | */ |
||
| 280 | public function setExternalId(int $externalId) |
||
| 281 | { |
||
| 282 | $this->externalId = $externalId; |
||
| 283 | return $this; |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @return null|string |
||
| 288 | */ |
||
| 289 | public function getB2bGroupId() |
||
| 290 | { |
||
| 291 | return $this->b2bGroupId; |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param null|string $b2bGroupId |
||
| 296 | * @return Category |
||
| 297 | */ |
||
| 298 | public function setB2bGroupId($b2bGroupId) |
||
| 299 | { |
||
| 300 | $this->b2bGroupId = $b2bGroupId; |
||
| 301 | return $this; |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @return \DateTimeImmutable|null |
||
| 306 | */ |
||
| 307 | public function getCreatedDate() |
||
| 308 | { |
||
| 309 | return $this->createdDate; |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param \DateTimeImmutable|null $createdDate |
||
| 314 | * @return Category |
||
| 315 | */ |
||
| 316 | public function setCreatedDate($createdDate) |
||
| 317 | { |
||
| 318 | $this->createdDate = $createdDate; |
||
| 319 | return $this; |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @return int|null |
||
| 324 | */ |
||
| 325 | public function getCustomInfoLayout() |
||
| 326 | { |
||
| 327 | return $this->customInfoLayout; |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @param int|null $customInfoLayout |
||
| 332 | * @return Category |
||
| 333 | */ |
||
| 334 | public function setCustomInfoLayout($customInfoLayout) |
||
| 335 | { |
||
| 336 | $this->customInfoLayout = $customInfoLayout; |
||
| 337 | return $this; |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @return int|null |
||
| 342 | */ |
||
| 343 | public function getCustomListLayout() |
||
| 344 | { |
||
| 345 | return $this->customListLayout; |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param int|null $customListLayout |
||
| 350 | * @return Category |
||
| 351 | */ |
||
| 352 | public function setCustomListLayout($customListLayout) |
||
| 353 | { |
||
| 354 | $this->customListLayout = $customListLayout; |
||
| 355 | return $this; |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @return int|null |
||
| 360 | */ |
||
| 361 | public function getDefaultParentId() |
||
| 362 | { |
||
| 363 | return $this->defaultParentId; |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @param int|null $defaultParentId |
||
| 368 | * @return Category |
||
| 369 | */ |
||
| 370 | public function setDefaultParentId($defaultParentId) |
||
| 371 | { |
||
| 372 | $this->defaultParentId = $defaultParentId; |
||
| 373 | return $this; |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @return \DateTimeImmutable|null |
||
| 378 | */ |
||
| 379 | public function getEditedDate() |
||
| 380 | { |
||
| 381 | return $this->editedDate; |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param \DateTimeImmutable|null $editedDate |
||
| 386 | * @return Category |
||
| 387 | */ |
||
| 388 | public function setEditedDate($editedDate) |
||
| 389 | { |
||
| 390 | $this->editedDate = $editedDate; |
||
| 391 | return $this; |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @return int|null |
||
| 396 | */ |
||
| 397 | public function getInfoLayout() |
||
| 398 | { |
||
| 399 | return $this->infoLayout; |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @param int|null $infoLayout |
||
| 404 | * @return Category |
||
| 405 | */ |
||
| 406 | public function setInfoLayout($infoLayout) |
||
| 407 | { |
||
| 408 | $this->infoLayout = $infoLayout; |
||
| 409 | return $this; |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @return int|null |
||
| 414 | */ |
||
| 415 | public function getInternalId() |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @param int|null $internalId |
||
| 422 | * @return Category |
||
| 423 | */ |
||
| 424 | public function setInternalId($internalId) |
||
| 425 | { |
||
| 426 | $this->internalId = $internalId; |
||
| 427 | return $this; |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @return int|null |
||
| 432 | */ |
||
| 433 | public function getListLayout() |
||
| 434 | { |
||
| 435 | return $this->listLayout; |
||
| 436 | } |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @param int|null $listLayout |
||
| 440 | * @return Category |
||
| 441 | */ |
||
| 442 | public function setListLayout($listLayout) |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @return bool|null |
||
| 450 | */ |
||
| 451 | public function getModified() |
||
| 452 | { |
||
| 453 | return $this->modified; |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @param bool|null $modified |
||
| 458 | * @return Category |
||
| 459 | */ |
||
| 460 | public function setModified($modified) |
||
| 461 | { |
||
| 462 | $this->modified = $modified; |
||
| 463 | return $this; |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @return array|null |
||
| 468 | */ |
||
| 469 | public function getParentIdList() |
||
| 470 | { |
||
| 471 | return $this->parentIdList; |
||
| 472 | } |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @param array|null $parentIdList |
||
| 476 | * @return Category |
||
| 477 | */ |
||
| 478 | public function setParentIdList($parentIdList) |
||
| 479 | { |
||
| 480 | $this->parentIdList = $parentIdList; |
||
| 481 | return $this; |
||
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @return array|null |
||
| 486 | */ |
||
| 487 | public function getSegmentIdList() |
||
| 488 | { |
||
| 489 | return $this->segmentIdList; |
||
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @param array|null $segmentIdList |
||
| 494 | * @return Category |
||
| 495 | */ |
||
| 496 | public function setSegmentIdList($segmentIdList) |
||
| 497 | { |
||
| 498 | $this->segmentIdList = $segmentIdList; |
||
| 499 | return $this; |
||
| 500 | } |
||
| 501 | |||
| 502 | /** |
||
| 503 | * @return ArrayCollection|Category[] |
||
| 504 | */ |
||
| 505 | public function getChildrenCategories() |
||
| 506 | { |
||
| 507 | return $this->childrenCategories; |
||
| 508 | } |
||
| 509 | |||
| 510 | /** |
||
| 511 | * @param ArrayCollection|Category[] $childrenCategories |
||
| 512 | * @return Category |
||
| 513 | */ |
||
| 514 | public function setChildrenCategories($childrenCategories) |
||
| 515 | { |
||
| 516 | $this->childrenCategories = $childrenCategories; |
||
| 517 | return $this; |
||
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * @return ArrayCollection|Category[] |
||
| 522 | */ |
||
| 523 | public function getParentCategories() |
||
| 524 | { |
||
| 525 | return $this->parentCategories; |
||
| 526 | } |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @param ArrayCollection|Category[] $parentCategories |
||
| 530 | * @return Category |
||
| 531 | */ |
||
| 532 | public function setParentCategories($parentCategories) |
||
| 533 | { |
||
| 534 | $this->parentCategories = $parentCategories; |
||
| 535 | return $this; |
||
| 536 | } |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @return Product[] |
||
| 540 | */ |
||
| 541 | public function getProducts(): array |
||
| 544 | } |
||
| 545 | |||
| 546 | /** |
||
| 547 | * @param Product[] $products |
||
| 548 | * @return Category |
||
| 549 | */ |
||
| 550 | public function setProducts(array $products) |
||
| 551 | { |
||
| 552 | $this->products = $products; |
||
| 553 | return $this; |
||
| 554 | } |
||
| 555 | |||
| 556 | /** |
||
| 557 | * @return ArrayCollection|Segment[] |
||
| 558 | */ |
||
| 559 | public function getSegments() |
||
| 560 | { |
||
| 561 | return $this->segments; |
||
| 562 | } |
||
| 563 | |||
| 564 | /** |
||
| 565 | * @param ArrayCollection|Segment[] $segments |
||
| 566 | * @return Category |
||
| 567 | */ |
||
| 568 | public function setSegments($segments) |
||
| 572 | } |
||
| 573 | } |
||
| 574 |