HasStoresTrait::store()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
ccs 3
cts 3
cp 1
cc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Nip\Cache\CacheManager;
4
5
use InvalidArgumentException;
6
use Nip\Cache\Stores\Repository;
7
8
/**
9
 * Trait HasRepositoryTrait
10
 * @package Nip\Cache\Traits
11
 */
12
trait HasStoresTrait
13
{
14
    /**
15
     * The array of resolved cache stores.
16
     *
17
     * @var array
18
     */
19
    protected $stores = [];
20
21
    /**
22
     * Get a cache store instance by name.
23
     *
24
     * @param string|null $name
25
     * @return Repository
26
     */
27 3
    public function store($name = null)
28
    {
29 3
        $name = $name ?: $this->getDefaultStore();
30 3
        return $this->getStore($name);
31
    }
32
33
    /**
34
     * Get the default cache driver name.
35
     *
36
     * @return string
37
     */
38 2
    public function getDefaultStore()
39
    {
40 2
        return $this->getConfig('default', 'file');
0 ignored issues
show
Bug introduced by
It seems like getConfig() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
        return $this->/** @scrutinizer ignore-call */ getConfig('default', 'file');
Loading history...
41
    }
42
43
    /**
44
     * Attempt to get the store from the local cache.
45
     *
46
     * @param string $name
47
     * @return Repository
48
     */
49 2
    protected function getStore($name)
50
    {
51 2
        $this->stores[$name] = $this->stores[$name] ?? $this->resolve($name);
52 2
        return $this->stores[$name];
53
    }
54
55
    /**
56
     * @param $name
57
     * @return Repository
58
     */
59 2
    protected function resolve($name)
60
    {
61 2
        $config = $this->getConfigStore($name);
0 ignored issues
show
Bug introduced by
It seems like getConfigStore() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
        /** @scrutinizer ignore-call */ 
62
        $config = $this->getConfigStore($name);
Loading history...
62
63 2
        if (is_null($config)) {
64 2
            if ($name == 'file') {
65 2
                $config = ['driver' => 'file'];
66
            } else {
67
                throw new InvalidArgumentException("Cache store [{$name}] is not defined.");
68
            }
69
        }
70
71 2
        return $this->repository(
72 2
            $this->createDriver($config['driver'], $config)
0 ignored issues
show
Bug introduced by
It seems like createDriver() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
            $this->/** @scrutinizer ignore-call */ 
73
                   createDriver($config['driver'], $config)
Loading history...
73
        );
74
    }
75
76
    /**
77
     * @param $driver
78
     * @return Repository
79
     */
80 2
    protected function repository($driver)
81
    {
82 2
        return new Repository($driver);
83
    }
84
}
85