Passed
Pull Request — master (#9)
by Volodymyr
03:58
created

CheckoutShipmentFormDataProvider   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 206
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 54
dl 0
loc 206
rs 10
c 0
b 0
f 0
wmc 20

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A appendDeliveryTime() 0 15 3
A createAvailableShipmentChoiceList() 0 16 3
A getAvailableShipmentMethods() 0 3 1
A getShipmentDescription() 0 8 1
A getData() 0 8 2
A appendShipmentPrice() 0 6 1
A getDeliveryTime() 0 7 2
A getOptions() 0 5 1
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\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
    /**
27
     * @var \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToShipmentClientInterface
28
     */
29
    protected $shipmentClient;
30
31
    /**
32
     * @var \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToGlossaryClientInterface
33
     */
34
    protected $glossaryClient;
35
36
    /**
37
     * @var \Spryker\Shared\Kernel\Store
38
     */
39
    protected $store;
40
41
    /**
42
     * @var \Spryker\Shared\Money\Dependency\Plugin\MoneyPluginInterface
43
     */
44
    protected $moneyPlugin;
45
46
    /**
47
     * @param \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToShipmentClientInterface $shipmentClient
48
     * @param \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToGlossaryClientInterface $glossaryClient
49
     * @param \Spryker\Shared\Kernel\Store $store
50
     * @param \Spryker\Shared\Money\Dependency\Plugin\MoneyPluginInterface $moneyPlugin
51
     */
52
    public function __construct(
53
        BraintreeToShipmentClientInterface $shipmentClient,
54
        BraintreeToGlossaryClientInterface $glossaryClient,
55
        Store $store,
56
        MoneyPluginInterface $moneyPlugin
57
    ) {
58
        $this->shipmentClient = $shipmentClient;
59
        $this->glossaryClient = $glossaryClient;
60
        $this->store = $store;
61
        $this->moneyPlugin = $moneyPlugin;
62
    }
63
64
    /**
65
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
66
     *
67
     * @return \Generated\Shared\Transfer\QuoteTransfer
68
     */
69
    public function getData(AbstractTransfer $quoteTransfer): QuoteTransfer
70
    {
71
        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

71
        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...
72
            $shipmentTransfer = new ShipmentTransfer();
73
            $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

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