| Conditions | 12 |
| Paths | 74 |
| Total Lines | 70 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 53 | public function register(ProductInterface $product, ChannelInterface $channel, bool $createOnly = false) |
||
| 54 | { |
||
| 55 | if (!$this->enabled) { |
||
| 56 | return false; |
||
| 57 | } |
||
| 58 | |||
| 59 | $productId = (string) $product->getId(); |
||
| 60 | $storeId = $channel->getCode(); |
||
| 61 | |||
| 62 | $response = $this->ecommerceApi->getProduct($storeId, $productId); |
||
|
|
|||
| 63 | $isNew = !isset($response['id']); |
||
| 64 | |||
| 65 | // Do nothing if the product exists |
||
| 66 | if (false === $isNew && true === $createOnly) { |
||
| 67 | return false; |
||
| 68 | } |
||
| 69 | |||
| 70 | $variants = []; |
||
| 71 | /** @var ProductVariant $productVariant */ |
||
| 72 | foreach ($product->getVariants() as $productVariant) { |
||
| 73 | $variant = [ |
||
| 74 | 'id' => (string) $productVariant->getId(), |
||
| 75 | 'title' => $productVariant->getName() ? $productVariant->getName() : $product->getName(), |
||
| 76 | 'inventory_quantity' => $productVariant->isTracked() ? $productVariant->getOnHand() : 1, |
||
| 77 | ]; |
||
| 78 | |||
| 79 | if ($variantPrice = $this->getVariantPrice($productVariant, $channel)) { |
||
| 80 | $variant['price'] = $variantPrice / 100; |
||
| 81 | } |
||
| 82 | |||
| 83 | $variants[] = $variant; |
||
| 84 | } |
||
| 85 | |||
| 86 | $productImages = []; |
||
| 87 | /** @var ProductImageInterface $image */ |
||
| 88 | foreach ($product->getImages() as $image) { |
||
| 89 | $productImages[] = [ |
||
| 90 | 'id' => (string) $image->getId(), |
||
| 91 | 'url' => $this->getImageUrl($image, $channel), |
||
| 92 | ]; |
||
| 93 | } |
||
| 94 | |||
| 95 | $data = [ |
||
| 96 | 'id' => $productId, |
||
| 97 | 'title' => $product->getName(), |
||
| 98 | 'url' => $this->getProductUrl($product, $channel), |
||
| 99 | 'description' => $product->getDescription() ?: '', |
||
| 100 | 'images' => $productImages, |
||
| 101 | 'variants' => $variants, |
||
| 102 | ]; |
||
| 103 | |||
| 104 | if (count($productImages) > 0) { |
||
| 105 | $data['image_url'] = $productImages[0]['url']; |
||
| 106 | } |
||
| 107 | |||
| 108 | if ($isNew) { |
||
| 109 | $event = new GenericEvent($product, ['data' => $data, 'channel' => $channel]); |
||
| 110 | $this->eventDispatcher->dispatch($event, 'mailchimp.product.pre_add'); |
||
| 111 | $data = $event->getArgument('data'); |
||
| 112 | |||
| 113 | $response = $this->ecommerceApi->addProduct($storeId, $data); |
||
| 114 | } else { |
||
| 115 | $event = new GenericEvent($product, ['data' => $data, 'channel' => $channel]); |
||
| 116 | $this->eventDispatcher->dispatch($event, 'mailchimp.product.pre_update'); |
||
| 117 | $data = $event->getArgument('data'); |
||
| 118 | |||
| 119 | $response = $this->ecommerceApi->updateProduct($storeId, $productId, $data); |
||
| 120 | } |
||
| 121 | |||
| 122 | return $response; |
||
| 123 | } |
||
| 210 |