Completed
Pull Request — master (#1)
by
unknown
26:00 queued 11:02
created

Images::getImagesEans()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Stockbase\Integration\Cron;
4
5
use Magento\Framework\ObjectManagerInterface;
6
use Psr\Log\LoggerInterface;
7
use Stockbase\Integration\StockbaseApi\Client\StockbaseClientFactory;
8
use Stockbase\Integration\Model\Config\StockbaseConfiguration;
9
use Stockbase\Integration\Model\ResourceModel\StockItem as StockItemResource;
10
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollection;
11
use Magento\Catalog\Model\Product as ProductModel;
12
use Magento\Framework\Url as UrlHelper;
13
use Magento\Framework\App\Filesystem\DirectoryList;
14
use Magento\Framework\Filesystem\Io\File;
15
16
/**
17
 * Stockbase images synchronization cron job.
18
 */
19
class Images
20
{
21
22
    /**
23
     * @var LoggerInterface
24
     */
25
    private $logger;
26
    /**
27
     * @var StockbaseClientFactory
28
     */
29
    private $stockbaseClientFactory;
30
    /**
31
     * @var ObjectManagerInterface
32
     */
33
    private $objectManager;
34
    /**
35
     * @var StockbaseConfiguration
36
     */
37
    private $config;
38
    /**
39
     * @var ProductCollection
40
     */
41
    private $productCollection;
42
    /**
43
     * @var ProductModel
44
     */
45
    private $product;
46
    /**
47
     * @var UrlHelper
48
     */
49
    private $urlHelper;
50
    /**
51
     * Directory List
52
     *
53
     * @var DirectoryList
54
     */
55
    private $directoryList;
56
    /**
57
     * File interface
58
     *
59
     * @var File
60
     */
61
    private $file;
62
    
63
    /**
64
     * Images constructor.
65
     * @param LoggerInterface $logger
66
     * @param ObjectManagerInterface $objectManager
67
     * @param StockbaseClientFactory $stockbaseClientFactory
68
     * @param StockbaseConfiguration $config
69
     * @param ProductCollection $productCollection
70
     * @param ProductModel $product
71
     * @param UrlHelper $urlHelper
72
     * @param DirectoryList $directoryList
73
     * @param File $file
74
     */
75
    public function __construct(
76
        LoggerInterface $logger,
77
        ObjectManagerInterface $objectManager,
78
        StockbaseClientFactory $stockbaseClientFactory,
79
        StockbaseConfiguration $config,
80
        ProductCollection $productCollection,
81
        ProductModel $product,
82
        UrlHelper $urlHelper,
83
        DirectoryList $directoryList,
84
        File $file
85
    ) {
86
        $this->logger = $logger;
87
        $this->stockbaseClientFactory = $stockbaseClientFactory;
88
        $this->objectManager = $objectManager;
89
        $this->config = $config;
90
        $this->productCollection = $productCollection;
91
        $this->product = $product;
92
        $this->urlHelper = $urlHelper;
93
        $this->directoryList = $directoryList;
94
        $this->file = $file;
95
    }
96
97
    /**
98
     * Executes the job.
99
     */
100
    public function execute()
101
    {
102
        // validate configuration:
103
        if (!$this->config->isModuleEnabled() || !$this->config->isImagesSyncEnabled()) {
104
            return;
105
        }
106
        // start process:
107
        $this->logger->info('Synchronizing Stockbase images...');
108
        // get all the eans:
109
        $allEans = $this->getAllEans();
110
        // get processed eans:
111
        $processedEans = json_decode($this->getImagesEans()) ?: array();
112
        // process 100 unprocessed eans at a time:
113
        $eans = array_slice(array_diff($allEans, $processedEans), 0, 100);
114
        try {
115
            $client = $this->stockbaseClientFactory->create();
116
            $images = $client->getImages($eans);
117
        } 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...
118
            $this->logger->info('Cron runImageImport error: '.$e->getMessage());
119
            return false;
120
        }
121
        // download and save the images locally:
122
        $this->saveImageForProduct($images);
0 ignored issues
show
Documentation introduced by
$images is of type object, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
123
        // update the processed images configuration:
124
        $processedEans = array_merge($processedEans, $eans);
125
        $encodedEans = json_encode($processedEans);
126
        $this->saveImagesEans($encodedEans);
127
        $this->logger->info('Stockbase images synchronization complete.');
128
    }
129
130
    /**
131
     * @return array
132
     */
133
    private function getAllEans()
134
    {
135
        $this->logger->info('Get All EANs process');
136
        // get ean attribute:
137
        $attribute = $this->config->getEanFieldName();
138
        if($attribute) {
139
            // apply filters and paginate by 100:
140
            $collection = $this->productCollection->create()
141
                ->addAttributeToSelect($attribute)
142
                ->addAttributeToFilter($attribute, array('notnull' => true, 'neq' => '')) // not null and not empty.
143
                ->setPageSize(100);
144
            // iterate over the pages:
145
            $eans = array();
146
            $currentPage = 0;
147
            $lastPage = $collection->getLastPageNumber();
148
            while ($currentPage < $lastPage) {
149
                // load the data of this page in a single query:
150
                $collection->setCurPage(++$currentPage);
151
                $collection->load();
152
                // iterate over the products:
153
                foreach ($collection as $product) {
154
                    $ean = $product->getData($attribute);
155
                    if ($ean) {
156
                        // add the ean if this product has one:
157
                        $eans[] = $ean;
158
                    }
159
                }
160
            }
161
            $this->logger->info('Found EANs: '.count($eans));
162
            return $eans;
163
        } else {
164
            $this->logger->info('Please setup the EAN attribute.');
165
        }
166
    }
167
168
    /**
169
     * Saves images array from stockbase for given $ean
170
     *
171
     * @param array $images
172
     *
173
     * @return bool
174
     */
175
    private function saveImageForProduct($images)
176
    {
177
        $this->logger->info('Save images process: ');
178
        // get product model:
179
        $productModel = $this->product;
180
        // get ean attribute:
181
        $eanField = $this->config->getEanFieldName();
182
        // get client:
183
        $client = $this->stockbaseClientFactory->create();
184
        // loop images:
185
        foreach ($images as $image) {
186
            $this->logger->info($image->{'Url'});
187
            // load product by ean:
188
            $product = $productModel->loadByAttribute($eanField, $image->EAN);
189
            // continue looping if we do not have product:
190
            if (!$product) {
191
                continue;
192
            }
193
            // get image from stockbase:
194
            $protectedImage = $client->getImageFile($image->{'Url'});
195
            // create temporal folder if it is not exists:
196
            $tmpDir = $this->getMediaDirTmpDir();
197
            $this->file->checkAndCreateFolder($tmpDir);
198
            // get new file path:
199
            $newFileName = $tmpDir . baseName($image->{'Url'});
200
            // read file from URL and copy it to the new destination:
201
            $result = $this->file->read($protectedImage, $newFileName);
202
            if ($result) {
203
                if ($product->getMediaGallery() == null) {
204
                    $product->setMediaGallery(array('images' => array(), 'values' => array()));
205
                }
206
                // add saved file to the $product gallery:
207
                $product->addImageToMediaGallery(
208
                    $newFileName,
209
                    array('image', 'small_image', 'thumbnail'),
210
                    false,
211
                    false
212
                );
213
                // save product:
214
                $product->{'save'}();
215
                $this->logger->info('Saved.');
216
            }
217
        }
218
        return true;
219
    }
220
221
    /**
222
     * Media directory name for the temporary file storage
223
     * pub/media/tmp
224
     *
225
     * @return string
226
     */
227
    private function getMediaDirTmpDir()
228
    {
229
        return $this->directoryList->getPath(DirectoryList::MEDIA) . DIRECTORY_SEPARATOR . 'tmp';
230
    }
231
232
    /**
233
     * @return mixed
234
     */
235
    private function getImagesEans()
236
    {
237
        return $this->config->getImagesEans();
238
    }
239
240
    /**
241
     * @param $eans
242
     */
243
    private function saveImagesEans($eans)
244
    {
245
        $this->config->saveImagesEans($eans);
246
    }
247
248
}
249