Completed
Push — master ( 1beb5a...c55f61 )
by Joachim
16:30
created

PaymentManager   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 104
Duplicated Lines 20.19 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 5
dl 21
loc 104
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
C createPaymentFromDandomainPaymentRequest() 21 35 7
A findByOrderIdOrAltapayId() 0 16 2
A create() 0 4 1
A delete() 0 4 1
A update() 0 4 1
A setPaymentLineManager() 0 4 1

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
3
namespace Loevgaard\DandomainAltapayBundle\Manager;
4
5
use Loevgaard\Dandomain\Pay\PaymentRequest;
6
use Loevgaard\DandomainAltapayBundle\Entity\Payment;
7
use Loevgaard\DoctrineManager\Manager;
8
9
class PaymentManager extends Manager
10
{
11
    /**
12
     * @var PaymentLineManager
13
     */
14
    protected $paymentLineManager;
15
16
    /**
17
     * This will transform a PaymentRequest (parent) to a Payment (child).
18
     *
19
     * @param PaymentRequest $paymentRequest
20
     *
21
     * @return Payment
22
     */
23
    public function createPaymentFromDandomainPaymentRequest(PaymentRequest $paymentRequest)
24
    {
25
        $payment = $this->create();
26
27
        $methods = get_class_methods($paymentRequest);
28
29 View Code Duplication
        foreach ($methods as $method) {
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...
30
            if ('get' === substr($method, 0, 3)) {
31
                $val = $paymentRequest->{$method}();
32
                $property = substr($method, 3);
33
            } elseif ('is' === substr($method, 0, 2)) {
34
                $val = $paymentRequest->{$method}();
35
                $property = substr($method, 2);
36
            } else {
37
                continue;
38
            }
39
40
            if (!is_scalar($val)) {
41
                continue;
42
            }
43
44
            $setter = 'set'.$property;
45
46
            if (method_exists($payment, $setter)) {
47
                $payment->{$setter}($val);
48
            }
49
        }
50
51
        foreach ($paymentRequest->getPaymentLines() as $paymentLine) {
52
            $newPaymentLine = $this->paymentLineManager->createPaymentLineFromDandomainPaymentRequest($paymentLine);
53
            $payment->addPaymentLine($newPaymentLine);
54
        }
55
56
        return $payment;
57
    }
58
59
    /**
60
     * @param $id
61
     * @return null|Payment
62
     */
63
    public function findByOrderIdOrAltapayId($id)
64
    {
65
        /** @var Payment $payment */
66
        $payment = $this->getRepository()->findOneBy([
67
            'orderId' => $id
68
        ]);
69
70
        if(!$payment) {
71
            /** @var Payment $payment */
72
            $payment = $this->getRepository()->findOneBy([
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $payment is correct as $this->getRepository()->...ay('altapayId' => $id)) (which targets Doctrine\Common\Persiste...Repository::findOneBy()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
73
                'altapayId' => $id
74
            ]);
75
        }
76
77
        return $payment;
78
    }
79
80
    /**
81
     * @return Payment
82
     */
83
    public function create()
84
    {
85
        return parent::create();
86
    }
87
88
    /**
89
     * @param Payment $obj
90
     */
91
    public function delete($obj)
92
    {
93
        parent::delete($obj);
94
    }
95
96
    /**
97
     * @param Payment $obj
98
     * @param bool    $flush
99
     */
100
    public function update($obj, $flush = true)
101
    {
102
        parent::update($obj, $flush);
103
    }
104
105
    /**
106
     * @param PaymentLineManager $paymentLineManager
107
     */
108
    public function setPaymentLineManager(PaymentLineManager $paymentLineManager)
109
    {
110
        $this->paymentLineManager = $paymentLineManager;
111
    }
112
}
113