Completed
Push — master ( 7d660e...c11331 )
by Avtandil
02:52
created

Repository::getCacheName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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
use Longman\LaravelMultiLang\Config;
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
    /**
29
     * The instance of the cache.
30
     *
31
     * @var \Illuminate\Cache\CacheManager
32
     */
33
    protected $cache;
34
35
    /**
36
     * The instance of the database.
37
     *
38
     * @var \Illuminate\Database\DatabaseManager
39
     */
40
    protected $db;
41
42
    /**
43
     * Create a new MultiLang instance.
44
     *
45
     * @param string                               $environment
0 ignored issues
show
Bug introduced by
There is no parameter named $environment. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
46
     * @param array                                $config
47
     * @param \Illuminate\Cache\CacheManager       $cache
48
     * @param \Illuminate\Database\DatabaseManager $db
49
     */
50 27
    public function __construct(Config $config, Cache $cache, Database $db)
51
    {
52 27
        $this->config = $config;
53 27
        $this->cache  = $cache;
54 27
        $this->db     = $db;
55 27
    }
56
57 5
    public function getCacheName($lang)
58
    {
59 5
        return $this->config->get('db.texts_table', 'texts') . '_' . $lang;
60
    }
61
62 18
    public function loadFromDatabase($lang)
63
    {
64 18
        $texts = $this->getDb()->table($this->getTableName())
65 18
            ->where('lang', $lang)
66 18
            ->get(['key', 'value', 'lang', 'scope']);
67
68 18
        $array = [];
69 18
        foreach ($texts as $row) {
70 12
            $array[$row->key] = $row->value;
71
        }
72 18
        return $array;
73
    }
74
75 2
    public function loadFromCache($lang)
76
    {
77 2
        $texts = $this->getCache()->get($this->getCacheName($lang));
78
79 2
        return $texts;
80
    }
81
82 4
    public function storeInCache($lang, array $texts)
83
    {
84 4
        $this->getCache()->put($this->getCacheName($lang), $texts, $this->config->get('cache.lifetime', 1440));
85 4
        return $this;
86
    }
87
88
    /**
89
     * Check if we must load texts from cache
90
     *
91
     * @return bool
92
     */
93 4
    public function existsInCache($lang)
94
    {
95 4
        return $this->getCache()->has($this->getCacheName($lang));
96
    }
97
98
    /**
99
     * Get a database connection instance.
100
     *
101
     * @return \Illuminate\Database\Connection
102
     */
103 18
    protected function getDb()
104
    {
105 18
        $connection = $this->config->get('db.connection');
106 18
        if ($connection == 'default') {
107 18
            return $this->db->connection();
108
        }
109
        return $this->db->connection($connection);
110
    }
111
112
    /**
113
     * Get a cache driver instance.
114
     *
115
     * @return \Illuminate\Contracts\Cache\Repository
116
     */
117 4
    protected function getCache()
118
    {
119 4
        $store = $this->config->get('cache.store', 'default');
120 4
        if ($store == 'default') {
121
            return $this->cache->store();
122
        }
123 4
        return $this->cache->store($store);
124
    }
125
126 4
    public function save($texts)
127
    {
128 4
        if (empty($texts)) {
129 1
            return false;
130
        }
131
132 4
        $table   = $this->getTableName();
133 4
        $locales = $this->config->get('locales', []);
134
135 4
        foreach ($texts as $k => $v) {
136 4
            foreach ($locales as $lang => $locale_data) {
137 4
                $exists = $this->getDb()->table($table)->where([
138 4
                    'key'  => $k,
139 4
                    'lang' => $lang,
140 4
                ])->first();
141
142 4
                if ($exists) {
143 1
                    continue;
144
                }
145
146 4
                $this->getDb()->table($table)->insert([
147 4
                    'key'   => $k,
148 4
                    'lang'  => $lang,
149 4
                    'value' => $v,
150
                ]);
151
            }
152
        }
153 4
        return true;
154
    }
155
156 19
    public function getTableName()
157
    {
158 19
        $table = $this->config->get('db.texts_table', 'texts');
159 19
        return $table;
160
    }
161
}
162