Passed
Push — feature/paypal-express ( ec9040...c08f5f )
by Volodymyr
06:46
created

appendDeliveryTime()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 2
nop 2
dl 0
loc 15
rs 9.9666
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\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\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...
12
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...
13
use Spryker\Shared\Kernel\Store;
14
use Spryker\Shared\Kernel\Transfer\AbstractTransfer;
15
use Spryker\Shared\Money\Dependency\Plugin\MoneyPluginInterface;
16
use Spryker\Yves\StepEngine\Dependency\Form\StepEngineFormDataProviderInterface;
17
use SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToGlossaryClientInterface;
18
use SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToShipmentClientInterface;
19
use SprykerShop\Yves\CheckoutPage\Form\Steps\ShipmentForm;
0 ignored issues
show
Bug introduced by
The type SprykerShop\Yves\Checkou...Form\Steps\ShipmentForm 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...
20
21
class CheckoutShipmentFormDataProvider implements StepEngineFormDataProviderInterface
22
{
23
    public const FIELD_ID_SHIPMENT_METHOD = 'idShipmentMethod';
24
25
    /**
26
     * @var BraintreeToShipmentClientInterface
27
     */
28
    protected $shipmentClient;
29
30
    /**
31
     * @var BraintreeToGlossaryClientInterface
32
     */
33
    protected $glossaryClient;
34
35
    /**
36
     * @var \Spryker\Shared\Kernel\Store
37
     */
38
    protected $store;
39
40
    /**
41
     * @var \Spryker\Shared\Money\Dependency\Plugin\MoneyPluginInterface
42
     */
43
    protected $moneyPlugin;
44
45
    /**
46
     * @param BraintreeToShipmentClientInterface $shipmentClient
47
     * @param BraintreeToGlossaryClientInterface $glossaryClient
48
     * @param Store $store
49
     * @param MoneyPluginInterface $moneyPlugin
50
     */
51
    public function __construct(
52
        BraintreeToShipmentClientInterface $shipmentClient,
53
        BraintreeToGlossaryClientInterface $glossaryClient,
54
        Store $store,
55
        MoneyPluginInterface $moneyPlugin
56
    ) {
57
        $this->shipmentClient = $shipmentClient;
58
        $this->glossaryClient = $glossaryClient;
59
        $this->store = $store;
60
        $this->moneyPlugin = $moneyPlugin;
61
    }
62
63
    /**
64
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
65
     *
66
     * @return \Generated\Shared\Transfer\QuoteTransfer
67
     */
68
    public function getData(AbstractTransfer $quoteTransfer)
69
    {
70
        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

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

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