HasCacheStore::generateCacheStoreInMemory()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 3
c 1
b 0
f 1
dl 0
loc 5
rs 10
ccs 0
cts 4
cp 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Nip\Cache\Cacheable;
4
5
use League\Container\Exception\NotFoundException;
0 ignored issues
show
Bug introduced by
The type League\Container\Exception\NotFoundException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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