Completed
Push — master ( 096858...7ec34d )
by Gabriel
03:50
created

CanCache   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 61
ccs 18
cts 24
cp 0.75
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A initFromCache() 0 4 1
A checkInitFromCache() 0 2 1
A generateCacheStore() 0 7 2
A checkSaveCache() 0 7 2
A needsCaching() 0 6 2
A cacheStore() 0 6 2
1
<?php
2
3
namespace Nip\Records\Mapping\MappingManager;
4
5
6
use DateInterval;
7
use Nip\Cache\Stores\Repository;
8
use Nip\Container\Container;
9
10
/**
11
 * Trait CanCache
12
 * @package Nip\Records\Mapping\MappingManager
13
 */
14
trait CanCache
15
{
16
    protected $needsCaching = false;
17
18
    protected $cacheStore = null;
19
20
    /**
21
     * @param null $needCaching
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $needCaching is correct as it would always require null to be passed?
Loading history...
22
     * @return bool
23
     */
24 3
    public function needsCaching($needCaching = null): bool
25
    {
26 3
        if (is_bool($needCaching)) {
0 ignored issues
show
introduced by
The condition is_bool($needCaching) is always false.
Loading history...
27 3
            $this->needsCaching = $needCaching;
28
        }
29 3
        return $this->needsCaching;
30
    }
31
32
    protected function checkInitFromCache()
33
    {
34
35
    }
36
37 1
    protected function initFromCache()
38
    {
39 1
        $data = $this->cacheStore()->get('orm.mapping.data');
40 1
        $this->repository->initFromCache($data);
41 1
    }
42
43
44 1
    protected function checkSaveCache()
45
    {
46 1
        if ($this->needsCaching() !== true) {
47 1
            return;
48
        }
49
        $data = $this->repository->generateCache();
50
        $this->cacheStore()->set('orm.mapping.data', $data, DateInterval::createFromDateString('10 years'));
51
    }
52
53
54
    /**
55
     * @return Repository
56
     */
57 1
    protected function cacheStore()
58
    {
59 1
        if ($this->cacheStore === null) {
60 1
            $this->cacheStore = $this->generateCacheStore();
61
        }
62 1
        return $this->cacheStore;
63
    }
64
65
    /**
66
     * @return Repository
67
     */
68 1
    protected function generateCacheStore()
69
    {
70 1
        if (function_exists('app')) {
71
            return app('cache.store');
72
        }
73
74 1
        return Container::getInstance()->get('cache.store');
75
    }
76
}
77