Passed
Push — master ( 641e69...c260d1 )
by Vasyl
02:57
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
use Illuminate\Cache\Repository as CacheRepository;
6
7
/**
8
 * Class VariableManager
9
 *
10
 * @package \Fomvasss\Variable
11
 */
12
class VariableManager implements VariableManagerContract
13
{
14
    protected $variableModel;
15
16
    protected $cacheRepo;
17
18
    protected $locale = null;
19
20
    private $cacheName;
21
22
    protected $cacheTime;
23
24
    /**
25
     * VariableManager constructor.
26
     *
27
     * @param \Illuminate\Cache\Repository $cacheRepo
28
     */
29
    public function __construct(CacheRepository $cacheRepo)
30
    {
31
        $this->cacheTime = config('variables.cache.time', 360);
0 ignored issues
show
Bug introduced by
The function config 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

31
        $this->cacheTime = /** @scrutinizer ignore-call */ config('variables.cache.time', 360);
Loading history...
32
        $this->cacheName = config('variables.cache.name', 'laravel.variables.cache');
33
        $this->variableModel = $this->model();
34
        $this->cacheRepo = $cacheRepo;
35
    }
36
37
    /**
38
     * @return mixed
39
     */
40
    protected function model()
41
    {
42
        return app()->make(config('variables.model', \Fomvasss\Variable\Variable::class));
0 ignored issues
show
Bug introduced by
The function config 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

42
        return app()->make(/** @scrutinizer ignore-call */ config('variables.model', \Fomvasss\Variable\Variable::class));
Loading history...
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

42
        return /** @scrutinizer ignore-call */ app()->make(config('variables.model', \Fomvasss\Variable\Variable::class));
Loading history...
43
    }
44
45
    /**
46
     * @return array
47
     */
48
    public function all(): array
49
    {
50
        $settings = $this->getAll();
51
        return $settings->pluck('value', 'name')->toArray();
52
    }
53
54
    /**
55
     * @param $name
56
     * @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...
57
     * @return null
58
     */
59
    public function first($name, $default = null)
60
    {
61
        if (!empty($variable = $this->firstByName($name))) {
62
            return $variable->value;
63
        }
64
65
        return $default;
66
    }
67
68
    /**
69
     * @param $name
70
     * @param bool $asoc
71
     * @return array|mixed
72
     */
73
    public function firstJsonDecode($name, $asoc = true)
74
    {
75
        if (!empty($variable = $this->firstByName($name))) {
76
            return json_decode($variable->value, $asoc);
77
        }
78
79
        return [];
80
    }
81
82
    /**
83
     * @param $name
84
     * @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...
Documentation Bug introduced by
Are you sure the doc-type for parameter $description is correct as it would always require null to be passed?
Loading history...
85
     * @param null $description
86
     * @return int
87
     */
88
    public function set($name, $value = null, $description = null): int
89
    {
90
        $this->cacheRepo->forget($this->cacheName);
91
92
        return $this->variableModel->updateOrCreate([
93
            'name' => $name, 'locale' => $this->locale
94
        ], [
95
            'value' => $value, 'description' => $description,
96
        ]) ? 1 : 0;
97
    }
98
99
    /**
100
     * @param $name
101
     * @return int
102
     */
103
    public function delete($name): int
104
    {
105
        $res = $this->variableModel->where('name', $name)->where('locale', $this->locale)->delete();
106
        $this->cacheRepo->forget($this->cacheName);
107
108
        return $res ? 0 : 1;
109
    }
110
111
    /**
112
     * @param string|null $locale
113
     * @return $this
114
     */
115
    public function locale(string $locale = null)
116
    {
117
        $this->locale = $locale;
118
119
        return $this;
120
    }
121
122
    /**
123
     * @param array $attributes
124
     * @return int
125
     */
126
    public function setArray(array $attributes): int
127
    {
128
        $r = 0;
129
        foreach ($attributes as $name => $value) {
130
            $this->variableModel->updateOrCreate([
131
                'name' => $name, 'locale' => $this->locale
132
            ], [
133
                'value' => $value
134
            ]);
135
            $r++;
136
        }
137
        $this->cacheRepo->forget($this->cacheName);
138
        return $r;
139
    }
140
141
    /**
142
     * @param $name
143
     * @return mixed
144
     */
145
    protected function firstByName($name)
146
    {
147
        return $this->getAll()->where('name', $name)->where('locale', $this->locale)->first();
148
    }
149
150
    /**
151
     * @return mixed
152
     */
153
    protected function getAll()
154
    {
155
        $settings = $this->cacheRepo->remember($this->cacheName, $this->cacheTime, function () {
156
            return $this->variableModel->all();
157
        });
158
159
        return $this->locale ? $settings->where('locale', $this->locale) : $settings;
160
    }
161
}
162