Passed
Push — feature/paypal-express ( f5d10f...058644 )
by Volodymyr
04:35 queued 13s
created

getTranslatedDayName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
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
    public const TRANSLATION_KEY_DELIVERY_TIME = 'page.checkout.shipping.delivery_time';
26
27
    protected const SECOND_IN_A_DAY = 86400;
28
29
    /**
30
     * @var \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToShipmentClientInterface
31
     */
32
    protected $shipmentClient;
33
34
    /**
35
     * @var \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToGlossaryClientInterface
36
     */
37
    protected $glossaryClient;
38
39
    /**
40
     * @var \Spryker\Shared\Kernel\Store
41
     */
42
    protected $store;
43
44
    /**
45
     * @var \Spryker\Shared\Money\Dependency\Plugin\MoneyPluginInterface
46
     */
47
    protected $moneyPlugin;
48
49
    /**
50
     * @param \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToShipmentClientInterface $shipmentClient
51
     * @param \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToGlossaryClientInterface $glossaryClient
52
     * @param \Spryker\Shared\Kernel\Store $store
53
     * @param \Spryker\Shared\Money\Dependency\Plugin\MoneyPluginInterface $moneyPlugin
54
     */
55
    public function __construct(
56
        BraintreeToShipmentClientInterface $shipmentClient,
57
        BraintreeToGlossaryClientInterface $glossaryClient,
58
        Store $store,
59
        MoneyPluginInterface $moneyPlugin
60
    ) {
61
        $this->shipmentClient = $shipmentClient;
62
        $this->glossaryClient = $glossaryClient;
63
        $this->store = $store;
64
        $this->moneyPlugin = $moneyPlugin;
65
    }
66
67
    /**
68
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
69
     *
70
     * @return \Generated\Shared\Transfer\QuoteTransfer
71
     */
72
    public function getData(AbstractTransfer $quoteTransfer): QuoteTransfer
73
    {
74
        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

74
        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...
75
            $shipmentTransfer = new ShipmentTransfer();
76
            $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

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