Completed
Push — laravel-5 ( cdba04...7f413f )
by Andy
9s
created

ExportCommand::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
namespace JsLocalization\Console;
3
4
use Config;
5
use Illuminate\Console\Command;
6
use File;
7
use JsLocalization\Exceptions\ConfigException;
8
use JsLocalization\Facades\ConfigCachingService;
9
use JsLocalization\Facades\MessageCachingService;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Input\InputArgument;
12
13
class ExportCommand extends Command
14
{
15
    /**
16
     * The console command name.
17
     *
18
     * @var string
19
     */
20
    protected $name = 'js-localization:export';
21
22
    /**
23
     * The console command signature.
24
     *
25
     * @var string
26
     */
27
    protected $signature = 'js-localization:export {--no-cache : Ignores cache completely}';
28
29
    /**
30
     * The console command description.
31
     *
32
     * @var string
33
     */
34
    protected $description = "Refresh message cache and export to static files";
35
36
    /**
37
     *  Options defined for Laravel < 5.1
38
     *
39
     * @return array
40
     */
41 3
    protected function getOptions()
42
    {
43
        return [
44 3
            ['no-cache', 'd', InputOption::VALUE_NONE, 'Ignores cache completely'],
45 3
        ];
46
    }
47
48
    /**
49
     * Execute the console command.
50
     *
51
     * @return void
52
     * @throws ConfigException
53
     */
54
    public function handle()
55
    {
56
        $noCache = $this->option('no-cache');
57
        if ($noCache == true) $this->line('Exporting messages and config...');
58
        else $this->line('Refreshing and exporting the message and config cache...');
59
60
        $locales = Config::get('js-localization.locales');
61
62
        if(!is_array($locales)) {
63
          throw new ConfigException('Please set the "locales" config! See https://github.com/andywer/laravel-js-localization#configuration');
64
        }
65
66
        if ($noCache == false) MessageCachingService::refreshCache();
67
        $messagesFilePath = $this->createPath('messages.js');
68
        $this->generateMessagesFile($messagesFilePath, $noCache);
0 ignored issues
show
Documentation introduced by
$noCache 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...
69
70
        if ($noCache == false) ConfigCachingService::refreshCache();
71
        $configFilePath = $this->createPath('config.js');
72
        $this->generateConfigFile($configFilePath, $noCache);
0 ignored issues
show
Documentation introduced by
$noCache 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...
73
    }
74
    
75
    /**
76
     * Execute the console command.
77
     * Compatibility with previous Laravel 5.x versions.
78
     *
79
     * @return void
80
     * @throws ConfigException
81
     */
82
    public function fire()
83
    {
84
        $this->handle();
85
    }
86
87
    /**
88
     * Create full file path.
89
     * This method will also generate the directories if they don't exist already.
90
     *
91
     * @var string $filename
92
     *
93
     * @return string $path
94
     */
95
    public function createPath($filename)
96
    {
97
        $dir = Config::get('js-localization.storage_path');
98
        if (!is_dir($dir)) {
99
            mkdir($dir, '0777', true);
100
        }
101
102
        return $dir . $filename;
103
    }
104
105
    /**
106
     * Generate the messages file.
107
     *
108
     * @param string $path
109
     * @param bool $noCache
110
     */
111
    public function generateMessagesFile($path, $noCache = false)
112
    {
113
        $splitFiles = Config::get('js-localization.split_export_files');
114
        $messages = MessageCachingService::getMessagesJson($noCache);
0 ignored issues
show
Unused Code introduced by
The call to MessageCachingService::getMessagesJson() has too many arguments starting with $noCache.

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...
115
116
        if ($splitFiles) {
117
            $this->generateMessageFiles(File::dirname($path), $messages);
118
        }
119
120
        $contents = 'Lang.addMessages(' . $messages . ');';
121
122
        File::put($path, $contents);
123
124
        $this->line("Generated $path");
125
    }
126
127
    /**
128
     * Generate the lang-{locale}.js files
129
     *
130
     * @param string $path Directory to where we will store the files
131
     * @param string $messages JSON string of messages
132
     */
133
    protected function generateMessageFiles(string $path, string $messages)
134
    {
135
        $locales = Config::get('js-localization.locales');
136
        $messages = json_decode($messages, true);
137
138
        foreach ($locales as $locale) {
139
            $fileName = $path . "/lang-{$locale}.js";
140
141
            if (key_exists($locale, $messages)) {
142
                $content = 'Lang.addMessages(' . json_encode([$locale => $messages[$locale]], JSON_PRETTY_PRINT) . ');';
143
144
                File::put($fileName, $content);
145
                $this->line("Generated $fileName");
146
            }
147
        }
148
    }
149
150
    /**
151
     * Generate the config file.
152
     *
153
     * @param string $path
154
     * @param bool $noCache
155
     */
156
    public function generateConfigFile($path, $noCache = false)
157
    {
158
        $config = ConfigCachingService::getConfigJson($noCache);
0 ignored issues
show
Unused Code introduced by
The call to ConfigCachingService::getConfigJson() has too many arguments starting with $noCache.

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...
159
        if ($config === '{}') {
160
            $this->line('No config specified. Config not written to file.');
161
            return;
162
        }
163
164
        $contents = 'Config.addConfig(' . $config . ');';
165
166
        File::put($path, $contents);
167
168
        $this->line("Generated $path");
169
    }
170
}
171