Test Setup Failed
Push — master ( 562b48...254002 )
by Gabriel
04:15 queued 14s
created

HasStoresTrait::resolve()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 16
ccs 7
cts 8
cp 0.875
crap 3.0175
rs 9.7333
c 0
b 0
f 0
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

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