|
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 ConferenceTools\Tickets\Domain\ValueObject\TicketType; |
|
10
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
11
|
|
|
use JMS\Serializer\Annotation as Jms; |
|
12
|
|
|
|
|
13
|
|
|
class RestrictedToTicketType implements DiscountTypeInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @JMS\Type("Object") |
|
17
|
|
|
* @var DiscountTypeInterface |
|
18
|
|
|
*/ |
|
19
|
|
|
private $discountType; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @JMS\Type("array<ConferenceTools\Tickets\Domain\ValueObject\TicketType>") |
|
23
|
|
|
* @var TicketType[] |
|
24
|
|
|
*/ |
|
25
|
|
|
private $ticketTypes; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Percentage constructor. |
|
29
|
|
|
* @param $discount |
|
30
|
|
|
*/ |
|
31
|
3 |
|
public function __construct(DiscountTypeInterface $discount, TicketType ...$ticketTypes) |
|
32
|
|
|
{ |
|
33
|
3 |
|
$this->discountType = $discount; |
|
34
|
3 |
|
$this->ticketTypes = $ticketTypes; |
|
35
|
3 |
|
} |
|
36
|
|
|
|
|
37
|
3 |
|
public function apply(Basket $to): Price |
|
38
|
|
|
{ |
|
39
|
|
|
try { |
|
40
|
3 |
|
return $this->discountType->apply($to->containingOnly(...$this->ticketTypes)); |
|
41
|
1 |
|
} catch (\InvalidArgumentException $e) { |
|
42
|
1 |
|
return Price::fromNetCost( |
|
43
|
1 |
|
new Money(0, $to->getPreDiscountTotal()->getNet()->getCurrency()), |
|
44
|
1 |
|
$to->getPreDiscountTotal()->getTaxRate() |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function getDiscountType(): DiscountTypeInterface |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->discountType; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public static function fromArray(array $data, Configuration $configuration): DiscountTypeInterface |
|
55
|
|
|
{ |
|
56
|
|
|
$delegated = call_user_func([$data['discountType'], 'fromArray'], $data['options'], $configuration); |
|
57
|
|
|
$ticketTypes = []; |
|
58
|
|
|
|
|
59
|
|
|
foreach ((array) $data['allowedTicketTypes'] as $ticketType) { |
|
60
|
|
|
$ticketTypes[] = $configuration->getTicketType($ticketType); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return new static($delegated, ...$ticketTypes); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|