Completed
Push — master ( e16295...10cfb1 )
by Joachim
09:10
created

PaymentLine   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 4 1
A createFromDandomainPaymentLine() 0 16 2
A setId() 0 6 1
A setPrice() 0 9 1
A getPrice() 0 13 3
1
<?php
2
3
namespace Loevgaard\DandomainAltapayBundle\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use Loevgaard\Dandomain\Pay\Model\PaymentLine as BasePaymentLine;
7
use Loevgaard\Dandomain\Pay\Model\PaymentLine as DandomainPaymentLine;
8
use Money\Currency;
9
use Money\Money;
10
use Symfony\Component\Validator\Constraints as Assert;
11
12
/**
13
 * @ORM\Table(name="dandomain_altapay_payment_lines")
14
 * @ORM\Entity()
15
 */
16
class PaymentLine extends BasePaymentLine
17
{
18
    /**
19
     * @var int
20
     *
21
     * @ORM\Id
22
     * @ORM\Column(name="id", type="integer")
23
     * @ORM\GeneratedValue(strategy="AUTO")
24
     */
25
    protected $id;
26
27
    /**
28
     * @Assert\NotBlank()
29
     * @Assert\Length(max="191")
30
     *
31
     * @ORM\Column(type="string", length=191)
32
     */
33
    protected $productNumber;
34
35
    /**
36
     * @Assert\NotBlank()
37
     * @Assert\Length(max="191")
38
     *
39
     * @ORM\Column(type="string", length=191)
40
     */
41
    protected $name;
42
43
    /**
44
     * @Assert\NotBlank()
45
     * @Assert\GreaterThan(0)
46
     *
47
     * @ORM\Column(type="integer")
48
     */
49
    protected $quantity;
50
51
    /**
52
     * @Assert\NotBlank()
53
     *
54
     * @ORM\Column(type="integer")
55
     */
56
    protected $priceAmount;
57
58
    /**
59
     * @Assert\NotBlank()
60
     *
61
     * @ORM\Column(type="string")
62
     */
63
    protected $priceCurrency;
64
65
    /**
66
     * @Assert\NotBlank()
67
     * @Assert\GreaterThanOrEqual(0)
68
     *
69
     * @ORM\Column(type="integer")
70
     */
71
    protected $vat;
72
73
    /**
74
     * @Assert\NotNull()
75
     *
76
     * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
77
     * @ORM\ManyToOne(targetEntity="Payment", inversedBy="paymentLines")
78
     */
79
    protected $payment;
80
81
    /**
82
     * This will transform a Dandomain Payment Line (parent) to a Payment Line (child).
83
     *
84
     * @param DandomainPaymentLine $dandomainPaymentLine
85
     *
86
     * @return PaymentLine
87
     */
88
    public static function createFromDandomainPaymentLine(DandomainPaymentLine $dandomainPaymentLine)
89
    {
90
        $paymentLine = new self(
91
            $dandomainPaymentLine->getProductNumber(),
92
            $dandomainPaymentLine->getName(),
93
            $dandomainPaymentLine->getQuantity(),
94
            $dandomainPaymentLine->getPrice(),
95
            $dandomainPaymentLine->getVat()
96
        );
97
98
        if ($dandomainPaymentLine->getPayment()) {
99
            $paymentLine->setPayment($dandomainPaymentLine->getPayment());
100
        }
101
102
        return $paymentLine;
103
    }
104
105
    /**
106
     * @return int
107
     */
108
    public function getId(): ?int
109
    {
110
        return $this->id;
111
    }
112
113
    /**
114
     * @param mixed $id
115
     *
116
     * @return PaymentLine
117
     */
118
    public function setId($id): self
119
    {
120
        $this->id = $id;
121
122
        return $this;
123
    }
124
125
    public function setPrice(Money $price): DandomainPaymentLine
126
    {
127
        parent::setPrice($price);
128
129
        $this->priceAmount = $price->getAmount();
130
        $this->priceCurrency = $price->getCurrency()->getCode();
131
132
        return $this;
133
    }
134
135
    public function getPrice(): Money
136
    {
137
        if (!$this->priceCurrency) {
138
            return null;
139
        }
140
141
        $amount = $this->priceAmount;
142
        if (!$amount) {
143
            $amount = 0;
144
        }
145
146
        return new Money($amount, new Currency($this->priceCurrency));
147
    }
148
}
149