Completed
Push — master ( b8dae5...46c150 )
by Gabriel
01:13
created

Images   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 147
Duplicated Lines 3.4 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 6
Bugs 0 Features 0
Metric Value
c 6
b 0
f 0
dl 5
loc 147
rs 10
wmc 14
lcom 1
cbo 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
C execute() 5 28 7
B getEansToProcess() 0 37 4
A getProductEan() 0 12 2

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\Cron;
4
5
use Psr\Log\LoggerInterface;
6
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollection;
7
use Stockbase\Integration\StockbaseApi\Client\StockbaseClientFactory;
8
use Stockbase\Integration\Model\Config\StockbaseConfiguration;
9
use Stockbase\Integration\Model\ResourceModel\ProductImage as ProductImageResource;
10
use Stockbase\Integration\Helper\Images as ImagesHelper;
11
12
/**
13
 * Stockbase images synchronization cron job.
14
 */
15
class Images
16
{
17
18
    /**
19
     * @var LoggerInterface
20
     */
21
    private $logger;
22
    /**
23
     * @var StockbaseClientFactory
24
     */
25
    private $stockbaseClientFactory;
26
    /**
27
     * @var StockbaseConfiguration
28
     */
29
    private $config;
30
    /**
31
     * @var ProductImageResource
32
     */
33
    private $productImageResource;
34
    /**
35
     * @var ImagesHelper
36
     */
37
    private $imagesHelper;
38
    /**
39
     * @var ProductCollection
40
     */
41
    private $productCollection;
42
    /**
43
     * @var array
44
     */
45
    private $eans = array();
46
    
47
    /**
48
     * Images constructor.
49
     * @param LoggerInterface $logger
50
     * @param StockbaseClientFactory $stockbaseClientFactory
51
     * @param StockbaseConfiguration $config
52
     * @param ProductCollection $productCollection
53
     * @param ProductImageResource $productImageResource
54
     * @param ImagesHelper $imagesHelper
55
     */
56
    public function __construct(
57
        LoggerInterface $logger,
58
        StockbaseClientFactory $stockbaseClientFactory,
59
        StockbaseConfiguration $config,
60
        ProductCollection $productCollection,
61
        ProductImageResource $productImageResource,
62
        ImagesHelper $imagesHelper
63
    ) {
64
        $this->logger = $logger;
65
        $this->stockbaseClientFactory = $stockbaseClientFactory;
66
        $this->config = $config;
67
        $this->productCollection = $productCollection;
68
        $this->productImageResource = $productImageResource;
69
        $this->imagesHelper = $imagesHelper;
70
    }
71
72
    /**
73
     * Executes the job.
74
     */
75
    public function execute()
76
    {
77
        // validate configuration:
78
        if (!$this->config->isModuleEnabled() || !$this->config->isImagesSyncCronEnabled()) {
79
            return;
80
        }
81
        // start process:
82
        $this->logger->info('Synchronizing Stockbase images...');
83
        // get all the eans:
84
        $eans = $this->getEansToProcess();
85
        try {
86
            // if still need to process eans:
87
            if(count($eans) > 0) {
88
                $client = $this->stockbaseClientFactory->create();
89
                $images = $client->getImages($eans);
90
                // validate returned images:
91 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...
92
                    // download and save the images locally:
93
                    $newImagesCount = $this->imagesHelper->saveProductImages($images->{'Items'});
94
                    $this->logger->info('New synchronized images: '.$newImagesCount);
95
                }
96
            }
97
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class Stockbase\Integration\Cron\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
98
            $this->logger->info('Cron runImageImport error: '.$e->getMessage());
99
            return false;
100
        }
101
        $this->logger->info('Stockbase images synchronization complete.');
102
    }
103
104
    /**
105
     * @return array
106
     */
107
    private function getEansToProcess()
108
    {
109
        // clean eans list:
110
        $this->eans = array();
111
        // start process:
112
        $this->logger->info('Get All EANs');
113
        // get ean attribute:
114
        $attribute = $this->config->getEanFieldName();
115
        // validate attribute:
116
        if($attribute) {
117
            // create collection and apply filters:
118
            $collection = $this->productCollection->create()
119
                ->addAttributeToSelect($attribute)
120
                ->addAttributeToSelect('stockbase_product')
121
                ->addAttributeToFilter('stockbase_product', array('eq' => '1')) // only stockbase products
122
                ->addAttributeToFilter($attribute, array('notnull' => true, 'neq' => '')) // not null and not empty ean
123
                ;
124
            // if the filter is active then exclude the eans processed:
125
            if($this->config->filterProcessedProducts()) {
126
                // get eans list:
127
                $processedEans = $this->productImageResource->getProcessedEans();
128
                // add eans filter:
129
                if(count($processedEans) > 0) {
130
                    $this->logger->info('Filtered EANs: '.count($processedEans));
131
                    $collection->addAttributeToFilter($attribute, array('nin' => $processedEans));
132
                }
133
            }
134
            // walk collection and save eans in the object:
135
            $collection->walk(array($this, 'getProductEan'));
136
            // log eans count:
137
            $this->logger->info('EANs to process: '.count($this->eans));
138
        } else {
139
            // missing ean attribute:
140
            $this->logger->info('Please setup the EAN attribute.');
141
        }
142
        return $this->eans;
143
    }
144
145
    /**
146
     * @param $product
147
     */
148
    public function getProductEan($product)
149
    {
150
        // get ean attribute:
151
        $attribute = $this->config->getEanFieldName();
152
        // get ean:
153
        $ean = $product->getData($attribute);
154
        // if the ean is not empty:
155
        if ($ean) {
156
            // add the ean if this product has one:
157
            $this->eans[] = $ean;
158
        }
159
    }
160
161
}
162