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
![]() |
|||||||
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
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
![]() |
|||||||
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
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
![]() |
|||||||
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 |