Completed
Push — master ( b4e56c...1b6c56 )
by Avtandil
04:39
created

Repository::loadFromDatabase()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.2746

Importance

Changes 3
Bugs 0 Features 3
Metric Value
cc 3
eloc 14
c 3
b 0
f 3
nc 4
nop 2
dl 0
loc 22
ccs 11
cts 16
cp 0.6875
crap 3.2746
rs 9.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 Illuminate\Cache\CacheManager as Cache;
14
use Illuminate\Database\DatabaseManager as Database;
15
16
class Repository
17
{
18
19
    /**
20
     * The instance of the config.
21
     *
22
     * @var \Longman\LaravelMultiLang\Config
23
     */
24
    protected $config;
25
26
    /**
27
     * The instance of the cache.
28
     *
29
     * @var \Illuminate\Cache\CacheManager
30
     */
31
    protected $cache;
32
33
    /**
34
     * The instance of the database.
35
     *
36
     * @var \Illuminate\Database\DatabaseManager
37
     */
38
    protected $db;
39
40
    /**
41
     * Create a new MultiLang instance.
42
     *
43
     * @param \Longman\LaravelMultiLang\Config     $config
44
     * @param \Illuminate\Cache\CacheManager       $cache
45
     * @param \Illuminate\Database\DatabaseManager $db
46
     */
47 28
    public function __construct(Config $config, Cache $cache, Database $db)
48
    {
49 28
        $this->config = $config;
50 28
        $this->cache  = $cache;
51 28
        $this->db     = $db;
52 28
    }
53
54
    /**
55
     * Get cache key name based on lang and scope
56
     *
57
     * @param string $lang
58
     * @param string $scope
59
     * @return string
60
     */
61 5
    public function getCacheName($lang, $scope = null)
62
    {
63 5
        $key = $this->config->get('db.texts_table', 'texts') . '_' . $lang;
64 5
        if (!is_null($scope)) {
65
            $key .= '_' . $scope;
66
        }
67 5
        return $key;
68
    }
69
70
    /**
71
     * Load texts from database storage
72
     *
73
     * @param string $lang
74
     * @param string $scope
75
     * @return array
76
     */
77 19
    public function loadFromDatabase($lang, $scope = null)
78
    {
79 19
        $query = $this->getDb()->table($this->getTableName())
80 19
            ->where('lang', $lang);
81
82 19
        if (!is_null($scope)) {
83
            $query = $query->whereNested(function ($query) use ($scope) {
84
                $query->where('scope', 'global');
85
                $query->orWhere('scope', $scope);
86
            });
87
        } else {
88 19
            $query = $query->where('scope', 'global');
89
        }
90
91 19
        $texts = $query->get(['key', 'value', 'lang', 'scope']);
92
93 19
        $array = [];
94 19
        foreach ($texts as $row) {
95 12
            $array[$row->key] = $row->value;
96 19
        }
97 19
        return $array;
98
    }
99
100
    /**
101
     * Load texts from cache storage
102
     *
103
     * @param string $lang
104
     * @param string $scope
105
     * @return mixed
106
     */
107 2
    public function loadFromCache($lang, $scope = null)
108
    {
109 2
        $texts = $this->getCache()->get($this->getCacheName($lang, $scope));
110
111 2
        return $texts;
112
    }
113
114
    /**
115
     * Store texts in cache
116
     *
117
     * @param string $lang
118
     * @param array  $texts
119
     * @param string $scope
120
     * @return $this
121
     */
122 4
    public function storeInCache($lang, array $texts, $scope = null)
123
    {
124 4
        $this->getCache()->put($this->getCacheName($lang, $scope), $texts, $this->config->get('cache.lifetime', 1440));
125 4
        return $this;
126
    }
127
128
    /**
129
     * Check if we must load texts from cache
130
     *
131
     * @param string $lang
132
     * @param string $scope
133
     * @return bool
134
     */
135 4
    public function existsInCache($lang, $scope = null)
136
    {
137 4
        return $this->getCache()->has($this->getCacheName($lang, $scope));
138
    }
139
140
    /**
141
     * Get a database connection instance.
142
     *
143
     * @return \Illuminate\Database\Connection
144
     */
145 19
    protected function getDb()
146
    {
147 19
        $connection = $this->config->get('db.connection');
148 19
        if ($connection == 'default') {
149 19
            return $this->db->connection();
150
        }
151
        return $this->db->connection($connection);
152
    }
153
154
    /**
155
     * Get a cache driver instance.
156
     *
157
     * @return \Illuminate\Contracts\Cache\Repository
158
     */
159 4
    protected function getCache()
160
    {
161 4
        $store = $this->config->get('cache.store', 'default');
162 4
        if ($store == 'default') {
163
            return $this->cache->store();
164
        }
165 4
        return $this->cache->store($store);
166
    }
167
168
    /**
169
     * Save missing texts in database
170
     *
171
     * @param array  $texts
172
     * @param string $scope
173
     * @return bool
174
     */
175 4
    public function save(array $texts, $scope = null)
176
    {
177 4
        if (empty($texts)) {
178 1
            return false;
179
        }
180
181 4
        $table   = $this->getTableName();
182 4
        $locales = $this->config->get('locales', []);
183 4
        if (is_null($scope)) {
184 4
            $scope = 'global';
185 4
        }
186
187 4
        foreach ($texts as $k => $v) {
188 4
            foreach ($locales as $lang => $locale_data) {
189 4
                $exists = $this->getDb()
190 4
                    ->table($table)
191 4
                    ->where([
192 4
                                'key'   => $k,
193 4
                                'lang'  => $lang,
194 4
                                'scope' => $scope,
195 4
                            ])->first();
196
197 4
                if ($exists) {
198 1
                    continue;
199
                }
200
201 4
                $this->getDb()
202 4
                    ->table($table)
203 4
                    ->insert([
204 4
                                 'key'   => $k,
205 4
                                 'lang'  => $lang,
206 4
                                 'scope' => $scope,
207 4
                                 'value' => $v,
208 4
                             ]);
209 4
            }
210 4
        }
211 4
        return true;
212
    }
213
214
    /**
215
     * Get texts table name
216
     *
217
     * @return string
218
     */
219 20
    public function getTableName()
220
    {
221 20
        $table = $this->config->get('db.texts_table', 'texts');
222 20
        return $table;
223
    }
224
}
225