Completed
Push — master ( afa46a...04583c )
by Joachim
02:00
created

createCustomerInfo()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 29
nc 1
nop 12

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\Handler;
10
use Loevgaard\Dandomain\Pay\PaymentRequest as DandomainPaymentRequest;
11
use Loevgaard\DandomainAltapayBundle\Entity\Payment;
12
use Loevgaard\DandomainAltapayBundle\Entity\TerminalInterface;
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 DandomainPaymentRequest
31
     */
32
    protected $paymentRequest;
33
34
    /**
35
     * @var TerminalInterface
36
     */
37
    protected $terminal;
38
39
    /**
40
     * @var Payment
41
     */
42
    protected $payment;
43
44
    /**
45
     * @var Handler
46
     */
47
    protected $handler;
48
49
    public function __construct(
50
        ContainerInterface $container,
51
        DandomainPaymentRequest $paymentRequest,
52
        TerminalInterface $terminal,
53
        Payment $payment,
54
        Handler $handler
55
    ) {
56
        $this->container = $container;
57
        $this->router = $this->container->get('router');
58
        $this->paymentRequest = $paymentRequest;
59
        $this->terminal = $terminal;
60
        $this->payment = $payment;
61
        $this->handler = $handler;
62
    }
63
64
    /**
65
     * @return PaymentRequestPayload
66
     */
67
    public function generate(): PaymentRequestPayload
68
    {
69
        $paymentRequestPayload = new PaymentRequestPayload(
70
            $this->terminal->getTitle(),
71
            $this->paymentRequest->getOrderId(),
72
            $this->paymentRequest->getTotalAmount(),
73
            $this->paymentRequest->getCurrencySymbol()
74
        );
75
76
        foreach ($this->paymentRequest->getPaymentLines() as $paymentLine) {
77
            $orderLinePayload = $this->createOrderLine(
78
                $paymentLine->getName(),
79
                $paymentLine->getProductNumber(),
80
                $paymentLine->getQuantity(),
81
                $paymentLine->getPriceExclVat(),
82
                $paymentLine->getVat()
83
            );
84
85
            $paymentRequestPayload->addOrderLine($orderLinePayload);
86
        }
87
88
        // add payment fee as an order line if it's set
89 View Code Duplication
        if ($this->paymentRequest->getPaymentFee()) {
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...
90
            $orderLinePayload = $this->createOrderLine(
91
                $this->paymentRequest->getPaymentMethod(),
92
                $this->paymentRequest->getPaymentMethod(),
93
                1,
94
                $this->paymentRequest->getPaymentFee(),
95
                null,
96
                OrderLinePayload::GOODS_TYPE_HANDLING
97
            );
98
            $paymentRequestPayload->addOrderLine($orderLinePayload);
99
        }
100
101
        // add shipping fee as an order line if it's set
102 View Code Duplication
        if ($this->paymentRequest->getShippingFee()) {
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...
103
            $orderLinePayload = $this->createOrderLine(
104
                $this->paymentRequest->getShippingMethod(),
105
                $this->paymentRequest->getShippingMethod(),
106
                1,
107
                $this->paymentRequest->getShippingFee(),
108
                null,
109
                OrderLinePayload::GOODS_TYPE_SHIPMENT
110
            );
111
            $paymentRequestPayload->addOrderLine($orderLinePayload);
112
        }
113
114
        $customerNames = explode(' ', $this->paymentRequest->getCustomerName(), 2);
115
        $shippingNames = explode(' ', $this->paymentRequest->getDeliveryName(), 2);
116
117
        $customerInfoPayload = $this->createCustomerInfo(
118
            $customerNames[0] ?? '',
119
            $customerNames[1] ?? '',
120
            $this->paymentRequest->getCustomerAddress().($this->paymentRequest->getCustomerAddress2() ? "\r\n".$this->paymentRequest->getCustomerAddress2() : ''),
121
            $this->paymentRequest->getCustomerZipCode(),
122
            $this->paymentRequest->getCustomerCity(),
123
            $this->paymentRequest->getCustomerCountry(),
124
            $shippingNames[0] ?? '',
125
            $shippingNames[1] ?? '',
126
            $this->paymentRequest->getDeliveryAddress().($this->paymentRequest->getDeliveryAddress2() ? "\r\n".$this->paymentRequest->getDeliveryAddress2() : ''),
127
            $this->paymentRequest->getDeliveryZipCode(),
128
            $this->paymentRequest->getDeliveryCity(),
129
            $this->paymentRequest->getDeliveryCountry()
130
        );
131
        $paymentRequestPayload->setCustomerInfo($customerInfoPayload);
132
133
        $configPayload = $this->createConfig(
134
            $this->router->generate(
135
                'loevgaard_dandomain_altapay_callback_form',
136
                [],
137
                UrlGeneratorInterface::ABSOLUTE_URL
138
            ),
139
            $this->router->generate(
140
                'loevgaard_dandomain_altapay_callback_ok',
141
                [],
142
                UrlGeneratorInterface::ABSOLUTE_URL
143
            ),
144
            $this->router->generate(
145
                'loevgaard_dandomain_altapay_callback_fail',
146
                [],
147
                UrlGeneratorInterface::ABSOLUTE_URL
148
            ),
149
            $this->router->generate(
150
                'loevgaard_dandomain_altapay_callback_redirect',
151
                [],
152
                UrlGeneratorInterface::ABSOLUTE_URL
153
            ),
154
            $this->router->generate(
155
                'loevgaard_dandomain_altapay_callback_open',
156
                [],
157
                UrlGeneratorInterface::ABSOLUTE_URL
158
            ),
159
            $this->router->generate(
160
                'loevgaard_dandomain_altapay_callback_notification',
161
                [],
162
                UrlGeneratorInterface::ABSOLUTE_URL
163
            )
164
        );
165
        $paymentRequestPayload->setConfig($configPayload);
166
167
        $paymentRequestPayload
168
            ->setCookiePart(
169
                $this->container->getParameter('loevgaard_dandomain_altapay.cookie_payment_id'),
170
                $this->payment->getId()
171
            )
172
            ->setCookiePart(
173
                $this->container->getParameter('loevgaard_dandomain_altapay.cookie_checksum_complete'),
174
                $this->handler->getChecksum2()
175
            )
176
        ;
177
178
        return $paymentRequestPayload;
179
    }
