ProductSaveAfterObserver   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 7.04 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 5
loc 71
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B execute() 5 28 7

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace Stockbase\Integration\Model\Observer;
4
5
use Psr\Log\LoggerInterface;
6
use Magento\Framework\Event\ObserverInterface;
7
use Magento\Framework\Event\Observer as EventObserver;
8
use Stockbase\Integration\Model\Config\StockbaseConfiguration;
9
use Stockbase\Integration\StockbaseApi\Client\StockbaseClientFactory;
10
use Stockbase\Integration\Helper\Images as ImagesHelper;
11
12
/**
13
 * Class ProductSaveAfterObserver
14
 */
15
class ProductSaveAfterObserver implements ObserverInterface
16
{
17
18
    /**
19
     * @var LoggerInterface
20
     */
21
    private $logger;
22
    /**
23
     * @var StockbaseConfiguration
24
     */
25
    private $config;
26
    /**
27
     * @var ImagesHelper
28
     */
29
    private $imagesHelper;
30
    /**
31
     * @var StockbaseClientFactory
32
     */
33
    private $stockbaseClientFactory;
34
35
    /**
36
     * ProductSaveAfterObserver constructor.
37
     * @param LoggerInterface        $logger
38
     * @param StockbaseConfiguration $config
39
     * @param ImagesHelper           $imagesHelper
40
     * @param StockbaseClientFactory $stockbaseClientFactory
41
     */
42
    public function __construct(
43
        LoggerInterface $logger,
44
        StockbaseConfiguration $config,
45
        ImagesHelper $imagesHelper,
46
        StockbaseClientFactory $stockbaseClientFactory
47
    ) {
48
        $this->logger = $logger;
49
        $this->config = $config;
50
        $this->imagesHelper = $imagesHelper;
51
        $this->stockbaseClientFactory = $stockbaseClientFactory;
52
    }
53
54
    /**
55
     * @param EventObserver $observer
56
     */
57
    public function execute(EventObserver $observer)
58
    {
59
        /** @var \Magento\Catalog\Model\Product */
60
        $product = $observer->getEvent()->getProduct();
61
        // validate configuration:
62
        if (!$this->config->isModuleEnabled() || !$this->config->isImagesSyncEnabled()) {
63
            return;
64
        }
65
        // get ean attribute:
66
        $attribute = $this->config->getEanFieldName();
67
        // validate attribute:
68
        if ($attribute) {
69
            // get ean:
70
            $ean = $product->getData($attribute);
71
            // if the ean is not empty:
72
            if ($ean) {
73
                $this->logger->debug('Image Sync in Product Save process. EAN found: '.$ean);
74
                $client = $this->stockbaseClientFactory->create();
75
                $images = $client->getImages([$ean]);
76
                // validate returned images:
77 View Code Duplication
                if (is_array($images->{'Items'}) && count($images->{'Items'}) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
78
                    // download and save the images locally:
79
                    $this->imagesHelper->saveProductImages($images->{'Items'}, $client);
80
                    $this->logger->debug('New images synchronized.');
81
                }
82
            }
83
        }
84
    }
85
}
86