Passed
Push — master ( b33b8e...5acb2c )
by Gabriel
04:09
created

IsCachedTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 53.85%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 9
c 1
b 0
f 1
dl 0
loc 31
ccs 7
cts 13
cp 0.5385
rs 10
wmc 5

4 Methods

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

34
        return $this->/** @scrutinizer ignore-call */ all();
Loading history...
35
    }
36
37 1
    protected function doLoad(): void
38
    {
39 1
        $data = $this->getDataFromCache();
40 1
        if (is_array($data)) {
41
            $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

41
            $this->/** @scrutinizer ignore-call */ 
42
                   setItems($data);
Loading history...
42
        }
43 1
    }
44
}
45