Images::getProductEan()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 12
ccs 0
cts 6
cp 0
crap 6
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Stockbase\Integration\Cron;
4
5
use Magento\Catalog\Model\Product;
6
use Psr\Log\LoggerInterface;
7
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollection;
8
use Stockbase\Integration\StockbaseApi\Client\StockbaseClientFactory;
9
use Stockbase\Integration\Model\Config\StockbaseConfiguration;
10
use Stockbase\Integration\Model\ResourceModel\ProductImage as ProductImageResource;
11
use Stockbase\Integration\Helper\Images as ImagesHelper;
12
13
/**
14
 * Stockbase images synchronization cron job.
15
 */
16
class Images
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'}, $client);
94
                    $this->logger->info('New synchronized images: '.$newImagesCount);
95
                }
96
            }
97
        } catch (\Exception $e) {
98
            $this->logger->info('Cron runImageImport error: '.$e->getMessage());
99
            
100
            return;
101
        }
102
        $this->logger->info('Stockbase images synchronization complete.');
103
    }
104
105
    /**
106
     * @param Product $product
107
     */
108
    public function getProductEan($product)
109
    {
110
        // get ean attribute:
111
        $attribute = $this->config->getEanFieldName();
112
        // get ean:
113
        $ean = $product->getData($attribute);
114
        // if the ean is not empty:
115
        if ($ean) {
116
            // add the ean if this product has one:
117
            $this->eans[] = $ean;
118
        }
119
    }
120
121
    /**
122
     * @return array
123
     */
124
    private function getEansToProcess()
125
    {
126
        // clean eans list:
127
        $this->eans = array();
128
        // start process:
129
        $this->logger->info('Get All EANs');
130
        // get ean attribute:
131
        $attribute = $this->config->getEanFieldName();
132
        // validate attribute:
133
        if ($attribute) {
134
            // create collection and apply filters:
135
            $collection = $this->productCollection->create()
136
                ->addAttributeToSelect($attribute)
137
                ->addAttributeToSelect('stockbase_product')
138
                ->addAttributeToFilter('stockbase_product', array('eq' => '1')) // only stockbase products
139
                ->addAttributeToFilter($attribute, array('notnull' => true, 'neq' => '')) // not null and not empty ean
140
                ;
141
            // if the filter is active then exclude the eans processed:
142
            if ($this->config->filterProcessedProducts()) {
143
                // get eans list:
144
                $processedEans = $this->productImageResource->getProcessedEans();
145
                // add eans filter:
146
                if (count($processedEans) > 0) {
147
                    $this->logger->info('Filtered EANs: '.count($processedEans));
148
                    $collection->addAttributeToFilter($attribute, array('nin' => $processedEans));
149
                }
150
            }
151
            // walk collection and save eans in the object:
152
            $collection->walk(array($this, 'getProductEan'));
153
            // log eans count:
154
            $this->logger->info('EANs to process: '.count($this->eans));
155
        } else {
156
            // missing ean attribute:
157
            $this->logger->info('Please setup the EAN attribute.');
158
        }
159
        
160
        return $this->eans;
161
    }
162
}
163