Passed
Push — master ( 3d74f3...79b5b3 )
by Gabriel
02:17
created

IsCachedTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 40.91%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
eloc 18
c 2
b 1
f 1
dl 0
loc 46
ccs 9
cts 22
cp 0.4091
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __destruct() 0 3 1
A offsetSet() 0 4 1
A generateCacheData() 0 7 1
A doLoad() 0 15 3
1
<?php
2
3
namespace Nip\Records\Registry\Traits;
4
5
use Nip\Cache\Cacheable\CanCache;
6
use Nip\Records\Instantiator\Instantiator;
7
8
/**
9
 * Trait IsCachedTrait
10
 * @package Nip\Records\Registry\Traits
11
 */
12
trait IsCachedTrait
13
{
14
    use CanCache;
15
16
    /**
17
     * @inheritDoc
18
     */
19 26
    public function offsetSet($key, $value)
20
    {
21 26
        $this->needsCaching(true);
22 26
        parent::offsetSet($key, $value);
23 26
    }
24
25
    public function __destruct()
26
    {
27
        $this->checkSaveCache();
28
    }
29
30
    /**
31
     * @return array
32
     */
33
    protected function generateCacheData()
34
    {
35
        return array_map(
36
            function ($manager) {
37
                return get_class($manager);
38
            },
39
            $this->all()
0 ignored issues
show
Bug introduced by
It seems like all() 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

39
            $this->/** @scrutinizer ignore-call */ 
40
                   all()
Loading history...
40
        );
41
    }
42
43 1
    protected function doLoad(): void
44
    {
45 1
        $data = $this->getDataFromCache();
46
47 1
        $instantiator = new Instantiator();
48 1
        $instantiator->setModelRegistry($this);
49
50 1
        if (is_array($data) && count($data)) {
51
            $data = array_map(
52
                function ($manager) use ($instantiator){
53
                    return $instantiator->instantiate($manager);
54
                },
55
                $data
56
            );
57
            $this->setItems($data);
0 ignored issues
show
Bug introduced by
It seems like setItems() 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

57
            $this->/** @scrutinizer ignore-call */ 
58
                   setItems($data);
Loading history...
58
        }
59 1
    }
60
}
61