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 $useCache; |
||||
31 | |||||
32 | /** |
||||
33 | * VariableManager constructor. |
||||
34 | * @param $variableModel |
||||
35 | * @param $cacheRepository |
||||
36 | * @param null $app |
||||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||||
37 | */ |
||||
38 | public function __construct($variableModel, $cacheRepository, $app = null) |
||||
39 | { |
||||
40 | if (! $app) { |
||||
0 ignored issues
–
show
|
|||||
41 | $app = app(); //Fallback when $app is not given |
||||
0 ignored issues
–
show
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
![]() |
|||||
42 | } |
||||
43 | $this->app = $app; |
||||
44 | |||||
45 | $this->config = $this->app['config']->get('variables'); |
||||
0 ignored issues
–
show
|
|||||
46 | |||||
47 | $this->useCache = $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 $useCache |
||||
57 | * @return \Illuminate\Database\Eloquent\Collection|mixed |
||||
58 | */ |
||||
59 | public function all(?string $langcode = null, ?bool $useCache = null) |
||||
60 | { |
||||
61 | $langcode = $langcode ?: $this->langcode; |
||||
62 | $useCache = $useCache === null |
||||
63 | ? $this->useCache |
||||
64 | : $useCache; |
||||
65 | |||||
66 | if ($langcode) { |
||||
67 | return $this->getCollection($useCache)->where('langcode', $langcode); |
||||
68 | } |
||||
69 | |||||
70 | return $this->getCollection($useCache); |
||||
71 | } |
||||
72 | |||||
73 | /** |
||||
74 | * @param string $key |
||||
75 | * @param null $default |
||||
0 ignored issues
–
show
|
|||||
76 | * @param string|null $langcode |
||||
77 | * @param bool|null $useCache |
||||
78 | * @return mixed|null |
||||
79 | */ |
||||
80 | public function get(string $key, $default = null, ?string $langcode = null, ?bool $useCache = null) |
||||
81 | { |
||||
82 | $langcode = $langcode ?: $this->langcode; |
||||
83 | $useCache = $useCache === null |
||||
84 | ? $this->useCache |
||||
85 | : $useCache; |
||||
86 | |||||
87 | if ($var = $this->getCollection($useCache) |
||||
88 | ->where('key', $key) |
||||
89 | ->where('langcode', $langcode) |
||||
90 | ->first()) { |
||||
91 | |||||
92 | return $var->value ?: $default; |
||||
93 | } |
||||
94 | |||||
95 | if ($var = $var = $this->getCollection($useCache) |
||||
0 ignored issues
–
show
|
|||||
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
|
|||||
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 $key |
||||
124 | * @param array $default |
||||
125 | * @param string|null $langcode |
||||
126 | * @param bool|null $useCache |
||||
127 | * @return array|mixed |
||||
128 | */ |
||||
129 | public function getArray(string $key, $default = [], ?string $langcode = null, ?bool $useCache = null) |
||||
130 | { |
||||
131 | $res = json_decode($this->get($key, '[]', $langcode, $useCache), true); |
||||
132 | |||||
133 | return empty($res) ? $default : $res; |
||||
134 | } |
||||
135 | |||||
136 | /** |
||||
137 | * @param string $key |
||||
138 | * @param array $value |
||||
139 | * @param string|null $langcode |
||||
140 | * @return mixed |
||||
141 | */ |
||||
142 | public function saveArray(string $key, $value = [], ?string $langcode = null) |
||||
143 | { |
||||
144 | $value = json_encode($value); |
||||
145 | |||||
146 | return $this->save($key, $value, $langcode); |
||||
147 | } |
||||
148 | |||||
149 | /** |
||||
150 | * @param string|null $langcode |
||||
151 | * @return $this |
||||
152 | */ |
||||
153 | public function setLang(?string $langcode = null): VariableManagerContract |
||||
154 | { |
||||
155 | $this->langcode = $langcode; |
||||
156 | |||||
157 | return $this; |
||||
158 | } |
||||
159 | |||||
160 | /** |
||||
161 | * @param bool $val |
||||
162 | * @return $this|mixed |
||||
163 | */ |
||||
164 | public function useCache(bool $val = true): VariableManagerContract |
||||
165 | { |
||||
166 | $this->useCache = $val; |
||||
167 | |||||
168 | return $this; |
||||
169 | } |
||||
170 | |||||
171 | /** |
||||
172 | * @return bool |
||||
173 | */ |
||||
174 | public function cacheClear() |
||||
175 | { |
||||
176 | return $this->cacheRepository->forget($this->config['cache']['name']); |
||||
177 | } |
||||
178 | |||||
179 | /** |
||||
180 | * @return \Illuminate\Database\Eloquent\Collection |
||||
181 | */ |
||||
182 | protected function getCollection(bool $useCache): \Illuminate\Database\Eloquent\Collection |
||||
183 | { |
||||
184 | try { |
||||
185 | if ($useCache) { |
||||
186 | return $this->cacheRepository->remember( |
||||
0 ignored issues
–
show
|
|||||
187 | $this->config['cache']['name'], |
||||
188 | $this->config['cache']['time'], |
||||
189 | function () { |
||||
190 | return $this->getVariableModel()->all(); |
||||
191 | }); |
||||
192 | } else { |
||||
193 | return $this->getVariableModel()->all(); |
||||
194 | } |
||||
195 | } catch (\Exception $exception) { |
||||
196 | $this->app['log']->warning($exception->getMessage()); |
||||
197 | |||||
198 | return new \Illuminate\Database\Eloquent\Collection; |
||||
199 | } |
||||
200 | } |
||||
201 | |||||
202 | /** |
||||
203 | * @return Model |
||||
204 | */ |
||||
205 | protected function getVariableModel(): Model |
||||
206 | { |
||||
207 | return $this->variableModel; |
||||
208 | } |
||||
209 | } |
||||
210 |