CartItemAdded::getCartId()   A
last analyzed

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 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace SyliusCart\Domain\Event;
6
7
use Broadway\Serializer\SerializableInterface;
8
use Money\Currency;
9
use Money\Money;
10
use Ramsey\Uuid\Uuid;
11
use Ramsey\Uuid\UuidInterface;
12
use SyliusCart\Domain\Model\CartItem;
13
use SyliusCart\Domain\ValueObject\CartItemQuantity;
14
use SyliusCart\Domain\ValueObject\ProductCode;
15
16
/**
17
 * @author Arkadiusz Krakowiak <[email protected]>
18
 */
19
final class CartItemAdded implements SerializableInterface
20
{
21
    /**
22
     * @var UuidInterface
23
     */
24
    private $cartId;
25
26
    /**
27
     * @var CartItem
28
     */
29
    private $cartItem;
30
31
    /**
32
     * @param UuidInterface $cartId
33
     * @param CartItem $cartItem
34
     */
35
    private function __construct(UuidInterface $cartId, CartItem $cartItem)
36
    {
37
        $this->cartId = $cartId;
38
        $this->cartItem = $cartItem;
39
    }
40
41
    /**
42
     * @param UuidInterface $cartId
43
     * @param CartItem $cartItem
44
     *
45
     * @return CartItemAdded
46
     */
47
    public static function occur(UuidInterface $cartId, CartItem $cartItem): self
48
    {
49
        return new self($cartId, $cartItem);
50
    }
51
52
    /**
53
     * @return UuidInterface
54
     */
55
    public function getCartId(): UuidInterface
56
    {
57
        return $this->cartId;
58
    }
59
60
    /**
61
     * @return CartItem
62
     */
63
    public function getCartItem(): CartItem
64
    {
65
        return $this->cartItem;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public static function deserialize(array $data)
72
    {
73
        return new self(
74
            Uuid::fromString($data['cartId']),
75
            new CartItem(
76
                ProductCode::fromString($data['cartItem']['productCode']),
77
                CartItemQuantity::create($data['cartItem']['quantity']),
78
                new Money($data['cartItem']['unitPrice']['amount'], new Currency($data['cartItem']['unitPrice']['currency']))
79
            )
80
        );
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function serialize()
87
    {
88
        return [
89
            'cartId' => $this->cartId->toString(),
90
            'cartItem' => [
91
                'productCode' => $this->cartItem->productCode()->__toString(),
92
                'quantity' => $this->cartItem->quantity()->getNumber(),
93
                'unitPrice' => $this->cartItem->unitPrice()->jsonSerialize(),
94
            ]
95
        ];
96
    }
97
}
98