Completed
Push — master ( b2ba42...b33960 )
by Joachim
26:09
created

PaymentManager   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 233
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 14

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 14
dl 0
loc 233
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
B createPaymentFromDandomainPaymentRequest() 0 95 2
A findPaymentById() 0 7 1
A setOrderManager() 0 5 1
A setOrderLineManager() 0 5 1
A setSiteManager() 0 5 1
A setCustomerManager() 0 5 1
A setDeliveryManager() 0 5 1
A setPaymentMethodManager() 0 5 1
A setShippingMethodManager() 0 5 1
1
<?php
2
namespace Loevgaard\DandomainAltapayBundle\Manager;
3
4
use Doctrine\Common\Persistence\ObjectManager;
5
use Loevgaard\Dandomain\Pay\PaymentRequest;
6
use Loevgaard\DandomainAltapayBundle\Entity\PaymentInterface;
7
use Loevgaard\DandomainFoundationBundle\Manager\Manager;
8
use Loevgaard\DandomainFoundationBundle\Manager\OrderManager;
9
use Loevgaard\DandomainFoundationBundle\Manager\OrderLineManager;
10
use Loevgaard\DandomainFoundationBundle\Manager\PaymentMethodManager;
11
use Loevgaard\DandomainFoundationBundle\Manager\ShippingMethodManager;
12
use Loevgaard\DandomainFoundationBundle\Manager\SiteManager;
13
use Loevgaard\DandomainFoundationBundle\Manager\CustomerManager;
14
use Loevgaard\DandomainFoundationBundle\Manager\DeliveryManager;
15
16
/**
17
 * @method PaymentInterface create()
18
 * @method delete(PaymentInterface $obj)
19
 * @method update(PaymentInterface $obj, $flush = true)
20
 */
