1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LAShowroom\TaxJarBundle; |
4
|
|
|
|
5
|
|
|
use LAShowroom\TaxJarBundle\Model\Order; |
6
|
|
|
use LAShowroom\TaxJarBundle\Model\Response\TaxResponse; |
7
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
8
|
|
|
use TaxJar\Client as TaxJarClient; |
9
|
|
|
|
10
|
|
|
class Client |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var TaxJarClient |
14
|
|
|
*/ |
15
|
|
|
private $apiClient; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var CacheItemPoolInterface |
19
|
|
|
*/ |
20
|
|
|
private $cacheItemPool; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param TaxJarClient $apiClient |
24
|
|
|
*/ |
25
|
8 |
|
public function __construct(TaxJarClient $apiClient) |
26
|
|
|
{ |
27
|
8 |
|
$this->apiClient = $apiClient; |
28
|
8 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param Order $order |
32
|
|
|
* |
33
|
|
|
* @return TaxResponse |
34
|
|
|
*/ |
35
|
6 |
|
public function getTaxesForOrder(Order $order) |
36
|
|
|
{ |
37
|
6 |
|
$request = $order->toArray(); |
38
|
6 |
|
unset($request['transaction_id']); |
39
|
6 |
|
unset($request['transaction_date']); |
40
|
|
|
|
41
|
6 |
|
if (null === $this->cacheItemPool) { |
42
|
4 |
|
return new TaxResponse($this->apiClient->taxForOrder($request)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** @var \Symfony\Component\Cache\CacheItem $cacheItem */ |
46
|
4 |
|
$cacheItem = $this->cacheItemPool->getItem($order->getCacheKey()); |
47
|
|
|
|
48
|
4 |
|
if (!$cacheItem->isHit()) { |
49
|
2 |
|
$cacheItem->set(new TaxResponse($this->apiClient->taxForOrder($request))); |
50
|
2 |
|
$this->cacheItemPool->save($cacheItem); |
51
|
2 |
|
$cacheItem->expiresAt(new \DateTime('+1 day')); |
52
|
|
|
} |
53
|
|
|
|
54
|
4 |
|
return $cacheItem->get(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param Order $order |
59
|
|
|
* |
60
|
|
|
* @return TaxResponse |
61
|
|
|
*/ |
62
|
2 |
|
public function createOrderTransaction(Order $order) |
63
|
|
|
{ |
64
|
2 |
|
$request = $order->toArray(); |
65
|
|
|
|
66
|
2 |
|
$response = new TaxResponse($this->apiClient->createOrder($request)); |
67
|
|
|
|
68
|
2 |
|
return $response; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param CacheItemPoolInterface $cacheItemPool |
73
|
|
|
*/ |
74
|
4 |
|
public function setCacheItemPool(CacheItemPoolInterface $cacheItemPool) |
75
|
|
|
{ |
76
|
4 |
|
$this->cacheItemPool = $cacheItemPool; |
77
|
4 |
|
} |
78
|
|
|
} |
79
|
|
|
|