Passed
Pull Request — master (#9)
by Volodymyr
05:00
created

createAvailableShipmentChoiceList()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 16
rs 9.9666
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\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 SprykerEco\Yves\Braintree\Form\CheckoutShipmentForm;
20
21
class CheckoutShipmentFormDataProvider implements StepEngineFormDataProviderInterface
22
{
23
    public const FIELD_ID_SHIPMENT_METHOD = 'idShipmentMethod';
24
25
    /**
26
     * @var \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToShipmentClientInterface
27
     */
28
    protected $shipmentClient;
29
30
    /**
31
     * @var \SprykerEco\Yves\Braintree\Dependency\Client\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 \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToShipmentClientInterface $shipmentClient
47
     * @param \SprykerEco\Yves\Braintree\Dependency\Client\BraintreeToGlossaryClientInterface $glossaryClient
48
     * @param \Spryker\Shared\Kernel\Store $store
49
     * @param \Spryker\Shared\Money\Dependency\Plugin\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
            CheckoutShipmentForm::OPTION_SHIPMENT_METHODS => $this->createAvailableShipmentChoiceList($quoteTransfer),
87
            CheckoutShipmentForm::OPTION_ID_SELECTED_SHIPMENT_METHOD => $this->getSelectedShipmentMethodId($quoteTransfer),
88
        ];
89
    }
90
91
    /**
92
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
93
     *
94
     * @return array
95
     */
96
    protected function createAvailableShipmentChoiceList(QuoteTransfer $quoteTransfer)
97
    {
98
        $shipmentMethods = [];
99
100
        $shipmentMethodsTransfer = $this->getAvailableShipmentMethods($quoteTransfer);
101
        foreach ($shipmentMethodsTransfer->getMethods() as $shipmentMethodTransfer) {
102
            if (!isset($shipmentMethods[$shipmentMethodTransfer->getCarrierName()])) {
103
                $shipmentMethods[$shipmentMethodTransfer->getCarrierName()] = [];
104
            }
105
            $description = $this->getShipmentDescription(
106
                $shipmentMethodTransfer
107
            );
108
            $shipmentMethods[$shipmentMethodTransfer->getCarrierName()][$description] = $shipmentMethodTransfer->getIdShipmentMethod();
109
        }
110
111
        return $shipmentMethods;
112
    }
113
114
    /**
115
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
116
     *
117
     * @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...
118
     */
119
    protected function getAvailableShipmentMethods(QuoteTransfer $quoteTransfer)
120
    {
121
        return $this->shipmentClient->getAvailableMethods($quoteTransfer);
122
    }
123
124
    /**
125
     * @param \Generated\Shared\Transfer\ShipmentMethodTransfer $shipmentMethodTransfer
126
     *
127
     * @return string
128
     */
129
    protected function getShipmentDescription(ShipmentMethodTransfer $shipmentMethodTransfer)
130
    {
131
        $shipmentDescription = $this->translate($shipmentMethodTransfer->getName());
132
133
        $shipmentDescription = $this->appendDeliveryTime($shipmentMethodTransfer, $shipmentDescription);
134
        $shipmentDescription = $this->appendShipmentPrice($shipmentMethodTransfer, $shipmentDescription);
135
136
        return $shipmentDescription;
137
    }
138
139
    /**
140
     * @param \Generated\Shared\Transfer\ShipmentMethodTransfer $shipmentMethodTransfer
141
     * @param string $shipmentDescription
142
     *
143
     * @return string
144
     */
145
    protected function appendDeliveryTime(ShipmentMethodTransfer $shipmentMethodTransfer, $shipmentDescription)
146
    {
147
        $deliveryTime = $this->getDeliveryTime($shipmentMethodTransfer);
148
149
        if ($deliveryTime !== 0) {
150
            $shipmentDescription = sprintf(
151
                '%s (%s %d %s)',
152
                $shipmentDescription,
153
                $this->translate('page.checkout.shipping.delivery_time'),
154
                $deliveryTime,
155
                ($deliveryTime === 1) ? 'day' : 'days'
156
            );
157
        }
158
159
        return $shipmentDescription;
160
    }
161
162
    /**
163
     * @param \Generated\Shared\Transfer\ShipmentMethodTransfer $shipmentMethodTransfer
164
     * @param string $shipmentDescription
165
     *
166
     * @return string
167
     */
168
    protected function appendShipmentPrice(ShipmentMethodTransfer $shipmentMethodTransfer, $shipmentDescription)
169
    {
170
        $shipmentPrice = $this->getFormattedShipmentPrice($shipmentMethodTransfer);
171
        $shipmentDescription .= ': ' . $shipmentPrice;
172
173
        return $shipmentDescription;
174
    }
175
176
    /**
177
     * @param \Generated\Shared\Transfer\ShipmentMethodTransfer $method
178
     *
179
     * @return int
180
     */
181
    protected function getDeliveryTime(ShipmentMethodTransfer $method)
182
    {
183
        if (!$method->getDeliveryTime()) {
184
            return 0;
185
        }
186
187
        return (int)($method->getDeliveryTime() / 86400);
188
    }
189
190
    /**
191
     * @param \Generated\Shared\Transfer\ShipmentMethodTransfer $shipmentMethodTransfer
192
     *
193
     * @return string
194
     */
195
    protected function getFormattedShipmentPrice(ShipmentMethodTransfer $shipmentMethodTransfer)
196
    {
197
        $moneyTransfer = $this->moneyPlugin
198
            ->fromInteger($shipmentMethodTransfer->getStoreCurrencyPrice());
199
200
        return $this->moneyPlugin->formatWithSymbol($moneyTransfer);
201
    }
202
203
    /**
204
     * @param string $translationKey
205
     *
206
     * @return string
207
     */
208
    protected function translate($translationKey)
209
    {
210
        return $this->glossaryClient->translate($translationKey, $this->store->getCurrentLocale());
211
    }
212
213
    /**
214
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
215
     *
216
     * @return int|null
217
     */
218
    protected function getSelectedShipmentMethodId(QuoteTransfer $quoteTransfer): ?int
219
    {
220
        $shipment = $quoteTransfer->getShipment();
221
222
        if (!$shipment) {
223
            return null;
224
        }
225
226
        return $shipment->getMethod() ? $shipment->getMethod()->getIdShipmentMethod() : null;
227
    }
228
}
229