FactFinderSdkCategoryExpander   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
eloc 33
c 2
b 0
f 0
dl 0
loc 93
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addCategoryPath() 0 14 3
A getCategoryPath() 0 19 3
A expand() 0 6 1
A getCategoryPathArray() 0 17 3
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\FactFinderSdk\Business\Expander;
9
10
use Generated\Shared\Transfer\CurrencyTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\CurrencyTransfer 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\StoreTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\StoreTransfer 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 SprykerEco\Shared\FactFinderSdk\FactFinderSdkConstants;
14
15
class FactFinderSdkCategoryExpander extends FactFinderSdkAbstractExpander
16
{
17
    public const VIRTUAL_COLUMN_NAME = 'name';
18
19
    /**
20
     * @param \Generated\Shared\Transfer\LocaleTransfer $localeTransfer
21
     * @param \Generated\Shared\Transfer\CurrencyTransfer $currencyTransfer
22
     * @param \Generated\Shared\Transfer\StoreTransfer $storeTransfer
23
     * @param array $productData
24
     *
25
     * @return array
26
     */
27
    public function expand(LocaleTransfer $localeTransfer, CurrencyTransfer $currencyTransfer, StoreTransfer $storeTransfer, $productData)
28
    {
29
        $categoryPathArray = $this->getCategoryPathArray($localeTransfer, $productData[FactFinderSdkConstants::ITEM_ID_ABSTRACT_PRODUCT]);
30
        $productData = $this->addCategoryPath($productData, $categoryPathArray);
31
32
        return $productData;
33
    }
34
35
    /**
36
     * @param array $data
37
     * @param array $categoriesPathArray
38
     *
39
     * @return array
40
     */
41
    protected function addCategoryPath($data, $categoriesPathArray)
42
    {
43
        if (empty($data[FactFinderSdkConstants::ITEM_CATEGORY_PATH])) {
44
            $data[FactFinderSdkConstants::ITEM_CATEGORY_PATH] = '';
45
        }
46
47
        foreach ($categoriesPathArray as $key => $pathArray) {
48
            $pathArray = array_map(function ($value) {
49
                return urlencode($value);
50
            }, $pathArray);
51
            $data[FactFinderSdkConstants::ITEM_CATEGORY_PATH] .= implode('/', $pathArray) . '|';
52
        }
53
54
        return $data;
55
    }
56
57
    /**
58
     * @param \Generated\Shared\Transfer\LocaleTransfer $localeTransfer
59
     * @param int $idProductAbstract
60
     *
61
     * @return array
62
     */
63
    protected function getCategoryPathArray(LocaleTransfer $localeTransfer, $idProductAbstract)
64
    {
65
        $pathArray = [];
66
67
        $query = $this->queryContainer
68
            ->getCategoriesQuery($localeTransfer, $idProductAbstract);
69
        $categories = $query->find();
70
71
        if (!count($categories)) {
72
            return $pathArray;
73
        }
74
75
        foreach ($categories as $category) {
76
            $pathArray[] = $this->getCategoryPath($localeTransfer, $category);
77
        }
78
79
        return $pathArray;
80
    }
81
82
    /**
83
     * @param \Generated\Shared\Transfer\LocaleTransfer $localeTransfer
84
     * @param \Orm\Zed\Category\Persistence\SpyCategory $category
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Category\Persistence\SpyCategory 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...
85
     * @param array $path
86
     *
87
     * @return string[]
88
     */
89
    protected function getCategoryPath(LocaleTransfer $localeTransfer, $category, $path = [])
90
    {
91
        /** @var \Orm\Zed\Category\Persistence\SpyCategoryNode $node */
92
        $node = $category->getNodes()
93
            ->getFirst();
94
95
        if ($node->getFkParentCategoryNode()) {
96
            $parentCategory = $this->queryContainer
97
                ->getCategoryQuery($node->getParentCategoryNode()->getFkCategory(), $localeTransfer)
98
                ->find()
99
                ->getFirst();
100
            $path = array_merge($path, $this->getCategoryPath($localeTransfer, $parentCategory, $path));
101
        }
102
103
        if ($category->isSearchable()) {
104
            $path[] = $category->getVirtualColumn(static::VIRTUAL_COLUMN_NAME);
105
        }
106
107
        return $path;
108
    }
109
}
110