Completed
Push — master ( 3229d6...c9eeb2 )
by Baldur
03:55
created

Order::getCacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace LAShowroom\TaxJarBundle\Model;
4
5
use Webmozart\Assert\Assert;
6
7
class Order
8
{
9
    /**
10
     * @var Address
11
     */
12
    private $fromAddress;
13
14
    /**
15
     * @var Address
16
     */
17
    private $toAddress;
18
19
    /**
20
     * @var float
21
     */
22
    private $amount;
23
24
    /**
25
     * @var float
26
     */
27
    private $shipping;
28
29
    /**
30
     * @var Address[]
31
     */
32
    private $nexusAddresses = [];
33
34
    /**
35
     * @var LineItem[]
36
     */
37
    private $lineItems = [];
38
39
    /**
40
     * @return Address
41
     */
42 2
    public function getFromAddress()
43
    {
44 2
        return $this->fromAddress;
45
    }
46
47
    /**
48
     * @param Address $fromAddress
49
     */
50 14
    public function setFromAddress(Address $fromAddress)
51
    {
52 14
        $this->fromAddress = $fromAddress;
53 14
    }
54
55
    /**
56
     * @return Address
57
     */
58 2
    public function getToAddress()
59
    {
60 2
        return $this->toAddress;
61
    }
62
63
    /**
64
     * @param Address $toAddress
65
     */
66 14
    public function setToAddress(Address $toAddress)
67
    {
68 14
        $this->toAddress = $toAddress;
69 14
    }
70
71
    /**
72
     * @return float
73
     */
74 12
    public function getAmount()
75
    {
76 12
        return $this->amount;
77
    }
78
79
    /**
80
     * @param float $amount
81
     */
82 12
    public function setAmount($amount)
83
    {
84 12
        Assert::float($amount);
85
86 12
        $this->amount = $amount;
87 12
    }
88
89
    /**
90
     * @return float
91
     */
92 12
    public function getShipping()
93
    {
94 12
        return $this->shipping;
95
    }
96
97
    /**
98
     * @param float $shipping
99
     */
100 12
    public function setShipping($shipping)
101
    {
102 12
        Assert::float($shipping);
103
104 12
        $this->shipping = $shipping;
105 12
    }
106
107
    /**
108
     * @return Address[]
109
     */
110 2
    public function getNexusAddresses()
111
    {
112 2
        return $this->nexusAddresses;
113
    }
114
115
    /**
116
     * @param Address[] $nexusAddresses
117
     */
118 2
    public function setNexusAddresses(array $nexusAddresses)
119
    {
120 2
        Assert::allIsInstanceOf($nexusAddresses, Address::class);
121
122 2
        $this->nexusAddresses = $nexusAddresses;
123 2
    }
124
125
    /**
126
     * @return LineItem[]
127
     */
128 2
    public function getLineItems()
129
    {
130 2
        return $this->lineItems;
131
    }
132
133
    /**
134
     * @param LineItem[] $lineItems
135
     */
136 2
    public function setLineItems(array $lineItems)
137
    {
138 2
        Assert::allIsInstanceOf($lineItems, LineItem::class);
139
140 2
        $this->lineItems = $lineItems;
141 2
    }
142
143 12
    public function addLineItem(LineItem $lineItem)
144
    {
145 12
        $this->lineItems[] = $lineItem;
146 12
    }
147
148 12
    public function addNexusAddress($id, Address $address)
149
    {
150 12
        $this->nexusAddresses[$id] = $address;
151 12
    }
152
153 12
    public function toArray()
154
    {
155
        return [
156 12
            'from_street' => $this->fromAddress->getStreet(),
157 12
            'from_city' => $this->fromAddress->getCity(),
158 12
            'from_state' => $this->fromAddress->getState(),
159 12
            'from_zip' => $this->fromAddress->getZip(),
160 12
            'from_country' => $this->fromAddress->getCountry(),
161 12
            'to_street' => $this->toAddress->getStreet(),
162 12
            'to_city' => $this->toAddress->getCity(),
163 12
            'to_state' => $this->toAddress->getState(),
164 12
            'to_zip' => $this->toAddress->getZip(),
165 12
            'to_country' => $this->toAddress->getCountry(),
166
            'line_items' => array_map(function(LineItem $elem) {
167 12
                return $elem->toArray();
168 12
            }, $this->lineItems),
169 12
            'amount' => $this->getAmount(),
170 12
            'shipping' => $this->getShipping(),
171 12
            'nexus_addresses' => array_map(function(Address $elem, $id) {
172 12
                return array_merge($elem->toArray(), ['id' => $id, ]);
173 12
            }, $this->nexusAddresses, array_keys($this->nexusAddresses)),
174
        ];
175
    }
176
177 8
    public function getCacheKey()
178
    {
179 8
        return sprintf('order_%s', md5(json_encode($this->toArray())));
180
    }
181
}
182