Completed
Push — master ( 76efee...bc6801 )
by Avtandil
13s
created

Repository::getCacheName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 2
1
<?php
2
/*
3
 * This file is part of the Laravel MultiLang package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\LaravelMultiLang;
12
13
use Carbon\Carbon;
14
use Illuminate\Cache\CacheManager as Cache;
15
use Illuminate\Database\DatabaseManager as Database;
16
17
class Repository
18
{
19
20
    /**
21
     * The instance of the config.
22
     *
23
     * @var \Longman\LaravelMultiLang\Config
24
     */
25
    protected $config;
26
27
    /**
28
     * The instance of the cache.
29
     *
30
     * @var \Illuminate\Cache\CacheManager
31
     */
32
    protected $cache;
33
34
    /**
35
     * The instance of the database.
36
     *
37
     * @var \Illuminate\Database\DatabaseManager
38
     */
39
    protected $db;
40
41
    /**
42
     * Create a new MultiLang instance.
43
     *
44
     * @param \Longman\LaravelMultiLang\Config $config
45
     * @param \Illuminate\Cache\CacheManager $cache
46
     * @param \Illuminate\Database\DatabaseManager $db
47
     */
48 38
    public function __construct(Config $config, Cache $cache, Database $db)
49
    {
50 38
        $this->config = $config;
51 38
        $this->cache = $cache;
52 38
        $this->db = $db;
53 38
    }
54
55
    /**
56
     * Get cache key name based on lang and scope
57
     *
58
     * @param string $lang
59
     * @param string $scope
60
     * @return string
61
     */
62 7
    public function getCacheName($lang, $scope = null)
63
    {
64 7
        $key = $this->config->get('db.texts_table', 'texts') . '_' . $lang;
65 7
        if (! is_null($scope)) {
66 5
            $key .= '_' . $scope;
67
        }
68
69 7
        return $key;
70
    }
71
72
    /**
73
     * Load texts from database storage
74
     *
75
     * @param string $lang
76
     * @param string $scope
77
     * @return array
78
     */
79 24
    public function loadFromDatabase($lang, $scope = null)
80
    {
81 24
        $query = $this->getDb()->table($this->getTableName())
82 24
            ->where('lang', $lang);
83
84 24
        if (! is_null($scope)) {
85 24
            $query = $query->whereNested(function ($query) use ($scope) {
86 24
                $query->where('scope', 'global');
87 24
                $query->orWhere('scope', $scope);
88 24
            });
89
        } else {
90 1
            $query = $query->where('scope', 'global');
91
        }
92
93 24
        $texts = $query->get(['key', 'value', 'lang', 'scope']);
94
95 24
        $array = [];
96 24
        foreach ($texts as $row) {
97 16
            $array[$row->key] = $row->value;
98
        }
99
100 24
        return $array;
101
    }
102
103
    /**
104
     * Load all texts from database storage
105
     *
106
     * @param string $lang
107
     * @param string $scope
108
     * @return array
109
     */
110 1
    public function loadAllFromDatabase($lang = null, $scope = null)
111
    {
112 1
        $query = $this->getDb()->table($this->getTableName());
113
114 1
        if (! is_null($lang)) {
115 1
            $query = $query->where('lang', $lang);
116
        }
117
118 1
        if (! is_null($scope)) {
119 1
            $query = $query->whereNested(function ($query) use ($scope) {
120 1
                $query->where('scope', 'global');
121 1
                $query->orWhere('scope', $scope);
122 1
            });
123
        }
124
125 1
        $texts = $query->get();
126
127 1
        $array = [];
128 1
        foreach ($texts as $row) {
129 1
            $array[$row->lang][$row->key] = $row;
130
        }
131
132 1
        return $array;
133
    }
134
135
    /**
136
     * Load texts from cache storage
137
     *
138
     * @param string $lang
139
     * @param string $scope
140
     * @return mixed
141
     */
142 3
    public function loadFromCache($lang, $scope = null)
143
    {
144 3
        $texts = $this->getCache()->get($this->getCacheName($lang, $scope));
145
146 3
        return $texts;
147
    }
148
149
    /**
150
     * Store texts in cache
151
     *
152
     * @param string $lang
153
     * @param array $texts
154
     * @param string $scope
155
     * @return $this
156
     */
157 5
    public function storeInCache($lang, array $texts, $scope = null)
158
    {
159 5
        $this->getCache()->put($this->getCacheName($lang, $scope), $texts, $this->config->get('cache.lifetime', 1440));
160
161 5
        return $this;
162
    }
163
164
    /**
165
     * Check if we must load texts from cache
166
     *
167
     * @param string $lang
168
     * @param string $scope
169
     * @return bool
170
     */
171 5
    public function existsInCache($lang, $scope = null)
172
    {
173 5
        return $this->getCache()->has($this->getCacheName($lang, $scope));
174
    }
175
176
    /**
177
     * Get a database connection instance.
178
     *
179
     * @return \Illuminate\Database\Connection
180
     */
181 25
    protected function getDb()
182
    {
183 25
        $connection = $this->config->get('db.connection');
184 25
        if ($connection == 'default') {
185 25
            return $this->db->connection();
186
        }
187
188
        return $this->db->connection($connection);
189
    }
190
191
    /**
192
     * Get a cache driver instance.
193
     *
194
     * @return \Illuminate\Contracts\Cache\Repository
195
     */
196 5
    protected function getCache()
197
    {
198 5
        $store = $this->config->get('cache.store', 'default');
199 5
        if ($store == 'default') {
200
            return $this->cache->store();
201
        }
202
203 5
        return $this->cache->store($store);
204
    }
205
206
    /**
207
     * Save missing texts in database
208
     *
209
     * @param array $texts
210
     * @param string $scope
211
     * @return bool
212
     */
213 4
    public function save(array $texts, $scope = null)
214
    {
215 4
        if (empty($texts)) {
216 1
            return false;
217
        }
218
219 4
        $table = $this->getTableName();
220 4
        $locales = $this->config->get('locales', []);
221 4
        if (is_null($scope)) {
222 1
            $scope = 'global';
223
        }
224
225 4
        $now = Carbon::now()->toDateTimeString();
226 4
        foreach ($texts as $k => $v) {
227 4
            foreach ($locales as $lang => $locale_data) {
228 4
                $exists = $this->getDb()
229 4
                    ->table($table)
230 4
                    ->where([
231 4
                        'key'   => $k,
232 4
                        'lang'  => $lang,
233 4
                        'scope' => $scope,
234 4
                    ])->first();
235
236 4
                if ($exists) {
237 1
                    continue;
238
                }
239
240 4
                $this->getDb()
241 4
                    ->table($table)
242 4
                    ->insert([
243 4
                        'key'        => $k,
244 4
                        'lang'       => $lang,
245 4
                        'scope'      => $scope,
246 4
                        'value'      => $v,
247 4
                        'created_at' => $now,
248 4
                        'updated_at' => $now,
249
                    ]);
250
            }
251
        }
252
253 4
        return true;
254
    }
255
256
    /**
257
     * Get texts table name
258
     *
259
     * @return string
260
     */
261 26
    public function getTableName()
262
    {
263 26
        $table = $this->config->get('db.texts_table', 'texts');
264
265 26
        return $table;
266
    }
267
}
268