EloquentStore   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 189
rs 10
c 0
b 0
f 0
ccs 53
cts 53
cp 1
wmc 19
lcom 1
cbo 2

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getCacheKey() 0 4 1
A isCached() 0 4 1
A config() 0 4 1
A setSaved() 0 6 1
A all() 0 8 2
A save() 0 11 2
A saveInserted() 0 8 3
A saveUpdated() 0 10 3
A saveDeleted() 0 8 3
A getSavedOne() 0 4 1
1
<?php namespace Arcanesoft\Settings\Stores;
2
3
use Arcanesoft\Settings\Models\Setting;
4
use Illuminate\Contracts\Cache\Repository as Cache;
5
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
6
7
/**
8
 * Class     EloquentStore
9
 *
10
 * @package  Arcanesoft\Settings\Stores
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class EloquentStore
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Properties
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /**
20
     * The Setting Eloquent Model.
21
     *
22
     * @var  \Arcanesoft\Settings\Models\Setting
23
     */
24
    protected $model;
25
26
    /**
27
     * The cache repository.
28
     *
29
     * @var \Illuminate\Contracts\Cache\Repository
30
     */
31
    protected $cache;
32
33
    /**
34
     * @var \Illuminate\Database\Eloquent\Collection
35
     */
36
    protected $saved;
37
38
    /* ------------------------------------------------------------------------------------------------
39
     |  Constructor
40
     | ------------------------------------------------------------------------------------------------
41
     */
42
    /**
43
     * SettingsManager constructor.
44
     *
45
     * @param  \Arcanesoft\Settings\Models\Setting     $model
46
     * @param  \Illuminate\Contracts\Cache\Repository  $cache
47
     */
48 30
    public function __construct(Setting $model, Cache $cache)
49
    {
50 30
        $this->model = $model;
51 30
        $this->cache = $cache;
52 30
    }
53
54
    /* ------------------------------------------------------------------------------------------------
55
     |  Getters & Setters
56
     | ------------------------------------------------------------------------------------------------
57
     */
58
    /**
59
     * Get the cache key.
60
     *
61
     * @return string
62
     */
63 30
    protected function getCacheKey()
64
    {
65 30
        return $this->config('cache.key', 'cached_settings');
66
    }
67
68
    /**
69
     * Check if cache is enabled.
70
     *
71
     * @return bool
72
     */
73 30
    protected function isCached()
74
    {
75 30
        return $this->config('cache.enabled', false);
76
    }
77
78
    /**
79
     * Get the config value by key.
80
     *
81
     * @param  string  $key
82
     * @param  mixed   $default
83
     *
84
     * @return mixed
85
     */
86 30
    private function config($key, $default = null)
87
    {
88 30
        return config("arcanesoft.settings.$key", $default);
89
    }
90
91
    /**
92
     * Set the saved entries.
93
     *
94
     * @param  \Illuminate\Database\Eloquent\Collection  $saved
95
     *
96
     * @return self
97
     */
98 21
    private function setSaved(EloquentCollection $saved)
99
    {
100 21
        $this->saved = $saved;
101
102 21
        return $this;
103
    }
104
105
    /* ------------------------------------------------------------------------------------------------
106
     |  Main Functions
107
     | ------------------------------------------------------------------------------------------------
108
     */
109
    /**
110
     * Get all the setting entries.
111
     *
112
     * @return \Illuminate\Database\Eloquent\Collection
113
     */
114 30
    public function all()
115
    {
116 30
        return ! $this->isCached()
117 20
            ? $this->model->all()
118 30
            : $this->cache->rememberForever($this->getCacheKey(), function() {
119 30
                return $this->model->all();
120 30
            });
121
    }
122
123
    /**
124
     * Save the changes.
125
     *
126
     * @param  \Illuminate\Database\Eloquent\Collection  $saved
127
     * @param  array                                     $changes
128
     */
129 21
    public function save($saved, array $changes)
130
    {
131 21
        $this->setSaved($saved);
132 21
        $this->saveInserted($changes['inserted']);
133 21
        $this->saveUpdated($changes['updated']);
134 21
        $this->saveDeleted($changes['deleted']);
135
136 21
        if ($this->isCached()) {
137 21
            $this->cache->forget($this->getCacheKey());
138 14
        }
139 21
    }
140
141
    /* ------------------------------------------------------------------------------------------------
142
     |  Other Functions
143
     | ------------------------------------------------------------------------------------------------
144
     */
145
    /**
146
     * Save the inserted entries.
147
     *
148
     * @param  array  $inserted
149
     */
150 21
    private function saveInserted(array $inserted)
151
    {
152 21
        foreach ($inserted as $domain => $values) {
153 18
            foreach ($values as $key => $value) {
154 18
                $this->model->createOne($domain, $key, $value);
155 12
            }
156 14
        }
157 21
    }
158
159
    /**
160
     * Save the updated entries.
161
     *
162
     * @param  array  $updated
163
     */
164 21
    private function saveUpdated(array $updated)
165
    {
166 21
        foreach ($updated as $domain => $values) {
167 3
            foreach ($values as $key => $value) {
168 3
                $model = $this->getSavedOne($domain, $key);
169 3
                $model->updateValue($value);
170 3
                $model->save();
171 2
            }
172 14
        }
173 21
    }
174
175
    /**
176
     * Save the deleted entries.
177
     *
178
     * @param  array  $deleted
179
     */
180 21
    private function saveDeleted(array $deleted)
181
    {
182 21
        foreach ($deleted as $domain => $values) {
183 9
            foreach ($values as $key) {
184 9
                $this->getSavedOne($domain, $key)->delete();
185 6
            }
186 14
        }
187 21
    }
188
189
    /**
190
     * Get the first saved entry.
191
     *
192
     * @param  string  $domain
193
     * @param  string  $key
194
     *
195
     * @return \Arcanesoft\Settings\Models\Setting
196
     */
197 12
    private function getSavedOne($domain, $key)
198
    {
199 12
        return $this->saved->groupBy('domain')->get($domain)->where('key', $key)->first();
200
    }
201
}
202