Test Setup Failed
Push — master ( 90378d...afbbde )
by Gabriel
06:45
created

HasCacheStore::generateCacheStore()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.5

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 8
ccs 2
cts 4
cp 0.5
crap 2.5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Nip\Cache\Cacheable;
4
5
use League\Container\Exception\NotFoundException;
6
use Nip\Cache\Stores\Repository;
7
use Nip\Container\Container;
8
use Symfony\Component\Cache\Adapter\ArrayAdapter;
9
10
/**
11
 * Trait HasCacheStore
12
 * @package Nip\Cache\Cacheable
13
 */
14
trait HasCacheStore
15
{
16
    protected $cacheStore = null;
17
18
    /**
19
     * @return Repository
20
     */
21 1
    protected function cacheStore()
22
    {
23 1
        if ($this->cacheStore === null) {
24 1
            $this->cacheStore = $this->generateCacheStore();
25
        }
26 1
        return $this->cacheStore;
27
    }
28
29
    /**
30
     * @return Repository
31
     */
32 1
    protected function generateCacheStore()
33
    {
34
        try {
35 1
            return $this->generateCacheStoreFromContainer();
36
        } catch (NotFoundException $exception) {
37
            return $this->generateCacheStoreInMemory();
38
        }
39
    }
40
41
    /**
42
     * @return Repository
43
     */
44
    protected function generateCacheStoreInMemory()
45
    {
46
        $adapter = new ArrayAdapter();
47
        $store = new Repository($adapter);
48
        return $store;
49
    }
50
51
    /**
52
     * @return Repository
53
     * @throws NotFoundException
54
     */
55 1
    protected function generateCacheStoreFromContainer()
56
    {
57 1
        if (function_exists('app')) {
58 1
            return app('cache.store');
59
        }
60
61
        return Container::getInstance()->get('cache.store');
62
    }
63
}
64