Issues (32)

src/Console/Command/OrderJsonFiles.php (1 issue)

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 FacturaScripts\Core\Base\FileManager;
0 ignored issues
show
The type FacturaScripts\Core\Base\FileManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use FacturaScriptsUtils\Console\ConsoleAbstract;
24
25
/**
26
 * Class OrderJsonFiles
27
 *
28
 * @author Francesc Pineda Segarra <[email protected]>
29
 */
30
class OrderJsonFiles extends ConsoleAbstract
31
{
32
    /**
33
     * Constant values for return, to easy know how execution ends.
34
     */
35
    const RETURN_SUCCESS = 0;
36
    const RETURN_SRC_FOLDER_NOT_SET = 1;
37
    const RETURN_DST_FOLDER_NOT_SET = 2;
38
    const RETURN_CANT_CREATE_FOLDER = 3;
39
    const RETURN_FAIL_SAVING_FILE = 4;
40
    const RETURN_NO_FILES = 5;
41
    const RETURN_SRC_FOLDER_NOT_EXISTS = 6;
42
43
    /**
44
     * Folder source path.
45
     *
46
     * @var string
47
     */
48
    private $srcFolder;
49
50
    /**
51
     * Folder destiny path.
52
     *
53
     * @var string
54
     */
55
    private $dstFolder;
56
57
    /**
58
     * Set default source folder.
59
     *
60
     * @param string $srcFolder
61
     *
62
     * @return $this
63
     */
64
    public function setSrcFolder(string $srcFolder): self
65
    {
66
        $this->srcFolder = $srcFolder;
67
        return $this;
68
    }
69
70
    /**
71
     * Set default destiny folder.
72
     *
73
     * @param string $dstFolder
74
     *
75
     * @return $this
76
     */
77
    public function setDstFolder(string $dstFolder): self
78
    {
79
        $this->dstFolder = $dstFolder;
80
        return $this;
81
    }
82
83
    /**
84
     * Start point to run the command.
85
     *
86
     * @param array $params
87
     *
88
     * @return int
89
     */
90
    public function run(...$params): int
91
    {
92
        $this->autoReply = (bool) $params[2] ?? false;
93
        $this->autoHide = (bool) $params[3] ?? false;
94
95
        $status = $this->checkOptions($params);
96
        if ($status !== 0) {
97
            return $status;
98
        }
99
100
        $this->setSrcFolder(\FS_FOLDER . ($params[0] ?? 'Core/Translation/'));
101
        $this->setDstFolder(\FS_FOLDER . ($params[1] ?? 'Core/Translation/'));
102
103
        $this->showMessage('Ordering JSON content' . \PHP_EOL . \PHP_EOL);
104
        $this->showMessage('   Options setted:' . \PHP_EOL);
105
        $this->showMessage('      Source path: ' . $this->srcFolder . \PHP_EOL);
106
        $this->showMessage('      Destiny path: ' . $this->dstFolder . \PHP_EOL);
107
108
        if (!$this->areYouSure($this->autoReply)) {
109
            $this->showMessage('   Options [SRC] [DST] [TAG]' . \PHP_EOL);
110
            return self::RETURN_SUCCESS;
111
        }
112
113
        $status = $this->check();
114
        if ($status !== 0) {
115
            return $status;
116
        }
117
118
        $files = FileManager::scanFolder($this->srcFolder);
119
120
        if (\count($files) === 0) {
121
            trigger_error('ERROR: No files on folder' . \PHP_EOL . \PHP_EOL);
122
            return self::RETURN_NO_FILES;
123
        }
124
125
        return $this->orderJson($files);
126
    }
127
128
    /**
129
     * Return description about this class.
130
     *
131
     * @return string
132
     */
133
    public function getDescription(): string
134
    {
135
        return 'Order JSON content files by key.';
136
    }
137
138
    /**
139
     * Print help information to the user.
140
     *
141
     * @return string
142
     */
143
    public function getHelpMsg(): string
144
    {
145
        $array = \explode('\\', __CLASS__);
146
        $class = array_pop($array);
147
        return 'Use as: php vendor/bin/console ' . $class . ' [OPTIONS]' . \PHP_EOL
148
            . 'Available options:' . \PHP_EOL
149
            . '   -h, --help        Show this help.' . \PHP_EOL
150
            . \PHP_EOL
151
            . '   OPTION1           Source path' . \PHP_EOL
152
            . '   OPTION2           Destiny path' . \PHP_EOL
153
            . \PHP_EOL;
154
    }
155
156
    /**
157
     * Check if options are looking for help.
158
     *
159
     * @param array $params
160
     *
161
     * @return int
162
     */
163
    private function checkOptions(array $params = []): int
164
    {
165
        if (isset($params[0])) {
166
            switch ($params[0]) {
167
                case '-h':
168
                case '--help':
169
                    $this->showMessage($this->getHelpMsg());
170
                    return -1;
171
            }
172
        }
173
        return 0;
174
    }
175
176
    /**
177
     * Launch basic checks.
178
     *
179
     * @return int
180
     */
181
    private function check(): int
182
    {
183
        if ($this->srcFolder === null) {
184
            trigger_error('ERROR: Source folder not setted.' . \PHP_EOL . \PHP_EOL);
185
            return self::RETURN_SRC_FOLDER_NOT_SET;
186
        }
187
        if ($this->dstFolder === null) {
188
            trigger_error('ERROR: Destiny folder not setted.' . \PHP_EOL . \PHP_EOL);
189
            return self::RETURN_DST_FOLDER_NOT_SET;
190
        }
191
        if (!is_dir($this->srcFolder)) {
192
            trigger_error('ERROR: Source folder ' . $this->srcFolder . ' not exists.' . \PHP_EOL . \PHP_EOL);
193
            return self::RETURN_SRC_FOLDER_NOT_EXISTS;
194
        }
195
        if (!is_file($this->dstFolder) && !@mkdir($this->dstFolder) && !is_dir($this->dstFolder)) {
196
            trigger_error("ERROR: Can't create folder " . $this->dstFolder . '.' . \PHP_EOL . \PHP_EOL);
197
            return self::RETURN_CANT_CREATE_FOLDER;
198
        }
199
        return self::RETURN_SUCCESS;
200
    }
201
202
    /**
203
     * Order JSON files
204
     *
205
     * @param array $files
206
     *
207
     * @return int
208
     */
209
    private function orderJson(array $files): int
210
    {
211
        foreach ($files as $fileName) {
212
            $arrayContent = $this->readJSON($this->srcFolder . $fileName);
213
            \ksort($arrayContent);
214
            if (!$this->saveJSON($arrayContent, $this->dstFolder . $fileName)) {
215
                trigger_error("ERROR: Can't save file " . $fileName . \PHP_EOL);
216
            }
217
        }
218
219
        $this->showMessage('Finished! Look at "' . $this->dstFolder . '"' . \PHP_EOL);
220
        return self::RETURN_SUCCESS;
221
    }
222
223
    /**
224
     * Reads a JSON from disc and return it content as array.
225
     *
226
     * @param string $pathName
227
     *
228
     * @return array
229
     */
230
    private function readJSON(string $pathName): array
231
    {
232
        $data = json_decode(file_get_contents($pathName), true);
233
        return \is_array($data) ? (array) $data : [];
234
    }
235
236
    /**
237
     * Write a JSON from an array to disc and return its result.
238
     *
239
     * @param array  $data
240
     * @param string $pathName
241
     *
242
     * @return int
243
     */
244
    private function saveJSON(array $data, string $pathName): int
245
    {
246
        $jsonData = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
247
        return (int) file_put_contents($pathName, $jsonData);
248
    }
249
}
250