180
181
    /**
182
     * @param string      $description
183
     * @param string      $itemId
184
     * @param string      $quantity
185
     * @param float       $unitPrice
186
     * @param float|null  $taxPercent
187
     * @param string|null $goodsType
188
     *
189
     * @return OrderLinePayload
190
     */
191
    protected function createOrderLine(
192
        string $description,
193
        string $itemId,
194
        string $quantity,
195
        float $unitPrice,
196
        float $taxPercent = null,
197
        string $goodsType = null
198
    ): OrderLinePayload {
199
        $payload = new OrderLinePayload($description, $itemId, $quantity, $unitPrice);
200
201
        if ($taxPercent) {
202
            $payload->setTaxPercent($taxPercent);
203
        }
204
205
        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...
206
            $payload->setGoodsType($goodsType);
207
        }
208
209
        return $payload;
210
    }
211
212
    /**
213
     * @param string $billingFirstName
214
     * @param string $billingLastName
215
     * @param string $billingAddress
216
     * @param string $billingPostal
217
     * @param string $billingCity
218
     * @param string $billingCountry
219
     * @param string $shippingFirstName
220
     * @param string $shippingLastName
221
     * @param string $shippingAddress
222
     * @param string $shippingPostal
223
     * @param string $shippingCity
224
     * @param string $shippingCountry
225
     *
226
     * @return CustomerInfoPayload
227
     */
228
    protected function createCustomerInfo(
229
        string $billingFirstName,
230
        string $billingLastName,
231
        string $billingAddress,
232
        string $billingPostal,
233
        string $billingCity,
234
        string $billingCountry,
235
        string $shippingFirstName,
236
        string $shippingLastName,
237
        string $shippingAddress,
238
        string $shippingPostal,
239
        string $shippingCity,
240
        string $shippingCountry
241
    ): CustomerInfoPayload {
242
        $payload = new CustomerInfoPayload();
243
        $payload
244
            ->setBillingFirstName($billingFirstName)
245
            ->setBillingLastName($billingLastName)
246
            ->setBillingAddress($billingAddress)
247
            ->setBillingPostal($billingPostal)
248
            ->setBillingCity($billingCity)
249
            ->setBillingCountry($billingCountry)
250
            ->setShippingFirstName($shippingFirstName)
251
            ->setShippingLastName($shippingLastName)
252
            ->setShippingAddress($shippingAddress)
253
            ->setShippingPostal($shippingPostal)
254
            ->setShippingCity($shippingCity)
255
            ->setShippingCountry($shippingCountry)
256
        ;
257
258
        return $payload;
259
    }
260
261
    /**
262
     * @param string $callbackForm
263
     * @param string $callbackOk
264
     * @param string $callbackFail
265
     * @param string $callbackRedirect
266
     * @param string $callbackOpen
267
     * @param string $callbackNotification
268
     *
269
     * @return ConfigPayload
270
     */
271
    protected function createConfig(
272
        string $callbackForm,
273
        string $callbackOk,
274
        string $callbackFail,
275
        string $callbackRedirect,
276
        string $callbackOpen,
277
        string $callbackNotification
278
    ): ConfigPayload {
279
        $payload = new ConfigPayload();
280
        $payload
281
            ->setCallbackForm($callbackForm)
282
            ->setCallbackOk($callbackOk)
283
            ->setCallbackFail($callbackFail)
284
            ->setCallbackRedirect($callbackRedirect)
285
            ->setCallbackOpen($callbackOpen)
286
            ->setCallbackNotification($callbackNotification)
287
        ;
288
289
        return $payload;
290
    }
291
}
292