Completed
Push — master ( cddd98...c8ab6f )
by Oleksandr
14s queued 10s
created

getShipmentDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Yves\Braintree\Form\DataProvider;
9
10
use Generated\Shared\Transfer\QuoteTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\QuoteTransfer 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\ShipmentMethodsTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ShipmentMethodsTransfer 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\ShipmentMethodTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ShipmentMethodTransfer 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 Generated\Shared\Transfer\ShipmentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ShipmentTransfer 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 Spryker\Shared\Kernel\Store;
15
use Spryker\Shared\Kernel\Transfer\AbstractTransfer;
16
use Spryker\Shared\Money\Dependency\Plugin\MoneyPluginInterface;
17
use Spryker\Yves\StepEngine\Dependency\Form\StepEngineFormDataProviderInterface;
18
use SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToGlossaryClientInterface;
19
use SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToShipmentClientInterface;
20
use SprykerEco\Yves\Braintree\Form\CheckoutShipmentForm;
21
22
class CheckoutShipmentFormDataProvider implements StepEngineFormDataProviderInterface
23
{
24
    public const FIELD_ID_SHIPMENT_METHOD = 'idShipmentMethod';
25
26
    public const TRANSLATION_KEY_DELIVERY_TIME = 'page.checkout.shipping.delivery_time';
27
    public const TRANSLATION_KEY_DAY = 'page.checkout.shipping.day';
28
    public const TRANSLATION_KEY_DAYS = 'page.checkout.shipping.days';
29
30
    protected const SECONDS_IN_A_DAY = 86400;
31
32
    /**
33
     * @var \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToShipmentClientInterface
34
     */
35
    protected $shipmentClient;
36
37
    /**
38
     * @var \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToGlossaryClientInterface
39
     */
40
    protected $glossaryClient;
41
42
    /**
43
     * @var \Spryker\Shared\Kernel\Store
44
     */
45
    protected $store;
46
47
    /**
48
     * @var \Spryker\Shared\Money\Dependency\Plugin\MoneyPluginInterface
49
     */
50
    protected $moneyPlugin;
51
52
    /**
53
     * @param \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToShipmentClientInterface $shipmentClient
54
     * @param \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToGlossaryClientInterface $glossaryClient
55
     * @param \Spryker\Shared\Kernel\Store $store
56
     * @param \Spryker\Shared\Money\Dependency\Plugin\MoneyPluginInterface $moneyPlugin
57
     */
58
    public function __construct(
59
        BraintreeToShipmentClientInterface $shipmentClient,
60
        BraintreeToGlossaryClientInterface $glossaryClient,
61
        Store $store,
62
        MoneyPluginInterface $moneyPlugin
63
    ) {
64
        $this->shipmentClient = $shipmentClient;
65
        $this->glossaryClient = $glossaryClient;
66
        $this->store = $store;
67
        $this->moneyPlugin = $moneyPlugin;
68
    }
69
70
    /**
71
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
72
     *
73
     * @return \Generated\Shared\Transfer\QuoteTransfer
74
     */
75
    public function getData(AbstractTransfer $quoteTransfer): QuoteTransfer
76
    {
77
        if ($quoteTransfer->getShipment() === null) {
0 ignored issues
show
Bug introduced by
The method getShipment() does not exist on Spryker\Shared\Kernel\Transfer\AbstractTransfer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
        if ($quoteTransfer->/** @scrutinizer ignore-call */ getShipment() === null) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
            $shipmentTransfer = new ShipmentTransfer();
79
            $quoteTransfer->setShipment($shipmentTransfer);
0 ignored issues
show
Bug introduced by
The method setShipment() does not exist on Spryker\Shared\Kernel\Transfer\AbstractTransfer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
            $quoteTransfer->/** @scrutinizer ignore-call */ 
80
                            setShipment($shipmentTransfer);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80
        }
81
82
        return $quoteTransfer;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $quoteTransfer returns the type Spryker\Shared\Kernel\Transfer\AbstractTransfer which is incompatible with the type-hinted return Generated\Shared\Transfer\QuoteTransfer.
Loading history...
83
    }
84
85
    /**
86
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
87
     *
88
     * @return array
89
     */
90
    public function getOptions(AbstractTransfer $quoteTransfer): array
91
    {
92
        return [
93
            CheckoutShipmentForm::OPTION_SHIPMENT_METHODS => $this->createAvailableShipmentChoiceList($quoteTransfer),
94
            CheckoutShipmentForm::OPTION_ID_SELECTED_SHIPMENT_METHOD => $this->getSelectedShipmentMethodId($quoteTransfer),
95
        ];
96
    }
97
98
    /**
99
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
100
     *
101
     * @return array
102
     */
103
    protected function createAvailableShipmentChoiceList(QuoteTransfer $quoteTransfer): array
104
    {
105
        $shipmentMethods = [];
106
107
        $shipmentMethodsTransfer = $this->getAvailableShipmentMethods($quoteTransfer);
108
        foreach ($shipmentMethodsTransfer->getMethods() as $shipmentMethodTransfer) {
109
            if (!isset($shipmentMethods[$shipmentMethodTransfer->getCarrierName()])) {
110
                $shipmentMethods[$shipmentMethodTransfer->getCarrierName()] = [];
111
            }
112
            $description = $this->getShipmentDescription(
113
                $shipmentMethodTransfer
114
            );
115
            $shipmentMethods[$shipmentMethodTransfer->getCarrierName()][$description] = $shipmentMethodTransfer->getIdShipmentMethod();
116
        }
117
118
        return $shipmentMethods;
119
    }
120
121
    /**
122
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
123
     *
124
     * @return \Generated\Shared\Transfer\ShipmentMethodsTransfer
125
     */
126
    protected function getAvailableShipmentMethods(QuoteTransfer $quoteTransfer): ShipmentMethodsTransfer
127
    {
128
        return $this->shipmentClient->getAvailableMethods($quoteTransfer);
129
    }
130
131
    /**
132
     * @param \Generated\Shared\Transfer\ShipmentMethodTransfer $shipmentMethodTransfer
133
     *
134
     * @return string
135
     */
136
    protected function getShipmentDescription(ShipmentMethodTransfer $shipmentMethodTransfer): string
137
    {
138
        $shipmentDescription = $this->translate($shipmentMethodTransfer->getName());
139
140
        $shipmentDescription = $this->appendDeliveryTime($shipmentMethodTransfer, $shipmentDescription);
141
        $shipmentDescription = $this->appendShipmentPrice($shipmentMethodTransfer, $shipmentDescription);
142
143
        return $shipmentDescription;
144
    }
145
146
    /**
147
     * @param \Generated\Shared\Transfer\ShipmentMethodTransfer $shipmentMethodTransfer
148
     * @param string $shipmentDescription
149
     *
150
     * @return string
151
     */
152
    protected function appendDeliveryTime(ShipmentMethodTransfer $shipmentMethodTransfer, $shipmentDescription): string
153
    {
154
        $deliveryTime = $this->getDeliveryTime($shipmentMethodTransfer);
155
156
        if ($deliveryTime !== 0) {
157
            $shipmentDescription = sprintf(
158
                '%s (%s %d %s)',
159
                $shipmentDescription,
160
                $this->translate(static::TRANSLATION_KEY_DELIVERY_TIME),
161
                $deliveryTime,
162
                $this->getTranslatedDayName($deliveryTime)
163
            );
164
        }
165
166
        return $shipmentDescription;
167
    }
168
169
    /**
170
     * @param \Generated\Shared\Transfer\ShipmentMethodTransfer $shipmentMethodTransfer
171
     * @param string $shipmentDescription
172
     *
173
     * @return string
174
     */
175
    protected function appendShipmentPrice(ShipmentMethodTransfer $shipmentMethodTransfer, $shipmentDescription): string
176
    {
177
        $shipmentPrice = $this->getFormattedShipmentPrice($shipmentMethodTransfer);
178
        $shipmentDescription .= ': ' . $shipmentPrice;
179
180
        return $shipmentDescription;
181
    }
182
183
    /**
184
     * @param \Generated\Shared\Transfer\ShipmentMethodTransfer $method
185
     *
186
     * @return int
187
     */
188
    protected function getDeliveryTime(ShipmentMethodTransfer $method): int
189
    {
190
        if (!$method->getDeliveryTime()) {
191
            return 0;
192
        }
193
194
        return (int)ceil($method->getDeliveryTime() / static::SECONDS_IN_A_DAY);
195
    }
196
197
    /**
198
     * @param \Generated\Shared\Transfer\ShipmentMethodTransfer $shipmentMethodTransfer
199
     *
200
     * @return string
201
     */
202
    protected function getFormattedShipmentPrice(ShipmentMethodTransfer $shipmentMethodTransfer): string
203
    {
204
        $moneyTransfer = $this->moneyPlugin
205
            ->fromInteger($shipmentMethodTransfer->getStoreCurrencyPrice());
206
207
        return $this->moneyPlugin->formatWithSymbol($moneyTransfer);
208
    }
209
210
    /**
211
     * @param string $translationKey
212
     *
213
     * @return string
214
     */
215
    protected function translate($translationKey): string
216
    {
217
        return $this->glossaryClient->translate($translationKey, $this->store->getCurrentLocale());
218
    }
219
220
    /**
221
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
222
     *
223
     * @return int|null
224
     */
225
    protected function getSelectedShipmentMethodId(QuoteTransfer $quoteTransfer): ?int
226
    {
227
        $shipment = $quoteTransfer->getShipment();
228
229
        if (!$shipment) {
230
            return null;
231
        }
232
233
        return $shipment->getMethod() ? $shipment->getMethod()->getIdShipmentMethod() : null;
234
    }
235
236
    /**
237
     * @param int $deliveryTime
238
     *
239
     * @return string
240
     */
241
    protected function getTranslatedDayName(int $deliveryTime): string
242
    {
243
        if ($deliveryTime === 1) {
244
            return $this->translate(static::TRANSLATION_KEY_DAY);
245
        }
246
247
        return $this->translate(static::TRANSLATION_KEY_DAYS);
248
    }
249
}
250