Passed
Branch development (0519a2)
by Theodoros
02:02
created

EcondaProductCollector   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 272
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 272
rs 10
c 0
b 0
f 0
wmc 21

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getPriceBySku() 0 3 1
A getDefaultImageSet() 0 7 3
A collectItem() 0 13 1
A getSmallPictureUrlFromDefaultImageSet() 0 9 2
A getProductCategoryMappings() 0 12 1
A getSmallImageUrl() 0 6 2
B generateProductConcreteImageSets() 0 27 3
A getImageUrlFromItemData() 0 8 1
A generateCategories() 0 16 3
A collectResourceType() 0 3 1
A __construct() 0 16 1
A collectData() 0 9 2
1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\Econda\Business\Collector\File;
9
10
use Everon\Component\Collection\Collection;
0 ignored issues
show
Bug introduced by
The type Everon\Component\Collection\Collection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Generated\Shared\Transfer\LocaleTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\LocaleTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Generated\Shared\Transfer\StorageProductImageTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ageProductImageTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Orm\Zed\Category\Persistence\Map\SpyCategoryTableMap;
1 ignored issue
show
Bug introduced by
The type Orm\Zed\Category\Persist...Map\SpyCategoryTableMap was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Propel\Runtime\ActiveQuery\Criteria;
0 ignored issues
show
Bug introduced by
The type Propel\Runtime\ActiveQuery\Criteria was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Spryker\Shared\SqlCriteriaBuilder\CriteriaBuilder\CriteriaBuilderInterface;
0 ignored issues
show
Bug introduced by
The type Spryker\Shared\SqlCriter...riteriaBuilderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Spryker\Zed\ProductCategory\Persistence\ProductCategoryQueryContainerInterface;
0 ignored issues
show
Bug introduced by
The type Spryker\Zed\ProductCateg...QueryContainerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Spryker\Zed\ProductImage\Persistence\ProductImageQueryContainerInterface;
0 ignored issues
show
Bug introduced by
The type Spryker\Zed\ProductImage...QueryContainerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use SprykerEco\Zed\Econda\Business\Collector\AbstractDatabaseCollector;
19
use SprykerEco\Zed\Econda\Dependency\Facade\EcondaToPriceBridgeInterface;
20
use SprykerEco\Zed\Econda\EcondaConfig;
21
use SprykerEco\Zed\Econda\Persistence\Econda\AbstractPdoEcondaQuery;
22
23
class EcondaProductCollector extends AbstractDatabaseCollector
24
{
25
26
    //CSV file columns
27
    const ID_COLUMN = 'ID';
28
    const NAME_COLUMN = 'Name';
29
    const DESCRIPTION_COLUMN = 'Description';
30
    const PRODUCTURL_COLUMN = 'PRODUCTURL';
31
    const IMAGE_URL_COLUMN = 'ImageURL';
32
    const PRICE_COLUMN = 'Price';
33
    const STOCK_COLUMN = 'Stock';
34
    const PRODUCT_CATEGORY_COLUMN = 'ProductCategory';
35
36
    //internal query fields
37
    const ID_PRODUCT_ABSTRACT = 'id_product_abstract';
38
    const SKU = 'sku';
39
    const URL = 'url';
40
    const NAME = 'name';
41
    const DESCRIPTION = 'description';
42
    const META_DESCRIPTION = 'meta_description';
43
    const QUANTITY = 'quantity';
44
    const ID_PRODUCT_CONCRETE = 'id_product';
45
    const DEFAULT_QUERY_FIELD = 'default';
46
    const EXTERNAL_URL_SMALL_QUERY_FIELD = 'externalUrlSmall';
47
48
    const PRODUCTS = 'products';
49
50
    /**
51
     * @var \SprykerEco\Zed\Econda\EcondaConfig
52
     */
53
    protected $config;
54
55
    /**
56
     * @var \Spryker\Zed\ProductCategory\Persistence\ProductCategoryQueryContainerInterface
57
     */
58
    protected $productCategoryQueryContainer;
59
60
    /**
61
     * @var \Spryker\Zed\Price\Business\PriceFacadeInterface
0 ignored issues
show
Bug introduced by
The type Spryker\Zed\Price\Business\PriceFacadeInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
62
     */
63
    protected $priceFacade;
64
65
    /**
66
     * @var \Everon\Component\Collection\CollectionInterface
0 ignored issues
show
Bug introduced by
The type Everon\Component\Collection\CollectionInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
67
     */
68
    protected $categoryCacheCollection;
69
70
    /**
71
     * @var \Spryker\Zed\ProductImage\Persistence\ProductImageQueryContainerInterface
72
     */
73
    protected $productImageQueryContainer;
74
75
    /**
76
     * @var array
77
     */
78
    protected $superAttributes;
79
80
    /**
81
     * @param \Spryker\Shared\SqlCriteriaBuilder\CriteriaBuilder\CriteriaBuilderInterface $criteria
82
     * @param \SprykerEco\Zed\Econda\Persistence\Econda\AbstractPdoEcondaQuery $query
83
     * @param \Spryker\Zed\ProductCategory\Persistence\ProductCategoryQueryContainerInterface $productCategoryQueryContainer
84
     * @param \Spryker\Zed\ProductImage\Persistence\ProductImageQueryContainerInterface $productImageQueryContainer
85
     * @param \SprykerEco\Zed\Econda\Dependency\Facade\EcondaToPriceBridgeInterface|\Spryker\Zed\Price\Business\PriceFacadeInterface $priceFacade
86
     * @param \SprykerEco\Zed\Econda\EcondaConfig $config
87
     */
88
    public function __construct(
89
        CriteriaBuilderInterface $criteria,
90
        AbstractPdoEcondaQuery $query,
91
        ProductCategoryQueryContainerInterface $productCategoryQueryContainer,
92
        ProductImageQueryContainerInterface $productImageQueryContainer,
93
        EcondaToPriceBridgeInterface $priceFacade,
94
        EcondaConfig $config
95
    ) {
96
97
        parent::__construct($criteria, $query);
98
99
        $this->productCategoryQueryContainer = $productCategoryQueryContainer;
100
        $this->productImageQueryContainer = $productImageQueryContainer;
101
        $this->priceFacade = $priceFacade;
0 ignored issues
show
Documentation Bug introduced by
It seems like $priceFacade of type SprykerEco\Zed\Econda\De...aToPriceBridgeInterface is incompatible with the declared type Spryker\Zed\Price\Business\PriceFacadeInterface of property $priceFacade.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
102
        $this->categoryCacheCollection = new Collection([]);
103
        $this->config = $config;
104
    }
105
106
    /**
107
     * @param array $collectedSet
108
     * @param \Generated\Shared\Transfer\LocaleTransfer $locale
109
     *
110
     * @return array
111
     */
112
    protected function collectData(array $collectedSet, LocaleTransfer $locale)
113
    {
114
        $setToExport = [];
115
116
        foreach ($collectedSet as $index => $collectedItemData) {
117
            $setToExport[] = $this->collectItem($collectedItemData);
118
        }
119
120
        return $setToExport;
121
    }
122
123
    /**
124
     * @param array $collectItemData
125
     *
126
     * @return array
127
     */
128
    protected function collectItem(array $collectItemData)
129
    {
130
        $imageUrl = $this->getImageUrlFromItemData($collectItemData);
131
132
        return [
133
            self::ID_COLUMN => $collectItemData[self::SKU],
134
            self::NAME_COLUMN => $collectItemData[self::NAME],
135
            self::DESCRIPTION_COLUMN => $collectItemData[self::META_DESCRIPTION],
136
            self::PRODUCTURL_COLUMN => $this->config->getHostYves() . $collectItemData[self::URL],
137
            self::IMAGE_URL_COLUMN => $imageUrl,
138
            self::PRICE_COLUMN => $this->getPriceBySku($collectItemData[self::SKU]),
139
            self::STOCK_COLUMN => (int)$collectItemData[self::QUANTITY],
140
            self::PRODUCT_CATEGORY_COLUMN => implode(EcondaConfig::ECONDA_CSV_CATEGORY_DELIMITER, $this->generateCategories($collectItemData[self::ID_PRODUCT_ABSTRACT])),
141
        ];
142
    }
143
144
    /**
145
     * @return string
146
     */
147
    protected function collectResourceType()
148
    {
149
        return self::PRODUCTS;
150
    }
151
152
    /**
153
     * @param string $sku
154
     *
155
     * @return float
156
     */
157
    protected function getPriceBySku($sku)
158
    {
159
        return $this->priceFacade->getPriceBySku($sku) / 100;
160
    }
161
162
    /**
163
     * @param int $idProductAbstract
164
     *
165
     * @return array
166
     */
167
    protected function generateCategories($idProductAbstract)
168
    {
169
        if ($this->categoryCacheCollection->has($idProductAbstract)) {
170
            return $this->categoryCacheCollection->get($idProductAbstract);
171
        }
172
173
        $productCategoryMappings = $this->getProductCategoryMappings($idProductAbstract);
174
175
        $categories = [];
176
        foreach ($productCategoryMappings as $mapping) {
177
            $categories[] = $mapping->getSpyCategory()->getIdCategory();
178
        }
179
180
        $this->categoryCacheCollection->set($idProductAbstract, $categories);
181
182
        return $categories;
183
    }
184
185
    /**
186
     * @param int $idProductAbstract
187
     *
188
     * @return \Orm\Zed\ProductCategory\Persistence\SpyProductCategory[]|\Propel\Runtime\Collection\ObjectCollection
189
     */
190
    protected function getProductCategoryMappings($idProductAbstract)
191
    {
192
        return $this->productCategoryQueryContainer
193
            ->queryLocalizedProductCategoryMappingByIdProduct($idProductAbstract)
194
            ->innerJoinSpyCategory()
195
            ->addAnd(
196
                SpyCategoryTableMap::COL_IS_ACTIVE,
197
                true,
198
                Criteria::EQUAL
199
            )
200
            ->orderByProductOrder()
201
            ->find();
202
    }
203
204
    /**
205
     * @param int $idProductAbstract
206
     * @param int $idProductConcrete
207
     *
208
     * @return array
209
     */
210
    protected function generateProductConcreteImageSets($idProductAbstract, $idProductConcrete)
211
    {
212
        $imageSets = $this->productImageQueryContainer
213
            ->queryProductImageSet()
214
            ->filterByFkProductAbstract($idProductAbstract)
215
            ->_or()
216
            ->filterByFkProduct($idProductConcrete)
217
            ->find();
218
219
        $result = [];
220
        foreach ($imageSets as $imageSetEntity) {
221
            $result[$imageSetEntity->getName()] = [];
222
            $productsToImages = $imageSetEntity->getSpyProductImageSetToProductImages(
223
                $this->productImageQueryContainer->queryProductImageSetToProductImage()
224
                    ->orderBySortOrder(Criteria::DESC)
225
            );
226
            foreach ($productsToImages as $productToImageEntity) {
227
                $imageEntity = $productToImageEntity->getSpyProductImage();
228
                $result[$imageSetEntity->getName()][] = [
229
                    StorageProductImageTransfer::ID_PRODUCT_IMAGE => $imageEntity->getIdProductImage(),
230
                    StorageProductImageTransfer::EXTERNAL_URL_LARGE => $imageEntity->getExternalUrlLarge(),
231
                    StorageProductImageTransfer::EXTERNAL_URL_SMALL => $imageEntity->getExternalUrlSmall(),
232
                ];
233
            }
234
        }
235
236
        return $result;
237
    }
238
239
    /**
240
     * @param array $collectItemData
241
     *
242
     * @return string
243
     */
244
    protected function getImageUrlFromItemData(array $collectItemData)
245
    {
246
        $imageSet = $this->generateProductConcreteImageSets(
247
            $collectItemData[self::ID_PRODUCT_ABSTRACT],
248
            $collectItemData[self::ID_PRODUCT_CONCRETE]
249
        );
250
251
        return $this->getSmallPictureUrlFromDefaultImageSet($imageSet);
252
    }
253
254
    /**
255
     * @param array $imageSet
256
     *
257
     * @return string
258
     */
259
    protected function getSmallPictureUrlFromDefaultImageSet($imageSet)
260
    {
261
        $defaultImageSet = $this->getDefaultImageSet($imageSet);
262
263
        if (is_array($defaultImageSet[0])) {
264
            return $this->getSmallImageUrl($defaultImageSet[0]);
265
        }
266
267
        return '';
268
    }
269
270
    /**
271
     * @param array $imageSet
272
     *
273
     * @return array
274
     */
275
    protected function getDefaultImageSet($imageSet)
276
    {
277
        if (array_key_exists(self::DEFAULT_QUERY_FIELD, $imageSet) && is_array($imageSet[self::DEFAULT_QUERY_FIELD])) {
278
            return $imageSet[self::DEFAULT_QUERY_FIELD];
279
        }
280
281
        return [];
282
    }
283
284
    /**
285
     * @param array $imageArray
286
     *
287
     * @return string
288
     */
289
    protected function getSmallImageUrl($imageArray)
290
    {
291
        if (array_key_exists(self::EXTERNAL_URL_SMALL_QUERY_FIELD, $imageArray)) {
292
            return $imageArray[self::EXTERNAL_URL_SMALL_QUERY_FIELD];
293
        }
294
        return '';
295
    }
296
297
}
298