Passed
Push — master ( bf1cb1...07ceb6 )
by Vasyl
02:42
created

VariableManager::firstJsonDecode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
1
<?php
2
3
namespace Fomvasss\Variable;
4
5
/**
6
 * Class VariableManager
7
 *
8
 * @package \Fomvasss\Variable
9
 */
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
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...
24
     */
25
    public function __construct($variableModel, $cacheRepository, $app = null)
26
    {
27
        if (! $app) {
0 ignored issues
show
introduced by
$app is of type null, thus it always evaluated to false.
Loading history...
28
            $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

28
            $app = /** @scrutinizer ignore-call */ app();   //Fallback when $app is not given
Loading history...
29
        }
30
        $this->app = $app;
0 ignored issues
show
Bug Best Practice introduced by
The property app does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
31
32
        $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...
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)
73
    {
74
        $this->locale = $locale;
75
76
        return $this;
77
    }
78
}
79