21
class PaymentManager extends Manager
22
{
23
    /**
24
     * @var ObjectManager
25
     */
26
    protected $objectManager;
27
28
    /**
29
     * @var OrderManager
30
     */
31
    protected $orderManager;
32
33
    /**
34
     * @var OrderLineManager
35
     */
36
    protected $orderLineManager;
37
38
    /**
39
     * @var SiteManager
40
     */
41
    protected $siteManager;
42
43
    /**
44
     * @var CustomerManager
45
     */
46
    protected $customerManager;
47
48
    /**
49
     * @var DeliveryManager
50
     */
51
    protected $deliveryManager;
52
53
    /**
54
     * @var PaymentMethodManager
55
     */
56
    protected $paymentMethodManager;
57
58
    /**
59
     * @var ShippingMethodManager
60
     */
61
    protected $shippingMethodManager;
62
63
    /**
64
     * @var string
65
     */
66
    protected $class;
67
68
    /**
69
     * @param PaymentRequest $paymentRequest
70
     * @return PaymentInterface
71
     */
72
    public function createPaymentFromDandomainPaymentRequest(PaymentRequest $paymentRequest) : PaymentInterface
73
    {
74
        $order = $this->orderManager->create();
75
76
77
        $site = $this->siteManager->findByExternalId($paymentRequest->getLanguageId());
78
        $order->setExternalId($paymentRequest->getOrderId());
79
        $order->setCurrencyCode($paymentRequest->getCurrencySymbol());
80
        $order->setIp($paymentRequest->getCustomerIp());
81
        //$order->setLoadBalancerRealIp($paymentRequest->getLoadBalancerRealIp());
0 ignored issues
show
Unused Code Comprehensibility introduced by
82% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
82
        $order->setSite($site);
83
84
        // create customer object
85
        $customer = $this->customerManager->create();
86
        //$customer->setCustomerRekvNr($paymentRequest->getCustomerRekvNr()); @todo ask Dandomain why this field is not present on the customer object
87
        $customer->setAttention($paymentRequest->getCustomerName());
88
        $customer->setName($paymentRequest->getCustomerCompany());
89
        $customer->setAddress($paymentRequest->getCustomerAddress());
90
        $customer->setAddress2($paymentRequest->getCustomerAddress2());
91
        $customer->setZipCode($paymentRequest->getCustomerZipCode());
92
        $customer->setCity($paymentRequest->getCustomerCity());
93
        $customer->setCountryId((int)$paymentRequest->getCustomerCountryId());
94
        $customer->setCountry($paymentRequest->getCustomerCountry());
95
        $customer->setPhone($paymentRequest->getCustomerPhone());
96
        $customer->setFax($paymentRequest->getCustomerFax());
97
        $customer->setEmail($paymentRequest->getCustomerEmail());
98
        $customer->setComments($paymentRequest->getCustomerNote());
99
        $customer->setCvr($paymentRequest->getCustomerCvrnr());
100
        //$customer->setCustomerCustTypeId($paymentRequest->getCustomerCustTypeId()); @todo figure out what this field is
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
101
        $customer->setEan($paymentRequest->getCustomerEan());
102
        $customer->setReservedField1($paymentRequest->getCustomerRes1());
103
        $customer->setReservedField2($paymentRequest->getCustomerRes2());
104
        $customer->setReservedField3($paymentRequest->getCustomerRes3());
105
        $customer->setReservedField4($paymentRequest->getCustomerRes4());
106
        $customer->setReservedField5($paymentRequest->getCustomerRes5());
107
108
        // add customer to order
109
        $order->setCustomer($customer);
110
111
        // create delivery object
112
        $delivery = $this->deliveryManager->create();
113
        $delivery->setAttention($paymentRequest->getDeliveryName());
114
        $delivery->setName($paymentRequest->getDeliveryCompany());
115
        $delivery->setAddress($paymentRequest->getDeliveryAddress());
116
        $delivery->setAddress2($paymentRequest->getDeliveryAddress2());
117
        $delivery->setZipCode($paymentRequest->getDeliveryZipCode());
118
        $delivery->setCity($paymentRequest->getDeliveryCity());
119
        $delivery->setCountryId((int)$paymentRequest->getDeliveryCountryID());
120
        $delivery->setCountry($paymentRequest->getDeliveryCountry());
121
        $delivery->setPhone($paymentRequest->getDeliveryPhone());
122
        $delivery->setFax($paymentRequest->getDeliveryFax());
123
        $delivery->setEmail($paymentRequest->getDeliveryEmail());
124
        $delivery->setEan($paymentRequest->getDeliveryEan());
125
126
        // create shipping method object
127
        // @todo figure out if the payment request from Dandomain contains a payment method id (check also shipping method)
128
        //$shippingMethod = $this->shippingMethodManager->findByExternalId($paymentRequest->ship)
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
129
        $order->setShippingMethod($paymentRequest->getShippingMethod());
0 ignored issues
show
Documentation introduced by
$paymentRequest->getShippingMethod() is of type string, but the function expects a null|object<Loevgaard\Da...hippingMethodInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
130
        $order->setShippingMethodFee($paymentRequest->getShippingFee());
131
        $order->setPaymentMethod($paymentRequest->getPaymentMethod());
0 ignored issues
show
Documentation introduced by
$paymentRequest->getPaymentMethod() is of type string, but the function expects a null|object<Loevgaard\Da...PaymentMethodInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
132
        $order->setPaymentMethodFee($paymentRequest->getPaymentFee());
133
134
        // add delivery to order
135
        $order->setDelivery($delivery);
136
137
        foreach ($paymentRequest->getOrderLines() as $orderLine) {
138
            $orderLineEntity = $this->orderLineManager->create();
139
            $orderLineEntity
140
                ->setProductNumber($orderLine->getProductNumber())
141
                ->setProductName($orderLine->getName())
142
                ->setQuantity($orderLine->getQuantity())
143
                ->setUnitPrice($orderLine->getPrice())
144
                ->setVatPct($orderLine->getVat())
145
            ;
146
            $order->addOrderLine($orderLineEntity);
147
        }
148
149
        // create payment object
150
        $payment = $this->create();
151
        $payment->setApiKey($paymentRequest->getApiKey());
152
        $payment->setMerchant($paymentRequest->getMerchant());
153
        $payment->setSessionId($paymentRequest->getSessionId());
154
        $payment->setCurrencySymbol($paymentRequest->getCurrencySymbol());
155
        $payment->setTotalAmount($paymentRequest->getTotalAmount());
156
        $payment->setCallBackUrl($paymentRequest->getCallBackUrl());
157
        $payment->setFullCallBackOkUrl($paymentRequest->getFullCallBackOkUrl());
158
        $payment->setCallBackOkUrl($paymentRequest->getCallBackOkUrl());
159
        $payment->setCallBackServerUrl($paymentRequest->getCallBackServerUrl());
160
        $payment->setTestMode($paymentRequest->isTestMode());
161
        $payment->setPaymentGatewayCurrencyCode($paymentRequest->getPaymentGatewayCurrencyCode());
162
        $payment->setCardTypeId($paymentRequest->getCardTypeId());
163
        $payment->setOrder($order);
164
165
        return $payment;
166
    }
167
168
    /**
169
     * @param int $id
170
     * @return PaymentInterface|null
171
     */
172
    public function findPaymentById(int $id) : ?PaymentInterface
173
    {
174
        /** @var PaymentInterface $payment */
175
        $payment = $this->getRepository()->find($id);
176
177
        return $payment;
178
    }
179
180
    /**
181
     * Setters
182
     */
183
184
    /**
185
     * @param OrderManager $orderManager
186
     * @return PaymentManager
187
     */
188
    public function setOrderManager(OrderManager $orderManager)
189
    {
190
        $this->orderManager = $orderManager;
191
        return $this;
192
    }
193
194
    /**
195
     * @param OrderLineManager $orderLineManager
196
     * @return PaymentManager
197
     */
198
    public function setOrderLineManager(OrderLineManager $orderLineManager)
199
    {
200
        $this->orderLineManager = $orderLineManager;
201
        return $this;
202
    }
203
204
    /**
205
     * @param SiteManager $siteManager
206
     * @return PaymentManager
207
     */
208
    public function setSiteManager(SiteManager $siteManager)
209
    {
210
        $this->siteManager = $siteManager;
211
        return $this;
212
    }
213
214
    /**
215
     * @param CustomerManager $customerManager
216
     * @return PaymentManager
217
     */
218
    public function setCustomerManager(CustomerManager $customerManager)
219
    {
220
        $this->customerManager = $customerManager;
221
        return $this;
222
    }
223
224
    /**
225
     * @param DeliveryManager $deliveryManager
226
     * @return PaymentManager
227
     */
228
    public function setDeliveryManager(DeliveryManager $deliveryManager)
229
    {
230
        $this->deliveryManager = $deliveryManager;
231
        return $this;
232
    }
233
234
    /**
235
     * @param PaymentMethodManager $paymentMethodManager
236
     * @return PaymentManager
237
     */
238
    public function setPaymentMethodManager(PaymentMethodManager $paymentMethodManager)
239
    {
240
        $this->paymentMethodManager = $paymentMethodManager;
241
        return $this;
242
    }
243
244
    /**
245
     * @param ShippingMethodManager $shippingMethodManager
246
     * @return PaymentManager
247
     */
248
    public function setShippingMethodManager(ShippingMethodManager $shippingMethodManager)
249
    {
250
        $this->shippingMethodManager = $shippingMethodManager;
251
        return $this;
252
    }
253
}