findConcreteProductPrice()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 2
dl 0
loc 22
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Yves\Coremedia\ApiResponse\Renderer;
9
10
use Generated\Shared\Transfer\CoremediaPlaceholderTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ediaPlaceholderTransfer 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\CurrentProductPriceTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...entProductPriceTransfer 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 SprykerEco\Yves\Coremedia\Dependency\Client\CoremediaToPriceProductClientInterface;
13
use SprykerEco\Yves\Coremedia\Dependency\Client\CoremediaToPriceProductStorageClientInterface;
14
use SprykerEco\Yves\Coremedia\Dependency\Client\CoremediaToProductStorageClientInterface;
15
use SprykerEco\Yves\Coremedia\Formatter\ProductPriceFormatterInterface;
16
17
class ProductPricePlaceholderReplacementRenderer implements PlaceholderReplacementRendererInterface
18
{
19
    protected const PLACEHOLDER_OBJECT_TYPE = 'product';
20
    protected const PLACEHOLDER_RENDER_TYPE = 'price';
21
22
    protected const PRODUCT_DATA_KEY_ID_PRODUCT_CONCRETE = 'id_product_concrete';
23
    protected const PRODUCT_DATA_KEY_ID_PRODUCT_ABSTRACT = 'id_product_abstract';
24
25
    protected const PRODUCT_ABSTRACT_MAPPING_TYPE = 'sku';
26
27
    /**
28
     * @var \SprykerEco\Yves\Coremedia\Dependency\Client\CoremediaToProductStorageClientInterface
29
     */
30
    protected $productStorageClient;
31
32
    /**
33
     * @var \SprykerEco\Yves\Coremedia\Dependency\Client\CoremediaToPriceProductStorageClientInterface
34
     */
35
    protected $priceProductStorageClient;
36
37
    /**
38
     * @var \SprykerEco\Yves\Coremedia\Dependency\Client\CoremediaToPriceProductClientInterface
39
     */
40
    protected $priceProductClient;
41
42
    /**
43
     * @var \SprykerEco\Yves\Coremedia\Formatter\ProductPriceFormatterInterface
44
     */
45
    protected $productPriceFormatter;
46
47
    /**
48
     * @param \SprykerEco\Yves\Coremedia\Dependency\Client\CoremediaToProductStorageClientInterface $productStorageClient
49
     * @param \SprykerEco\Yves\Coremedia\Dependency\Client\CoremediaToPriceProductStorageClientInterface $priceProductStorageClient
50
     * @param \SprykerEco\Yves\Coremedia\Dependency\Client\CoremediaToPriceProductClientInterface $priceProductClient
51
     * @param \SprykerEco\Yves\Coremedia\Formatter\ProductPriceFormatterInterface $productPriceFormatter
52
     */
53
    public function __construct(
54
        CoremediaToProductStorageClientInterface $productStorageClient,
55
        CoremediaToPriceProductStorageClientInterface $priceProductStorageClient,
56
        CoremediaToPriceProductClientInterface $priceProductClient,
57
        ProductPriceFormatterInterface $productPriceFormatter
58
    ) {
59
        $this->productStorageClient = $productStorageClient;
60
        $this->priceProductStorageClient = $priceProductStorageClient;
61
        $this->priceProductClient = $priceProductClient;
62
        $this->productPriceFormatter = $productPriceFormatter;
63
    }
64
65
    /**
66
     * @param \Generated\Shared\Transfer\CoremediaPlaceholderTransfer $coreMediaPlaceholderTransfer
67
     *
68
     * @return bool
69
     */
70
    public function isApplicable(CoremediaPlaceholderTransfer $coreMediaPlaceholderTransfer): bool
71
    {
72
        return $coreMediaPlaceholderTransfer->getObjectType() === static::PLACEHOLDER_OBJECT_TYPE &&
73
            $coreMediaPlaceholderTransfer->getRenderType() === static::PLACEHOLDER_RENDER_TYPE;
74
    }
75
76
    /**
77
     * @param \Generated\Shared\Transfer\CoremediaPlaceholderTransfer $coreMediaPlaceholderTransfer
78
     * @param string $locale
79
     *
80
     * @return string|null
81
     */
82
    public function getPlaceholderReplacement(
83
        CoremediaPlaceholderTransfer $coreMediaPlaceholderTransfer,
84
        string $locale
85
    ): ?string {
86
        if (!$coreMediaPlaceholderTransfer->getProductId()) {
87
            return null;
88
        }
89
90
        $currentProductPriceTransfer = $this->findAbstractProductPrice(
91
            $coreMediaPlaceholderTransfer,
92
            $locale
93
        );
94
95
        if ($currentProductPriceTransfer) {
96
            return $this->productPriceFormatter->getFormattedProductPrice($currentProductPriceTransfer);
97
        }
98
99
        $currentProductPriceTransfer = $this->findConcreteProductPrice(
100
            $coreMediaPlaceholderTransfer,
101
            $locale
102
        );
103
104
        if ($currentProductPriceTransfer) {
105
            return $this->productPriceFormatter->getFormattedProductPrice($currentProductPriceTransfer);
106
        }
107
108
        return null;
109
    }
110
111
    /**
112
     * @return string|null
113
     */
114
    public function getFallbackPlaceholderReplacement(): ?string
115
    {
116
        return '';
117
    }
118
119
    /**
120
     * @param \Generated\Shared\Transfer\CoremediaPlaceholderTransfer $coreMediaPlaceholderTransfer
121
     * @param string $locale
122
     *
123
     * @return \Generated\Shared\Transfer\CurrentProductPriceTransfer|null
124
     */
125
    protected function findAbstractProductPrice(
126
        CoremediaPlaceholderTransfer $coreMediaPlaceholderTransfer,
127
        string $locale
128
    ): ?CurrentProductPriceTransfer {
129
        $abstractProductData = $this->productStorageClient->findProductAbstractStorageDataByMapping(
130
            static::PRODUCT_ABSTRACT_MAPPING_TYPE,
131
            $coreMediaPlaceholderTransfer->getProductId(),
132
            $locale
133
        );
134
135
        if (!isset($abstractProductData[static::PRODUCT_DATA_KEY_ID_PRODUCT_ABSTRACT])) {
136
            return null;
137
        }
138
139
        $priceProductTransfers = $this->priceProductStorageClient
140
            ->getPriceProductAbstractTransfers($abstractProductData[static::PRODUCT_DATA_KEY_ID_PRODUCT_ABSTRACT]);
141
142
        return $this->resolveProductPriceTransfer($priceProductTransfers);
143
    }
144
145
    /**
146
     * @param \Generated\Shared\Transfer\CoremediaPlaceholderTransfer $coreMediaPlaceholderTransfer
147
     * @param string $locale
148
     *
149
     * @return \Generated\Shared\Transfer\CurrentProductPriceTransfer|null
150
     */
151
    protected function findConcreteProductPrice(
152
        CoremediaPlaceholderTransfer $coreMediaPlaceholderTransfer,
153
        string $locale
154
    ): ?CurrentProductPriceTransfer {
155
        $concreteProductData = $this->productStorageClient->findProductConcreteStorageDataByMapping(
156
            static::PRODUCT_ABSTRACT_MAPPING_TYPE,
157
            $coreMediaPlaceholderTransfer->getProductId(),
158
            $locale
159
        );
160
161
        if (!$this->validateConcreteProductData($concreteProductData)) {
162
            return null;
163
        }
164
165
        $priceProductTransfers = $this->priceProductStorageClient
166
            ->getResolvedPriceProductConcreteTransfers(
167
                $concreteProductData[static::PRODUCT_DATA_KEY_ID_PRODUCT_CONCRETE],
168
                $concreteProductData[static::PRODUCT_DATA_KEY_ID_PRODUCT_ABSTRACT]
169
            );
170
171
        return $this->resolveProductPriceTransfer(
172
            $priceProductTransfers
173
        );
174
    }
175
176
    /**
177
     * @param array|null $concreteProductData
178
     *
179
     * @return bool
180
     */
181
    protected function validateConcreteProductData(?array $concreteProductData): bool
182
    {
183
        return $concreteProductData
0 ignored issues
show
Bug Best Practice introduced by
The expression $concreteProductData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
184
            && isset($concreteProductData[static::PRODUCT_DATA_KEY_ID_PRODUCT_CONCRETE], $concreteProductData[static::PRODUCT_DATA_KEY_ID_PRODUCT_ABSTRACT]);
185
    }
186
187
    /**
188
     * @param \Generated\Shared\Transfer\PriceProductTransfer[] $priceProductTransfers
189
     *
190
     * @return \Generated\Shared\Transfer\CurrentProductPriceTransfer|null
191
     */
192
    protected function resolveProductPriceTransfer(array $priceProductTransfers): ?CurrentProductPriceTransfer
193
    {
194
        $currentProductPriceTransfer = $this->priceProductClient->resolveProductPriceTransfer($priceProductTransfers);
195
196
        if (!$currentProductPriceTransfer->getCurrency() || !$currentProductPriceTransfer->getPrice()) {
197
            return null;
198
        }
199
200
        return $currentProductPriceTransfer;
201
    }
202
}
203