Completed
Push — master ( 10cfb1...be140a )
by Joachim
02:56
created

PaymentRequestPayloadGenerator::generate()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 121
Code Lines 80

Duplication

Lines 26
Ratio 21.49 %

Importance

Changes 0
Metric Value
dl 26
loc 121
rs 5.2676
c 0
b 0
f 0
cc 8
eloc 80
nc 8
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Loevgaard\DandomainAltapayBundle\PayloadGenerator;
4
5
use Loevgaard\AltaPay\Payload\OrderLine as OrderLinePayload;
6
use Loevgaard\AltaPay\Payload\PaymentRequest as PaymentRequestPayload;
7
use Loevgaard\AltaPay\Payload\PaymentRequest\Config as ConfigPayload;
8
use Loevgaard\AltaPay\Payload\PaymentRequest\CustomerInfo as CustomerInfoPayload;
9
use Loevgaard\Dandomain\Pay\Helper\ChecksumHelper;
10
use Loevgaard\Dandomain\Pay\Model\Payment as DandomainPayment;
11
use Loevgaard\DandomainAltapayBundle\Entity\Payment;
12
use Loevgaard\DandomainAltapayBundle\Entity\Terminal;
13
use Symfony\Bundle\FrameworkBundle\Routing\Router;
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
16
17
class PaymentRequestPayloadGenerator implements PayloadGeneratorInterface
18
{
19
    /**
20
     * @var ContainerInterface
21
     */
22
    protected $container;
23
24
    /**
25
     * @var Router
26
     */
27
    protected $router;
28
29
    /**
30
     * @var DandomainPayment
31
     */
32
    protected $dandomainPayment;
33
34
    /**
35
     * @var Terminal
36
     */
37
    protected $terminal;
38
39
    /**
40
     * @var Payment
41
     */
42
    protected $payment;
43
44
    /**
45
     * @var ChecksumHelper
46
     */
47
    protected $checksumHelper;
48
49
    public function __construct(
50
        ContainerInterface $container,
51
        DandomainPayment $paymentRequest,
52
        Terminal $terminal,
53
        Payment $payment,
54
        ChecksumHelper $checksumHelper
55
    ) {
56
        $this->container = $container;
57
        $this->router = $this->container->get('router');
58
        $this->dandomainPayment = $paymentRequest;
59
        $this->terminal = $terminal;
60
        $this->payment = $payment;
61
        $this->checksumHelper = $checksumHelper;
62
    }
63
64
    /**
65
     * @return PaymentRequestPayload
66
     */
67
    public function generate(): PaymentRequestPayload
68
    {
69
        $totalAmount = (float) $this->dandomainPayment->getTotalAmount()->getAmount() / 100;
70
71
        $paymentRequestPayload = new PaymentRequestPayload(
72
            $this->terminal->getTitle(),
73
            $this->dandomainPayment->getOrderId(),
74
            $totalAmount,
75
            $this->dandomainPayment->getCurrencySymbol()
76
        );
77
78
        foreach ($this->dandomainPayment->getPaymentLines() as $paymentLine) {
79
            $priceExclVat = (float) $paymentLine->getPriceExclVat()->getAmount() / 100;
80
81
            $orderLinePayload = $this->createOrderLine(
82
                $paymentLine->getName(),
83
                $paymentLine->getProductNumber(),
84
                $paymentLine->getQuantity(),
85
                $priceExclVat,
86
                $paymentLine->getVat()
87
            );
88
89
            $paymentRequestPayload->addOrderLine($orderLinePayload);
90
        }
91
92
        // add payment fee as an order line if it's set
93 View Code Duplication
        if ($this->dandomainPayment->getPaymentFee() && 0 !== (int) $this->dandomainPayment->getPaymentFee()->getAmount()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
            $paymentFee = (float) $this->dandomainPayment->getPaymentFee()->getAmount() / 100;
95
96
            $orderLinePayload = $this->createOrderLine(
97
                $this->dandomainPayment->getPaymentMethod(),
98
                $this->dandomainPayment->getPaymentMethod(),
99
                1,
100
                $paymentFee,
101
                null,
102
                OrderLinePayload::GOODS_TYPE_HANDLING
103
            );
104
            $paymentRequestPayload->addOrderLine($orderLinePayload);
105
        }
106
107
        // add shipping fee as an order line if it's set
108 View Code Duplication
        if ($this->dandomainPayment->getShippingFee() && 0 !== (int) $this->dandomainPayment->getShippingFee()->getAmount()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
            $shippingFee = (float) $this->dandomainPayment->getShippingFee()->getAmount() / 100;
110
111
            $orderLinePayload = $this->createOrderLine(
112
                $this->dandomainPayment->getShippingMethod(),
113
                $this->dandomainPayment->getShippingMethod(),
114
                1,
115
                $shippingFee,
116
                null,
117
                OrderLinePayload::GOODS_TYPE_SHIPMENT
118
            );
119
            $paymentRequestPayload->addOrderLine($orderLinePayload);
120
        }
121
122
        $customerNames = explode(' ', $this->dandomainPayment->getCustomerName(), 2);
123
        $shippingNames = explode(' ', $this->dandomainPayment->getDeliveryName(), 2);
124
125
        $customerInfoPayload = $this->createCustomerInfo(
126
            $customerNames[0] ?? '',
127
            $customerNames[1] ?? '',
128
            $this->dandomainPayment->getCustomerAddress().($this->dandomainPayment->getCustomerAddress2() ? "\r\n".$this->dandomainPayment->getCustomerAddress2() : ''),
129
            $this->dandomainPayment->getCustomerZipCode(),
130
            $this->dandomainPayment->getCustomerCity(),
131
            $this->dandomainPayment->getCustomerCountry(),
132
            $shippingNames[0] ?? '',
133
            $shippingNames[1] ?? '',
134
            $this->dandomainPayment->getDeliveryAddress().($this->dandomainPayment->getDeliveryAddress2() ? "\r\n".$this->dandomainPayment->getDeliveryAddress2() : ''),
135
            $this->dandomainPayment->getDeliveryZipCode(),
136
            $this->dandomainPayment->getDeliveryCity(),
137
            $this->dandomainPayment->getDeliveryCountry()
138
        );
139
        $paymentRequestPayload->setCustomerInfo($customerInfoPayload);
140
141
        $configPayload = $this->createConfig(
142
            $this->router->generate(
143
                'loevgaard_dandomain_altapay_callback_form',
144
                [],
145
                UrlGeneratorInterface::ABSOLUTE_URL
146
            ),
147
            $this->router->generate(
148
                'loevgaard_dandomain_altapay_callback_ok',
149
                [],
150
                UrlGeneratorInterface::ABSOLUTE_URL
151
            ),
152
            $this->router->generate(
153
                'loevgaard_dandomain_altapay_callback_fail',
154
                [],
155
                UrlGeneratorInterface::ABSOLUTE_URL
156
            ),
157
            $this->router->generate(
158
                'loevgaard_dandomain_altapay_callback_redirect',
159
                [],
160
                UrlGeneratorInterface::ABSOLUTE_URL
161
            ),
162
            $this->router->generate(
163
                'loevgaard_dandomain_altapay_callback_open',
164
                [],
165
                UrlGeneratorInterface::ABSOLUTE_URL
166
            ),
167
            $this->router->generate(
168
                'loevgaard_dandomain_altapay_callback_notification',
169
                [],
170
                UrlGeneratorInterface::ABSOLUTE_URL
171
            )
172
        );
173
        $paymentRequestPayload->setConfig($configPayload);
174
175
        $paymentRequestPayload
176
            ->setCookiePart(
177
                $this->container->getParameter('loevgaard_dandomain_altapay.cookie_payment_id'),
178
                $this->payment->getId()
179
            )
180
            ->setCookiePart(
181
                $this->container->getParameter('loevgaard_dandomain_altapay.cookie_checksum_complete'),
182
                $this->checksumHelper->getChecksum2()
183
            )
184
        ;
185
186
        return $paymentRequestPayload;
187
    }
188
189
    /**
190
     * @param string      $description
191
     * @param string      $itemId
192
     * @param string      $quantity
193
     * @param float       $unitPrice
194
     * @param float|null  $taxPercent
195
     * @param string|null $goodsType
196
     *
197
     * @return OrderLinePayload
198
     */
199
    protected function createOrderLine(
200
        string $description,
201
        string $itemId,
202
        string $quantity,
203
        float $unitPrice,
204
        float $taxPercent = null,
205
        string $goodsType = null
206
    ): OrderLinePayload {
207
        $payload = new OrderLinePayload($description, $itemId, $quantity, $unitPrice);
208
209
        if ($taxPercent) {
210
            $payload->setTaxPercent($taxPercent);
211
        }
212
213
        if ($goodsType) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $goodsType of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
214
            $payload->setGoodsType($goodsType);
215
        }
216
217
        return $payload;
218
    }
