Passed
Push — master ( b69416...641e69 )
by Vasyl
02:28
created

VariableManager::firstByName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 1
nc 1
nop 1
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 = 'fomvasss.variables.cache';
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->variableModel = $this->model();
33
        $this->cacheRepo = $cacheRepo;
34
    }
35
36
    /**
37
     * @return mixed
38
     */
39
    protected function model()
40
    {
41
        return app()->make(config('variables.model', \Fomvasss\Variable\Variable::class));
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

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

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