Completed
Push — master ( 54b8d1...ceafd9 )
by Avtandil
02:36
created

ExportCommand::export()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 8
nop 3
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
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\Console;
12
13
use App;
14
use Illuminate\Console\Command;
15
use InvalidArgumentException;
16
use Illuminate\Database\DatabaseManager as Database;
17
use Symfony\Component\Yaml\Yaml;
18
19
class ExportCommand extends Command
20
{
21
    /**
22
     * The name and signature of the console command.
23
     *
24
     * @var string
25
     */
26
    protected $signature = 'multilang:export        
27
        {--path=storage/multilang : The path to multilang folder}
28
        {--lang= : Comma separated langs to export, default all}
29
        {--scope= : Comma separated scopes, default all}
30
        {--force : Force update existing texts in files}
31
        {--clear : Clear texts from files before export}
32
        ';
33
34
    /**
35
     * The console command description.
36
     *
37
     * @var string
38
     */
39
    protected $description = 'Export texts from database to yml files.';
40
41
    /**
42
     * The name of texts table.
43
     *
44
     * @var string
45
     */
46
    protected $table;
47
48
    /**
49
     * The database connection instance.
50
     *
51
     * @var \Illuminate\Database\Connection
52
     */
53
    protected $db;
54
55
    /**
56
     * The path to texts files.
57
     *
58
     * @var string
59
     */
60
    protected $path;
61
62
    /**
63
     * The langs.
64
     *
65
     * @var array
66
     */
67
    protected $langs;
68
69
    /**
70
     * The available scopes.
71
     *
72
     * @var array
73
     */
74
    protected $scopes = ['global', 'site', 'admin'];
75
76
    /**
77
     * Execute the console command.
78
     *
79
     * @return mixed
80
     */
81
    public function handle()
82
    {
83
        $this->table = config('multilang.db.texts_table', 'texts');
84
        $this->db    = $this->getDatabase();
85
86
        $lang = $this->option('lang');
87
        if (! empty($lang)) {
88
            $this->langs = explode(',', $lang);
89
        }
90
91
        $scopes = $this->scopes;
92
        $scope  = $this->option('scope');
93 View Code Duplication
        if (! empty($scope)) {
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...
94
            $scopes = explode(',', $scope);
95
            foreach ($scopes as $scope) {
96
                if (! in_array($scope, $this->scopes)) {
97
                    throw new InvalidArgumentException('Scope "' . $scope . '" is not found! Available scopes is ' . implode(', ', $this->scopes));
98
                }
99
            }
100
        }
101
102
        $path       = $this->option('path', 'storage/multilang');
0 ignored issues
show
Unused Code introduced by
The call to ExportCommand::option() has too many arguments starting with 'storage/multilang'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
103
        $this->path = base_path($path);
0 ignored issues
show
Bug introduced by
It seems like $path defined by $this->option('path', 'storage/multilang') on line 102 can also be of type array; however, base_path() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
104
        if (! is_dir($this->path)) {
105
            throw new InvalidArgumentException('Folder "' . $this->path . '" is not accessible!');
106
        }
107
        if (! is_writable($this->path)) {
108
            throw new InvalidArgumentException('Folder "' . $this->path . '" is not writable!');
109
        }
110
111
        $force = $this->option('force');
112
        $clear = $this->option('clear');
113
        foreach ($scopes as $scope) {
114
            $this->export($scope, $force, $clear);
0 ignored issues
show
Documentation introduced by
$force is of type string|array, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$clear is of type string|array, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
115
        }
116
117
        return;
118
119
        /*$texts = Text::where('scope', 'site')->where('lang', 'en')->get()->toArray();
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
120
121
122
        $newTexts = [];
123
        foreach($texts as $text) {
124
            $arr = [];
125
            $arr['key'] = $text['key'];
126
            $arr['texts']['en'] = $text['value'];
127
            $arr['texts']['ir'] = $text['value'];
128
129
            $newTexts[] = $arr;
130
        }
131
132
        $yaml = Yaml::dump($newTexts, 3, 2, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);*/
133
134
        $path = storage_path('texts/site.yml');
0 ignored issues
show
Unused Code introduced by
/*$texts = Text::where('...path('texts/site.yml'); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
135
136
        //dump(file_put_contents($path, $yaml));
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
137
        //die;
138
139
        $value = Yaml::parse(file_get_contents($path));
140
        dump($value);
141
        die;
0 ignored issues
show
Coding Style Compatibility introduced by
The method handle() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
142
143
        //$this->info('Database backup restored successfully');
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
144
    }
145
146
    protected function export($scope = 'global', $force = false, $clear = false)
147
    {
148
        $dbTexts = $this->getTextsFromDb($scope);
149
150
        $fileTexts = ! $clear ? $this->getTextsFromFile($scope) : [];
151
152
        $textsToWrite = $force ? array_replace_recursive($fileTexts, $dbTexts) : array_replace_recursive($dbTexts, $fileTexts);
153
154
        // Reset keys
155
        $textsToWrite = array_values($textsToWrite);
156
157
        $yaml = Yaml::dump($textsToWrite, 3, 2, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
158
159
        $path    = $this->path . '/' . $scope . '.yml';
160
        $written = file_put_contents($path, $yaml);
161
        if (! $written) {
162
            $this->error('Export texts of "' . $scope . '" is failed!');
163
        }
164
165
        $this->info('Export texts of "' . $scope . '" is finished in "' . $path . '"');
166
    }
167
168
    /**
169
     * Get a texts from file.
170
     *
171
     * @return array
172
     */
173
    protected function getTextsFromFile($scope)
174
    {
175
        $fileTexts = [];
176
        $path      = $this->path . '/' . $scope . '.yml';
177
        if (is_readable($path)) {
178
            $fileTexts = Yaml::parse(file_get_contents($path));
179
        }
180
181
        $formattedFileTexts = [];
182
        foreach ($fileTexts as $text) {
0 ignored issues
show
Bug introduced by
The expression $fileTexts of type array|string|object<stdClass> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
183
            $formattedFileTexts[$text['key']] = $text;
184
        }
185
186
        return $formattedFileTexts;
187
    }
188
189
    /**
190
     * Get a texts from database.
191
     *
192
     * @return array
193
     */
194
    protected function getTextsFromDb($scope)
195
    {
196
        $dbTexts = $this->db
197
            ->table($this->table)
198
            ->where('scope', $scope)
199
            ->get();
200
201
        $formattedDbTexts = [];
202
        foreach ($dbTexts as $text) {
203
            $key  = $text->key;
204
            $lang = $text->lang;
205
            if (! isset($formattedDbTexts[$key])) {
206
                $formattedDbTexts[$key] = ['key' => $key];
207
            }
208
            $formattedDbTexts[$key]['texts'][$lang] = $text->value;
209
        }
210
        return $formattedDbTexts;
211
    }
212
213
    /**
214
     * Get a database connection instance.
215
     *
216
     * @return \Illuminate\Database\Connection
217
     */
218 View Code Duplication
    protected function getDatabase()
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...
219
    {
220
        $connection = config('multilang.db.connection', 'default');
221
        $db         = App::make(Database::class);
222
        if ($connection == 'default') {
223
            return $db->connection();
224
        }
225
        return $db->connection($connection);
226
    }
227
}
228