219
220
    /**
221
     * @param string $billingFirstName
222
     * @param string $billingLastName
223
     * @param string $billingAddress
224
     * @param string $billingPostal
225
     * @param string $billingCity
226
     * @param string $billingCountry
227
     * @param string $shippingFirstName
228
     * @param string $shippingLastName
229
     * @param string $shippingAddress
230
     * @param string $shippingPostal
231
     * @param string $shippingCity
232
     * @param string $shippingCountry
233
     *
234
     * @return CustomerInfoPayload
235
     */
236
    protected function createCustomerInfo(
237
        string $billingFirstName,
238
        string $billingLastName,
239
        string $billingAddress,
240
        string $billingPostal,
241
        string $billingCity,
242
        string $billingCountry,
243
        string $shippingFirstName,
244
        string $shippingLastName,
245
        string $shippingAddress,
246
        string $shippingPostal,
247
        string $shippingCity,
248
        string $shippingCountry
249
    ): CustomerInfoPayload {
250
        $payload = new CustomerInfoPayload();
251
        $payload
252
            ->setBillingFirstName($billingFirstName)
253
            ->setBillingLastName($billingLastName)
254
            ->setBillingAddress($billingAddress)
255
            ->setBillingPostal($billingPostal)
256
            ->setBillingCity($billingCity)
257
            ->setBillingCountry($billingCountry)
258
            ->setShippingFirstName($shippingFirstName)
259
            ->setShippingLastName($shippingLastName)
260
            ->setShippingAddress($shippingAddress)
261
            ->setShippingPostal($shippingPostal)
262
            ->setShippingCity($shippingCity)
263
            ->setShippingCountry($shippingCountry)
264
        ;
265
266
        return $payload;
267
    }
268
269
    /**
270
     * @param string $callbackForm
271
     * @param string $callbackOk
272
     * @param string $callbackFail
273
     * @param string $callbackRedirect
274
     * @param string $callbackOpen
275
     * @param string $callbackNotification
276
     *
277
     * @return ConfigPayload
278
     */
279
    protected function createConfig(
280
        string $callbackForm,
281
        string $callbackOk,
282
        string $callbackFail,
283
        string $callbackRedirect,
284
        string $callbackOpen,
285
        string $callbackNotification
286
    ): ConfigPayload {
287
        $payload = new ConfigPayload();
288
        $payload
289
            ->setCallbackForm($callbackForm)
290
            ->setCallbackOk($callbackOk)
291
            ->setCallbackFail($callbackFail)
292
            ->setCallbackRedirect($callbackRedirect)
293
            ->setCallbackOpen($callbackOpen)
294
            ->setCallbackNotification($callbackNotification)
295
        ;
296
297
        return $payload;
298
    }
299
}
300