FixedPerTicket::getDiscount()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ConferenceTools\Tickets\Domain\ValueObject\DiscountType;
4
5
use ConferenceTools\Tickets\Domain\Service\Configuration;
6
use ConferenceTools\Tickets\Domain\ValueObject\Basket;
7
use ConferenceTools\Tickets\Domain\ValueObject\Money;
8
use ConferenceTools\Tickets\Domain\ValueObject\Price;
9
use JMS\Serializer\Annotation as Jms;
10
11
class FixedPerTicket implements DiscountTypeInterface
12
{
13
    /**
14
     * @JMS\Type("Price")
15
     * @var Price
16
     */
17
    private $discount;
18
19
    /**
20
     * Percentage constructor.
21
     * @param $discount
22
     */
23 5
    public function __construct(Price $discount)
24
    {
25 5
        $this->discount = $discount;
26 5
    }
27
28 5
    public function apply(Basket $to): Price
29
    {
30 5
        $tickets = count($to->getTickets());
31 5
        return $this->discount->multiply($tickets);
32
    }
33
34
    /**
35
     * @return Price
36
     */
37
    public function getDiscount(): Price
38
    {
39
        return $this->discount;
40
    }
41
42
    public static function fromArray(array $data, Configuration $configuration): DiscountTypeInterface
43
    {
44
        if (isset($data['net'])) {
45
            $amount = new Money($data['net'], $configuration->getCurrency());
46
            $discount = Price::fromNetCost($amount, $configuration->getTaxRate());
47
        } else {
48
            $amount = new Money($data['gross'], $configuration->getCurrency());
49
            $discount = Price::fromGrossCost($amount, $configuration->getTaxRate());
50
        }
51
52
        return new static($discount);
53
    }
54
}
55