CheckoutShipmentFormDataProvider   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 224
Duplicated Lines 0 %

Importance

Changes 6
Bugs 1 Features 0
Metric Value
eloc 61
c 6
b 1
f 0
dl 0
loc 224
rs 10
wmc 21

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getData() 0 8 2
A getOptions() 0 5 1
A appendDeliveryTime() 0 15 2
A getAvailableShipmentMethods() 0 3 1
A createAvailableShipmentChoiceList() 0 14 3
A getTranslatedDayName() 0 7 2
A getShipmentDescription() 0 8 1
A appendShipmentPrice() 0 6 1
A getDeliveryTime() 0 7 2
A getSelectedShipmentMethodId() 0 9 3
A translate() 0 3 1
A getFormattedShipmentPrice() 0 6 1
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\ShipmentMethodsCollectionTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...thodsCollectionTransfer 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\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...
13
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...
14
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...
15
use Spryker\Shared\Kernel\Store;
16
use Spryker\Shared\Kernel\Transfer\AbstractTransfer;
17
use Spryker\Shared\Money\Dependency\Plugin\MoneyPluginInterface;
18
use Spryker\Yves\StepEngine\Dependency\Form\StepEngineFormDataProviderInterface;
19
use SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToGlossaryClientInterface;
20
use SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToShipmentClientInterface;
21
use SprykerEco\Yves\Braintree\Form\CheckoutShipmentForm;
22
23
class CheckoutShipmentFormDataProvider implements StepEngineFormDataProviderInterface
24
{
25
    public const FIELD_ID_SHIPMENT_METHOD = 'idShipmentMethod';
26
27
    public const TRANSLATION_KEY_DELIVERY_TIME = 'page.checkout.shipping.delivery_time';
28
    public const TRANSLATION_KEY_DAY = 'page.checkout.shipping.day';
29
    public const TRANSLATION_KEY_DAYS = 'page.checkout.shipping.days';
30
31
    protected const SECONDS_IN_A_DAY = 86400;
32
33
    /**
34
     * @var \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToShipmentClientInterface
35
     */
36
    protected $shipmentClient;
37
38
    /**
39
     * @var \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToGlossaryClientInterface
40
     */
41
    protected $glossaryClient;
42
43
    /**
44
     * @var \Spryker\Shared\Kernel\Store
45
     */
46
    protected $store;
47
48
    /**
49
     * @var \Spryker\Shared\Money\Dependency\Plugin\MoneyPluginInterface
50
     */
51
    protected $moneyPlugin;
52
53
    /**
54
     * @param \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToShipmentClientInterface $shipmentClient
55
     * @param \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToGlossaryClientInterface $glossaryClient
56
     * @param \Spryker\Shared\Kernel\Store $store
57
     * @param \Spryker\Shared\Money\Dependency\Plugin\MoneyPluginInterface $moneyPlugin
58
     */
59
    public function __construct(
60
        BraintreeToShipmentClientInterface $shipmentClient,
61
        BraintreeToGlossaryClientInterface $glossaryClient,
62
        Store $store,
63
        MoneyPluginInterface $moneyPlugin
64
    ) {
65
        $this->shipmentClient = $shipmentClient;
66
        $this->glossaryClient = $glossaryClient;
67
        $this->store = $store;
68
        $this->moneyPlugin = $moneyPlugin;
69
    }
70
71
    /**
72
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
73
     *
74
     * @return \Generated\Shared\Transfer\QuoteTransfer
75
     */
76
    public function getData(AbstractTransfer $quoteTransfer): QuoteTransfer
77
    {
78
        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

78
        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...
79
            $shipmentTransfer = new ShipmentTransfer();
80
            $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

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