Total Complexity | 7 |
Total Lines | 67 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
10 | class VariableManager implements VariableManagerContract |
||
11 | { |
||
12 | protected $variableModel; |
||
13 | |||
14 | protected $cacheRepository; |
||
15 | |||
16 | protected $locale; |
||
17 | |||
18 | protected $variables; |
||
19 | |||
20 | /** |
||
21 | * VariableManager constructor. |
||
22 | * @param $cacheRepository |
||
23 | * @param null $app |
||
|
|||
24 | */ |
||
25 | public function __construct($variableModel, $cacheRepository, $app = null) |
||
26 | { |
||
27 | if (! $app) { |
||
28 | $app = app(); //Fallback when $app is not given |
||
29 | } |
||
30 | $this->app = $app; |
||
31 | |||
32 | $this->config = $this->app['config']->get('variables'); |
||
33 | |||
34 | $this->variableModel = $variableModel; |
||
35 | |||
36 | $this->cacheRepository = $cacheRepository; |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @param string $key |
||
41 | * @param string|null $default |
||
42 | * @return mixed|string |
||
43 | */ |
||
44 | public function get(string $key, $default = null) |
||
45 | { |
||
46 | return $this->all()[$key] ?? $default; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @return array |
||
51 | */ |
||
52 | public function all(): array |
||
53 | { |
||
54 | if (! isset($this->variables)) { |
||
55 | |||
56 | $all = $this->cacheRepository->remember($this->config['cache']['name'], $this->config['cache']['time'], function () { |
||
57 | return $this->variableModel->select('key', 'value', 'locale')->get(); |
||
58 | }); |
||
59 | |||
60 | $allByLocale = $this->locale ? $all->where('locale', $this->locale) : $all; |
||
61 | |||
62 | $this->variables = $allByLocale->pluck('value', 'key')->toArray(); |
||
63 | } |
||
64 | |||
65 | return $this->variables; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param string $locale |
||
70 | * @return $this |
||
71 | */ |
||
72 | public function locale(string $locale = null) |
||
77 | } |
||
78 | } |
||
79 |