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

CanCache::checkInitFromCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 1
cp 0
crap 2
rs 10
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