Completed
Push — master ( 427d1b...7d660e )
by Avtandil
03:12
created

Repository::save()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 29
Code Lines 18

Duplication

Lines 18
Ratio 62.07 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 18
loc 29
rs 8.439
cc 5
eloc 18
nc 5
nop 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 Illuminate\Http\Request;
16
use Illuminate\Support\Collection;
17
use InvalidArgumentException;
18
use Longman\LaravelMultiLang\Config;
19
20
class Repository
21
{
22
    /**
23
     * Language/Locale.
24
     *
25
     * @var string
26
     */
27
    protected $lang;
28
29
    /**
30
     * The instance of the cache.
31
     *
32
     * @var \Illuminate\Cache\CacheManager
33
     */
34
    protected $cache;
35
36
    /**
37
     * Config.
38
     *
39
     * @var array
40
     */
41
    protected $config;
42
43
    /**
44
     * The instance of the database.
45
     *
46
     * @var \Illuminate\Database\DatabaseManager
47
     */
48
    protected $db;
49
50
    /**
51
     * Name of the cache.
52
     *
53
     * @var string
54
     */
55
    protected $cache_name;
56
57
58
59
    /**
60
     * Create a new MultiLang instance.
61
     *
62
     * @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...
63
     * @param array                                $config
64
     * @param \Illuminate\Cache\CacheManager       $cache
65
     * @param \Illuminate\Database\DatabaseManager $db
66
     */
67
    public function __construct(Config $config, Cache $cache, Database $db)
68
    {
69
        $this->config       = $config;
0 ignored issues
show
Documentation Bug introduced by
It seems like $config of type object<Longman\LaravelMultiLang\Config> is incompatible with the declared type array of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
70
        $this->cache       = $cache;
71
        $this->db          = $db;
72
73
    }
74
75
76 View Code Duplication
    public function loadFromDatabase($lang)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78
        $texts = $lang ? $this->getDb()->table($this->getTableName())
79
            ->where('lang', $lang)
80
            ->get(['key', 'value', 'lang', 'scope']) : $this->getDb()->table($this->getTableName())->get(['key', 'value', 'lang', 'scope']);
81
82
        $array = [];
83
        foreach ($texts as $row) {
84
            $array[$row->key] = $row->value;
85
        }
86
        return $array;
87
    }
88
89
    /**
90
     * Get a database connection instance.
91
     *
92
     * @return \Illuminate\Database\Connection
93
     */
94 View Code Duplication
    public function getDb()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
    {
96
        $connection = $this->config->get('db.connection');
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->config (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
97
        if ($connection == 'default') {
98
            return $this->db->connection();
99
        }
100
        return $this->db->connection($connection);
101
    }
102
103
    /**
104
     * Get a cache driver instance.
105
     *
106
     * @return \Illuminate\Contracts\Cache\Repository
107
     */
108 View Code Duplication
    public function getCache()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
    {
110
        if ($this->config->get('cache.enabled', true) === false) {
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->config (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
111
            return null;
112
        }
113
        $store = $this->config->get('cache.store', 'default');
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->config (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
114
        if ($store == 'default') {
115
            return $this->cache->store();
116
        }
117
        return $this->cache->store($store);
118
    }
119
120
121
    public function save($texts)
122
    {
123
        if (empty($texts)) {
124
            return false;
125
        }
126
127
        $table   = $this->getTableName();
128
        $locales = $this->getLocales();
0 ignored issues
show
Bug introduced by
The method getLocales() does not seem to exist on object<Longman\LaravelMultiLang\Repository>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
129
130 View Code Duplication
        foreach ($texts as $k => $v) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
            foreach ($locales as $lang => $locale_data) {
132
                $exists = $this->getDb()->table($table)->where([
133
                    'key'  => $k,
134
                    'lang' => $lang,
135
                ])->first();
136
137
                if ($exists) {
138
                    continue;
139
                }
140
141
                $this->getDb()->table($table)->insert([
142
                    'key'   => $k,
143
                    'lang'  => $lang,
144
                    'value' => $v,
145
                ]);
146
            }
147
        }
148
        return true;
149
    }
150
151
152
    protected function getTableName()
153
    {
154
        $table = $this->config->get('db.texts_table', 'texts');
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->config (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
155
        return $table;
156
    }
157
158
159
}
160