Passed
Push — master ( 0012be...178242 )
by Francesc
01:47
created

OrderJsonFiles::saveJSON()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of FSConsoleTools
4
 * Copyright (C) 2018 Francesc Pineda Segarra <[email protected]>
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation, either version 3 of the
9
 * License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace FacturaScriptsUtils\Console\Command;
21
22
use FacturaScriptsUtils\Console\ConsoleAbstract;
23
24
/**
25
 * Class OrderJsonFiles
26
 *
27
 * @author Francesc Pineda Segarra <[email protected]>
28
 */
29
class OrderJsonFiles extends ConsoleAbstract
30
{
31
    /**
32
     * Constant values for return, to easy know how execution ends.
33
     */
34
    const RETURN_SUCCESS = 0;
35
    const RETURN_SRC_FOLDER_NOT_SET = 1;
36
    const RETURN_DST_FOLDER_NOT_SET = 2;
37
    const RETURN_CANT_CREATE_FOLDER = 3;
38
    const RETURN_FAIL_SAVING_FILE = 4;
39
    const RETURN_NO_FILES = 5;
40
    const RETURN_SRC_FOLDER_NOT_EXISTS = 6;
41
42
    /**
43
     * Folder source path.
44
     *
45
     * @var string
46
     */
47
    private $folderSrcPath;
48
49
    /**
50
     * Folder destiny path.
51
     *
52
     * @var string
53
     */
54
    private $folderDstPath;
55
56
    /**
57
     * Set default source folder.
58
     *
59
     * @param string $folderSrcPath
60
     */
61
    public function setFolderSrcPath($folderSrcPath)
62
    {
63
        $this->folderSrcPath = $folderSrcPath;
64
    }
65
66
    /**
67
     * Set default destiny folder.
68
     *
69
     * @param string $folderDstPath
70
     */
71
    public function setFolderDstPath($folderDstPath)
72
    {
73
        $this->folderDstPath = $folderDstPath;
74
    }
75
76
    /**
77
     * Start point to run the command.
78
     *
79
     * @param array $params
80
     *
81
     * @return int
82
     */
83
    public function run(...$params): int
84
    {
85
        $this->checkOptions($params);
86
87
        $this->setFolderSrcPath($params[0] ?? \FS_FOLDER . 'Core/Translation/');
88
        $this->setFolderDstPath($params[1] ?? \FS_FOLDER . 'Core/Translation/');
89
90
        echo 'Options setted:' . \PHP_EOL;
91
        echo '   Source path: ' . $this->folderSrcPath . \PHP_EOL;
92
        echo '   Destiny path: ' . $this->folderDstPath . \PHP_EOL;
93
        if (!$this->areYouSure()) {
94
            echo '   Options [SRC] [DST] [TAG]' . \PHP_EOL;
95
            return self::RETURN_SUCCESS;
96
        }
97
98
        $status = $this->check();
99
        if ($status > 0) {
100
            return $status;
101
        }
102
103
        $files = array_diff(scandir($this->folderSrcPath, SCANDIR_SORT_ASCENDING), ['.', '..']);
104
105
        if (\count($files) === 0) {
106
            echo 'ERROR: No files on folder' . \PHP_EOL;
107
            return self::RETURN_NO_FILES;
108
        }
109
110
        return $this->orderJson($files);
111
    }
112
113
    /**
114
     * Return description about this class.
115
     *
116
     * @return string
117
     */
118
    public function getDescription(): string
119
    {
120
        return 'Order JSON content files by key.';
121
    }
122
123
    /**
124
     * Print help information to the user.
125
     */
126
    public function showHelp()
127
    {
128
        $array = \explode('\\', __CLASS__);
129
        $class = array_pop($array);
130
        echo 'Use as: php vendor/bin/console ' . $class . ' [OPTIONS]' . \PHP_EOL;
131
        echo 'Available options:' . \PHP_EOL;
132
        echo '   -h, --help        Show this help.' . \PHP_EOL;
133
        echo \PHP_EOL;
134
        echo '   OPTION1           Source path' . \PHP_EOL;
135
        echo '   OPTION2           Destiny path' . \PHP_EOL;
136
        echo \PHP_EOL;
137
    }
138
139
    /**
140
     * @param array $params
141
     */
142
    private function checkOptions(array $params = [])
143
    {
144
        if (isset($params[0])) {
145
            switch ($params[0]) {
146
                case '-h':
147
                case '--help':
148
                    $this->showHelp();
149
                    break;
150
            }
151
            die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
152
        }
153
    }
154
155
    /**
156
     * Launch basic checks.
157
     *
158
     * @return int
159
     */
160
    private function check(): int
161
    {
162
        if ($this->folderSrcPath === null) {
163
            echo 'ERROR: Source folder not setted.' . \PHP_EOL;
164
            return self::RETURN_SRC_FOLDER_NOT_SET;
165
        }
166
        if ($this->folderDstPath === null) {
167
            echo 'ERROR: Destiny folder not setted.' . \PHP_EOL;
168
            return self::RETURN_DST_FOLDER_NOT_SET;
169
        }
170
        if (!is_dir($this->folderSrcPath)) {
171
            echo 'ERROR: Source folder ' . $this->folderSrcPath . ' not exists.' . \PHP_EOL;
172
            return self::RETURN_SRC_FOLDER_NOT_EXISTS;
173
        }
174
        if (!is_file($this->folderDstPath) && !@mkdir($this->folderDstPath) && !is_dir($this->folderDstPath)) {
175
            echo "ERROR: Can't create folder " . $this->folderDstPath;
176
            return self::RETURN_CANT_CREATE_FOLDER;
177
        }
178
        return self::RETURN_SUCCESS;
179
    }
180
181
    /**
182
     * Order JSON files
183
     *
184
     * @param array $files
185
     *
186
     * @return int
187
     */
188
    private function orderJson($files): int
189
    {
190
        foreach ($files as $fileName) {
191
            $arrayContent = $this->readJSON($this->folderSrcPath . $fileName);
192
            \ksort($arrayContent);
193
            if (!$this->saveJSON($arrayContent, $this->folderDstPath . $fileName)) {
194
                echo "ERROR: Can't save file " . $fileName . \PHP_EOL;
195
            }
196
        }
197
198
        echo 'Finished! Look at "' . $this->folderDstPath . '"' . \PHP_EOL;
199
        return self::RETURN_SUCCESS;
200
    }
201
202
    /**
203
     * Reads a JSON from disc and return it content as array.
204
     *
205
     * @param string $pathName
206
     *
207
     * @return mixed
208
     */
209
    private function readJSON(string $pathName)
210
    {
211
        return json_decode(file_get_contents($pathName), true);
212
    }
213
214
    /**
215
     * Write a JSON from an array to disc and return its result.
216
     *
217
     * @param array  $data
218
     * @param string $pathName
219
     *
220
     * @return bool|int
221
     */
222
    private function saveJSON(array $data, string $pathName)
223
    {
224
        $jsonData = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
225
        return file_put_contents($pathName, $jsonData);
226
    }
227
}
228