Completed
Push — master ( 7c1eaa...80d841 )
by Joachim
15:53
created

PaymentManager::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 2
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
     * @return Payment
61
     */
62
    public function create()
63
    {
64
        return parent::create();
65
    }
66
67
    /**
68
     * @param Payment $obj
69
     */
70
    public function delete($obj)
71
    {
72
        parent::delete($obj);
73
    }
74
75
    /**
76
     * @param Payment $obj
77
     * @param bool    $flush
78
     */
79
    public function update($obj, $flush = true)
80
    {
81
        parent::update($obj, $flush);
82
    }
83
84
    /**
85
     * @param PaymentLineManager $paymentLineManager
86
     */
87
    public function setPaymentLineManager(PaymentLineManager $paymentLineManager)
88
    {
89
        $this->paymentLineManager = $paymentLineManager;
90
    }
91
}
92