ProductRepository::addProductConcrete()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
/**
4
 * This file is part of the Spryker Commerce OS.
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types = 1);
9
10
namespace Pyz\Zed\DataImport\Business\Model\Product\Repository;
11
12
use Generated\Shared\Transfer\PaginationTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PaginationTransfer 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\Product\Persistence\Map\SpyProductAbstractTableMap;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Product\Persiste...ProductAbstractTableMap 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 Orm\Zed\Product\Persistence\Map\SpyProductTableMap;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Product\Persistence\Map\SpyProductTableMap 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 Orm\Zed\Product\Persistence\SpyProduct;
16
use Orm\Zed\Product\Persistence\SpyProductAbstract;
17
use Orm\Zed\Product\Persistence\SpyProductAbstractQuery;
18
use Orm\Zed\Product\Persistence\SpyProductQuery;
19
use Propel\Runtime\Collection\ArrayCollection;
20
use Pyz\Zed\DataImport\Business\Exception\EntityNotFoundException;
21
22
class ProductRepository implements ProductRepositoryInterface
23
{
24
    public const ID_PRODUCT = 'idProduct';
25
26
    public const ID_PRODUCT_ABSTRACT = 'idProductAbstract';
27
28
    public const ABSTRACT_SKU = 'abstractSku';
29
30
    /**
31
     * @var array<string, array<string, mixed>>
32
     */
33
    protected static array $resolved = [];
34
35
    public function getIdProductByConcreteSku(string $sku): int
36
    {
37
        if (!isset(static::$resolved[$sku])) {
38
            $this->resolveProductByConcreteSku($sku);
39
        }
40
41
        return static::$resolved[$sku][static::ID_PRODUCT];
42
    }
43
44
    public function getAbstractSkuByConcreteSku(string $sku): string
45
    {
46
        if (!isset(static::$resolved[$sku])) {
47
            $this->resolveProductByConcreteSku($sku);
48
        }
49
50
        return static::$resolved[$sku][static::ABSTRACT_SKU];
51
    }
52
53
    public function getIdProductAbstractByAbstractSku(string $sku): int
54
    {
55
        if (!isset(static::$resolved[$sku])) {
56
            $this->resolveProductByAbstractSku($sku);
57
        }
58
59
        return static::$resolved[$sku][static::ID_PRODUCT_ABSTRACT];
60
    }
61
62
    public function getProductConcreteAttributesCollection(PaginationTransfer $paginationTransfer): ArrayCollection
63
    {
64
        $productQuery = SpyProductQuery::create()
65
            ->joinWithSpyProductAbstract()
66
            ->select([SpyProductTableMap::COL_ATTRIBUTES, SpyProductTableMap::COL_SKU, SpyProductAbstractTableMap::COL_SKU]);
67
68
        $productQuery = $this->applyPagination($productQuery, $paginationTransfer);
69
70
        /** @phpstan-var \Propel\Runtime\Collection\ArrayCollection */
71
        return $productQuery->find();
72
    }
73
74
    /**
75
     * @param string $sku
76
     *
77
     * @throws \Pyz\Zed\DataImport\Business\Exception\EntityNotFoundException
78
     */
79
    private function resolveProductByConcreteSku(string $sku): void
80
    {
81
        $productEntity = SpyProductQuery::create()
82
            ->joinWithSpyProductAbstract()
83
            ->findOneBySku($sku);
84
85
        if (!$productEntity) {
86
            throw new EntityNotFoundException(sprintf('Concrete product by sku "%s" not found.', $sku));
87
        }
88
89
        static::$resolved[$sku] = [
90
            static::ID_PRODUCT => $productEntity->getIdProduct(),
91
            static::ABSTRACT_SKU => $productEntity->getSpyProductAbstract()->getSku(),
92
        ];
93
    }
94
95
    /**
96
     * @param string $sku
97
     *
98
     * @throws \Pyz\Zed\DataImport\Business\Exception\EntityNotFoundException
99
     */
100
    private function resolveProductByAbstractSku(string $sku): void
101
    {
102
        $productAbstractEntity = SpyProductAbstractQuery::create()
103
            ->findOneBySku($sku);
104
105
        if (!$productAbstractEntity) {
106
            throw new EntityNotFoundException(sprintf('Abstract product by sku "%s" not found.', $sku));
107
        }
108
109
        static::$resolved[$sku] = [
110
            static::ID_PRODUCT_ABSTRACT => $productAbstractEntity->getIdProductAbstract(),
111
        ];
112
    }
113
114
    public function addProductAbstract(SpyProductAbstract $productAbstractEntity): void
115
    {
116
        static::$resolved[$productAbstractEntity->getSku()] = [
117
            static::ID_PRODUCT_ABSTRACT => $productAbstractEntity->getIdProductAbstract(),
118
        ];
119
    }
120
121
    public function addProductConcrete(SpyProduct $productEntity, ?string $abstractSku = null): void
122
    {
123
        static::$resolved[$productEntity->getSku()] = [
124
            static::ID_PRODUCT => $productEntity->getIdProduct(),
125
            static::ABSTRACT_SKU => ($abstractSku) ? $abstractSku : $productEntity->getSpyProductAbstract()->getSku(),
126
        ];
127
    }
128
129
    /**
130
     * @return array<string>
131
     */
132
    public function getSkuProductAbstractList(): array
133
    {
134
        /** @var \Propel\Runtime\Collection\ArrayCollection $productAbstractEntities */
135
        $productAbstractEntities = SpyProductAbstractQuery::create()
136
            ->select([SpyProductAbstractTableMap::COL_SKU])
137
            ->find();
138
139
        return $productAbstractEntities->toArray();
140
    }
141
142
    /**
143
     * @return array<string>
144
     */
145
    public function getSkuProductConcreteList(): array
146
    {
147
        /** @var \Propel\Runtime\Collection\ArrayCollection $productEntities */
148
        $productEntities = SpyProductQuery::create()
149
            ->select([SpyProductTableMap::COL_SKU])
150
            ->find();
151
152
        return $productEntities->toArray();
153
    }
154
155
    public function flush(): void
156
    {
157
        static::$resolved = [];
158
    }
159
160
    protected function applyPagination(SpyProductQuery $productQuery, PaginationTransfer $paginationTransfer): SpyProductQuery
161
    {
162
        if ($paginationTransfer->getOffset() === null || $paginationTransfer->getLimit() === null) {
163
            return $productQuery;
164
        }
165
166
        return $productQuery
167
            ->offset($paginationTransfer->getOffsetOrFail())
168
            ->setLimit($paginationTransfer->getLimitOrFail());
169
    }
170
}
171