Completed
Pull Request — laravel-5 (#35)
by
unknown
06:39
created

RefreshCommand::fire()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.2084

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 23
ccs 13
cts 17
cp 0.7647
rs 8.7972
cc 4
eloc 15
nc 4
nop 0
crap 4.2084
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 RefreshCommand extends Command
14
{
15
16
    /**
17
     * The console command name.
18
     *
19
     * @var string
20
     */
21
    protected $name = 'js-localization:refresh';
22
23
    /**
24
     * The console command description.
25
     *
26
     * @var string
27
     */
28
    protected $description = "Refresh message cache after changing the config file";
29
30
    /**
31
     * Execute the console command.
32
     *
33
     * @return void
34
     * @throws ConfigException
35
     */
36 3
    public function fire()
37
    {
38 3
        $this->line('Refreshing the message cache...');
39
40 3
        $locales = Config::get('js-localization.locales');
41
42 3
        if(!is_array($locales)) {
43
          throw new ConfigException('Please set the "locales" config! See https://github.com/andywer/laravel-js-localization#configuration');
44
        }
45
46 3
        switch (Config::get('js-localization.storage')) {
47 3
            case 'file':
48
                $this->generateMessagesFile(Config::get('js-localization.storage_path'));
49
                $this->generateConfigFile(Config::get('js-localization.storage_path'));
50
                break;
51
            // Cache is also the default option.
52 3
            case 'cache':
53 3
            default:
54 3
                MessageCachingService::refreshCache();
55 3
                ConfigCachingService::refreshCache();
56 3
                break;
57 3
        }
58 3
    }
59
60
    /**
61
     * Generate the messages file.
62
     *
63
     * @param string $path
64
     */
65
    public function generateMessagesFile($path)
66
    {
67
        $messages = MessageCachingService::getMessagesJson();
68
        $messages = $this->ensureBackwardsCompatibility($messages);
69
70
        $contents  = 'Lang.addMessages(' . $messages . ');';
71
72
        if (!is_dir($path)) {
73
            mkdir($path, '0777', true);
74
        }
75
        $path = $path . 'messages';
76
        File::put($path, $contents);
77
78
        $this->line("Generated $path");
79
    }
80
81
    /**
82
     * Generage the config file.
83
     *
84
     * @param string $path
85
     */
86
    public function generateConfigFile($path)
87
    {
88
        $config = ConfigCachingService::getConfigJson();
89
        if ($config === '{}') {
90
            return;
91
        }
92
93
        $contents = 'Config.addConfig(' . $config . ');';
94
95
        if (!is_dir($path)) {
96
            mkdir($path, '0777', true);
97
        }
98
        $path = $path . 'config';
99
        File::put($path, $contents);
100
101
        $this->line("Generated $path");
102
    }
103
104
    /**
105
     * Transforms the cached data to stay compatible to old versions of the package.
106
     *
107
     * @param string $messages
108
     * @return string
109
     */
110
    protected function ensureBackwardsCompatibility($messages)
111
    {
112
        if (preg_match('/^\\{"[a-z]{2}":/', $messages)) {
113
            return $messages;
114
        } else {
115
            return '{"' . app()->getLocale() . '":' . $messages . '}';
116
        }
117
    }
118
}
119