Passed
Push — master ( 32db04...fadd64 )
by Vasyl
02:53
created

VariableManager::useCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Fomvasss\Variable;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Collection;
7
8
/**
9
 * Class VariableManager
10
 *
11
 * @package \Fomvasss\Variable
12
 */
13
class VariableManager implements VariableManagerContract
14
{
15
    protected $app;
16
17
    /** @var \Fomvasss\Variable\Models\Variable */
18
    protected $variableModel;
19
20
    /** @var \Illuminate\Cache\Repository */
21
    protected $cacheRepository;
22
23
    /** @var string */
24
    protected $langcode = null;
25
26
    /** @var \Illuminate\Database\Eloquent\Collection */
27
    protected $variables = null;
28
29
    /** @var bool */
30
    protected $isUseCache;
31
32
    /**
33
     * VariableManager constructor.
34
     * @param $variableModel
35
     * @param $cacheRepository
36
     * @param null $app
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $app is correct as it would always require null to be passed?
Loading history...
37
     */
38
    public function __construct($variableModel, $cacheRepository, $app = null)
39
    {
40
        if (! $app) {
0 ignored issues
show
introduced by
$app is of type null, thus it always evaluated to false.
Loading history...
41
            $app = app();   //Fallback when $app is not given
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
            $app = /** @scrutinizer ignore-call */ app();   //Fallback when $app is not given
Loading history...
42
        }
43
        $this->app = $app;
44
45
        $this->config = $this->app['config']->get('variables');
0 ignored issues
show
Bug Best Practice introduced by
The property config does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
46
        
47
        $this->isUseCache = $this->config['cache']['is_use_cache'] ?? true;
48
49
        $this->variableModel = $variableModel;
50
51
        $this->cacheRepository = $cacheRepository;
52
    }
53
54
    /**
55
     * @param string|null $langcode
56
     * @param bool|null $isUseCache
57
     * @return array|\Illuminate\Database\Eloquent\Collection
58
     */
59
    public function all(?string $langcode = null, ?bool $isUseCache = null)
60
    {
61
        $langcode = $langcode ?: $this->langcode;
62
        $isUseCache = $isUseCache === null
63
            ? $this->isUseCache
64
            : $isUseCache;
65
66
        if ($langcode) {
67
            return $this->getCollection($isUseCache)->where('langcode', $langcode);
68
        }
69
70
        return $this->getCollection($isUseCache);
71
    }
72
    
73
    /**
74
     * @param string $key
75
     * @param null $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
76
     * @param string|null $langcode
77
     * @param bool $useCache
78
     * @return mixed|null
79
     */
80
    public function get(string $key, $default = null, ?string $langcode = null, ?bool $isUseCache = null)
81
    {
82
        $langcode = $langcode ?: $this->langcode;
83
        $isUseCache = $isUseCache === null
84
            ? $this->isUseCache
85
            : $isUseCache;
86
87
        if ($var = $this->getCollection($isUseCache)
88
            ->where('key', $key)
89
            ->where('langcode', $langcode)
90
            ->first()) {
91
92
            return $var->value ?: $default;
93
        }
94
95
        if ($var = $var = $this->getCollection($isUseCache)
0 ignored issues
show
Unused Code introduced by
The assignment to $var is dead and can be removed.
Loading history...
96
            ->where('key', $key)
97
            ->where('langcode', null)
98
            ->first()) {
99
100
            return $var->value ?: $default;
101
        }
102
        
103
        return $default;
104
    }
105
106
    /**
107
     * @param string $key
108
     * @param null $value
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $value is correct as it would always require null to be passed?
Loading history...
109
     * @param string|null $langcode
110
     * @return mixed
111
     */
112
    public function save(string $key, $value = null, ?string $langcode = null)
113
    {
114
        return $this->getVariableModel()->updateOrCreate([
115
            'key' => $key,
116
            'langcode' => $langcode ?: $this->langcode,
117
        ], [
118
            'value' => $value,
119
        ]);
120
    }
121
122
    /**
123
     * @param string|null $langcode
124
     * @return $this
125
     */
126
    public function setLang(?string $langcode = null): VariableManagerContract
127
    {
128
        $this->langcode = $langcode;
129
130
        return $this;
131
    }
132
133
    /**
134
     * @param bool $val
135
     * @return $this|mixed
136
     */
137
    public function useCache(bool $val = true): VariableManagerContract
138
    {
139
        $this->isUseCache = $val;
140
        
141
        return $this;
142
    }
143
144
    /**
145
     * @return bool
146
     */
147
    public function cacheClear()
148
    {
149
        return $this->cacheRepository->forget($this->config['cache']['name']);
150
    }
151
152
    /**
153
     * @return \Illuminate\Database\Eloquent\Collection
154
     */
155
    protected function getCollection(bool $isUseCache): \Illuminate\Database\Eloquent\Collection
156
    {
157
        try {
158
            if ($isUseCache) {
159
                return $this->cacheRepository->remember(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->cacheRepos...ion(...) { /* ... */ }) could return the type array which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Collection. Consider adding an additional type-check to rule them out.
Loading history...
160
                    $this->config['cache']['name'],
161
                    $this->config['cache']['time'],
162
                    function () {
163
                        return $this->getVariableModel()->all();
164
                    });  
165
            } else {
166
                return $this->getVariableModel()->all();
167
            }
168
        } catch (\Exception $exception) {
169
            $this->app['log']->warning($exception->getMessage());
170
171
            return new \Illuminate\Database\Eloquent\Collection;
172
        }
173
    }
174
175
    /**
176
     * @return Model
177
     */
178
    protected function getVariableModel(): Model
179
    {
180
        return $this->variableModel;
181
    }
182
}
183