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