Completed
Push — master ( 636a3e...0d9cfe )
by Joachim
12:23
created

PaymentManager   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 150
Duplicated Lines 5.33 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 7
dl 8
loc 150
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getRepository() 0 4 1
A getClass() 8 8 2
A createPayment() 0 6 1
A createPaymentFromDandomainPaymentRequest() 0 69 2
A deletePayment() 0 5 1
A updatePayment() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Loevgaard\DandomainAltapayBundle\Manager;
3
4
use Doctrine\Common\Persistence\ObjectManager;
5
use Doctrine\Common\Persistence\ObjectRepository;
6
use Loevgaard\Dandomain\Pay\PaymentRequest;
7
use Loevgaard\DandomainAltapayBundle\Entity\OrderLineInterface;
8
use Loevgaard\DandomainAltapayBundle\Entity\PaymentInterface;
9
use Loevgaard\DandomainAltapayBundle\Entity\TerminalInterface;
10
11
class PaymentManager
12
{
13
    /**
14
     * @var ObjectManager
15
     */
16
    protected $objectManager;
17
18
    /**
19
     * @var OrderLineManager
20
     */
21
    protected $orderLineManager;
22
23
    /**
24
     * @var string
25
     */
26
    protected $class;
27
28
    public function __construct(ObjectManager $objectManager, OrderLineManager $orderLineManager, string $class)
29
    {
30
        $this->objectManager = $objectManager;
31
        $this->orderLineManager = $orderLineManager;
32
        $this->class = $class;
33
    }
34
35
    /**
36
     * @return ObjectRepository
37
     */
38
    protected function getRepository() : ObjectRepository
39
    {
40
        return $this->objectManager->getRepository($this->getClass());
41
    }
42
43
    /**
44
     * @return string
45
     */
46 View Code Duplication
    public function getClass() : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
47
    {
48
        if (false !== strpos($this->class, ':')) {
49
            $metadata = $this->objectManager->getClassMetadata($this->class);
50
            $this->class = $metadata->getName();
51
        }
52
        return $this->class;
53
    }
54
55
    /**
56
     * @return PaymentInterface
57
     */
58
    public function createPayment() : PaymentInterface
59
    {
60
        $class = $this->getClass();
61
        $terminal = new $class();
62
        return $terminal;
63
    }
64
65
    /**
66
     * @param PaymentRequest $paymentRequest
67
     * @return PaymentInterface
68
     */
69
    public function createPaymentFromDandomainPaymentRequest(PaymentRequest $paymentRequest) : PaymentInterface
70
    {
71
        $payment = $this->createPayment();
72
        $payment->setApiKey($paymentRequest->getApiKey());
73
        $payment->setMerchant($paymentRequest->getMerchant());
74
        $payment->setOrderId($paymentRequest->getOrderId());
75
        $payment->setSessionId($paymentRequest->getSessionId());
76
        $payment->setCurrencySymbol($paymentRequest->getCurrencySymbol());
77
        $payment->setTotalAmount($paymentRequest->getTotalAmount());
78
        $payment->setCallBackUrl($paymentRequest->getCallBackUrl());
79
        $payment->setFullCallBackOkUrl($paymentRequest->getFullCallBackOkUrl());
80
        $payment->setCallBackOkUrl($paymentRequest->getCallBackOkUrl());
81
        $payment->setCallBackServerUrl($paymentRequest->getCallBackServerUrl());
82
        $payment->setLanguageId((int)$paymentRequest->getLanguageId());
83
        $payment->setTestMode($paymentRequest->isTestMode());
84
        $payment->setPaymentGatewayCurrencyCode($paymentRequest->getPaymentGatewayCurrencyCode());
85
        $payment->setCardTypeId($paymentRequest->getCardTypeId());
86
        $payment->setCustomerRekvNr($paymentRequest->getCustomerRekvNr());
87
        $payment->setCustomerName($paymentRequest->getCustomerName());
88
        $payment->setCustomerCompany($paymentRequest->getCustomerCompany());
89
        $payment->setCustomerAddress($paymentRequest->getCustomerAddress());
90
        $payment->setCustomerAddress2($paymentRequest->getCustomerAddress2());
91
        $payment->setCustomerZipCode($paymentRequest->getCustomerZipCode());
92
        $payment->setCustomerCity($paymentRequest->getCustomerCity());
93
        $payment->setCustomerCountryId((int)$paymentRequest->getCustomerCountryId());
94
        $payment->setCustomerCountry($paymentRequest->getCustomerCountry());
95
        $payment->setCustomerPhone($paymentRequest->getCustomerPhone());
96
        $payment->setCustomerFax($paymentRequest->getCustomerFax());
97
        $payment->setCustomerEmail($paymentRequest->getCustomerEmail());
98
        $payment->setCustomerNote($paymentRequest->getCustomerNote());
99
        $payment->setCustomerCvrnr($paymentRequest->getCustomerCvrnr());
100
        $payment->setCustomerCustTypeId($paymentRequest->getCustomerCustTypeId());
101
        $payment->setCustomerEan($paymentRequest->getCustomerEan());
102
        $payment->setCustomerRes1($paymentRequest->getCustomerRes1());
103
        $payment->setCustomerRes2($paymentRequest->getCustomerRes2());
104
        $payment->setCustomerRes3($paymentRequest->getCustomerRes3());
105
        $payment->setCustomerRes4($paymentRequest->getCustomerRes4());
106
        $payment->setCustomerRes5($paymentRequest->getCustomerRes5());
107
        $payment->setDeliveryName($paymentRequest->getDeliveryName());
108
        $payment->setDeliveryCompany($paymentRequest->getDeliveryCompany());
109
        $payment->setDeliveryAddress($paymentRequest->getDeliveryAddress());
110
        $payment->setDeliveryAddress2($paymentRequest->getDeliveryAddress2());
111
        $payment->setDeliveryZipCode($paymentRequest->getDeliveryZipCode());
112
        $payment->setDeliveryCity($paymentRequest->getDeliveryCity());
113
        $payment->setDeliveryCountryID((int)$paymentRequest->getDeliveryCountryID());
114
        $payment->setDeliveryCountry($paymentRequest->getDeliveryCountry());
115
        $payment->setDeliveryPhone($paymentRequest->getDeliveryPhone());
116
        $payment->setDeliveryFax($paymentRequest->getDeliveryFax());
117
        $payment->setDeliveryEmail($paymentRequest->getDeliveryEmail());
118
        $payment->setDeliveryEan($paymentRequest->getDeliveryEan());
119
        $payment->setShippingMethod($paymentRequest->getShippingMethod());
120
        $payment->setShippingFee($paymentRequest->getShippingFee());
121
        $payment->setPaymentMethod($paymentRequest->getPaymentMethod());
122
        $payment->setPaymentFee($paymentRequest->getPaymentFee());
123
        $payment->setCustomerIp($paymentRequest->getCustomerIp());
124
        $payment->setLoadBalancerRealIp($paymentRequest->getLoadBalancerRealIp());
125
126
        foreach ($paymentRequest->getOrderLines() as $orderLine) {
127
            $orderLineEntity = $this->orderLineManager->createOrderLine();
128
            $orderLineEntity
129
                ->setProductNumber($orderLine->getProductNumber())
130
                ->setName($orderLine->getName())
131
                ->setQuantity($orderLine->getQuantity())
132
                ->setPrice($orderLine->getPrice())
133
                ->setVat($orderLine->getVat())
134
            ;
135
            $payment->addOrderLine($orderLineEntity);
136
        }
137
    }
138
139
    /**
140
     * @param PaymentInterface $payment
141
     */
142
    public function deletePayment(PaymentInterface $payment)
143
    {
144
        $this->objectManager->remove($payment);
145
        $this->objectManager->flush();
146
    }
147
148
    /**
149
     * @param PaymentInterface $payment
150
     * @param bool $flush
151
     */
152
    public function updatePayment(PaymentInterface $payment, bool $flush = true)
153
    {
154
        $this->objectManager->persist($payment);
155
156
        if($flush) {
157
            $this->objectManager->flush();
158
        }
159
    }
160
}