Passed
Push — master ( 1916ef...0eb677 )
by Vasyl
02:53
created

VariableManager::all()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 9
c 2
b 0
f 0
dl 0
loc 18
rs 9.9666
cc 4
nc 4
nop 0
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 $app;
13
14
    protected $variableModel;
15
16
    protected $cacheRepository;
17
18
    protected $locale = null;
19
20
    protected $variables = null;
21
22
    /**
23
     * VariableManager constructor.
24
     * @param $cacheRepository
25
     * @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...
26
     */
27
    public function __construct($variableModel, $cacheRepository, $app = null)
28
    {
29
        if (! $app) {
0 ignored issues
show
introduced by
$app is of type null, thus it always evaluated to false.
Loading history...
30
            $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

30
            $app = /** @scrutinizer ignore-call */ app();   //Fallback when $app is not given
Loading history...
31
        }
32
        $this->app = $app;
33
34
        $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...
35
36
        $this->variableModel = $variableModel;
37
38
        $this->cacheRepository = $cacheRepository;
39
    }
40
41
    /**
42
     * @param string $key
43
     * @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...
44
     * @return mixed|null
45
     */
46
    public function get(string $key, $default = null, bool $useCache = true)
47
    {
48
        if ($useCache === false) {
49
            $var = $this->variableModel->where('key', $key)
50
                ->when($this->locale, function ($q) {
51
                    $q->where('locale', $this->locale);
52
                })
53
                ->first();
54
            return $var ? $var->value : $default;
55
        }
56
57
       if (isset($this->all()[$key])) {
58
           return $this->all()[$key];
59
       }
60
       
61
       return $default;
62
    }
63
64
    public function set(string $key, $value = null)
65
    {
66
        return $this->variableModel->updateOrCreate([
67
            'key' => $key,
68
        ], [
69
            'value' => $value,
70
        ]);
71
    }
72
73
    /**
74
     * @return array
75
     */
76
    public function all(): array
77
    {
78
        if ($this->variables === null) {
79
80
            $all = $this->cacheRepository->remember($this->config['cache']['name'], $this->config['cache']['time'], function () {
81
                return $this->getCollection();
82
            });
83
84
            if ($all) {
85
                $allByLocale = $this->locale ? $all->where('locale', $this->locale) : $all;
86
                $this->variables = $allByLocale->pluck('value', 'key')->toArray();
87
            } else {
88
                $this->variables = [];
89
            }
90
91
        }
92
93
        return $this->variables;
94
    }
95
96
    /**
97
     * @param string $locale
98
     * @return $this
99
     */
100
    public function locale(string $locale = null)
101
    {
102
        $this->locale = $locale;
103
104
        return $this;
105
    }
106
107
    public function cacheOff()
108
    {
109
110
    }
111
112
    public function cacheClear()
113
    {
114
        return $this->cacheRepository->forget($this->config['cache']['name']);
115
    }
116
117
    /**
118
     * @return |null
0 ignored issues
show
Documentation Bug introduced by
The doc comment |null at position 0 could not be parsed: Unknown type name '|' at position 0 in |null.
Loading history...
119
     */
120
    protected function getCollection()
121
    {
122
        try {
123
            return $this->variableModel->select('key', 'value', 'locale')->get();
124
        } catch (\Exception $exception) {
125
            $this->app['log']->info(__CLASS__ . ': ' . $exception->getMessage());
126
127
            return false;
128
        }
129
    }
130
}
131