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

Client   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 51
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getTaxesForOrder() 0 17 3
A setCacheItemPool() 0 4 1
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 6
    public function __construct(TaxJarClient $apiClient)
26
    {
27 6
        $this->apiClient = $apiClient;
28 6
    }
29
30
    /**
31
     * @param Order $order
32
     *
33
     * @return TaxResponse
34
     */
35 6
    public function getTaxesForOrder(Order $order)
36
    {
37 6
        if (null === $this->cacheItemPool) {
38 4
            return new TaxResponse($this->apiClient->taxForOrder($order->toArray()));
39
        }
40
41
        /** @var \Symfony\Component\Cache\CacheItem $cacheItem */
42 4
        $cacheItem = $this->cacheItemPool->getItem($order->getCacheKey());
43
44 4
        if (!$cacheItem->isHit()) {
45 2
            $cacheItem->set(new TaxResponse($this->apiClient->taxForOrder($order->toArray())));
46 2
            $this->cacheItemPool->save($cacheItem);
47 2
            $cacheItem->expiresAt(new \DateTime('+1 day'));
48
        }
49
50 4
        return $cacheItem->get();
51
    }
52
53
    /**
54
     * @param CacheItemPoolInterface $cacheItemPool
55
     */
56 4
    public function setCacheItemPool(CacheItemPoolInterface $cacheItemPool)
57
    {
58 4
        $this->cacheItemPool = $cacheItemPool;
59 4
    }
60
}
61