Passed
Push — master ( ff9e28...8752e2 )
by Francesc
01:52
created

OrderJsonFiles::setSrcFolder()   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 1
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 FacturaScripts\Core\Base\FileManager;
0 ignored issues
show
Bug introduced by
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
        $status = $this->checkOptions($params);
93
        if ($status !== 0) {
94
            return $status;
95
        }
96
97
        $this->setSrcFolder(\FS_FOLDER . ($params[0] ?? 'Core/Translation/'));
98
        $this->setDstFolder(\FS_FOLDER . ($params[1] ?? 'Core/Translation/'));
99
100
        echo 'Ordering JSON content' . \PHP_EOL . \PHP_EOL;
101
        echo '   Options setted:' . \PHP_EOL;
102
        echo '      Source path: ' . $this->srcFolder . \PHP_EOL;
103
        echo '      Destiny path: ' . $this->dstFolder . \PHP_EOL;
104
105
        if (!$this->areYouSure()) {
106
            echo '   Options [SRC] [DST] [TAG]' . \PHP_EOL;
107
            return self::RETURN_SUCCESS;
108
        }
109
110
        $status = $this->check();
111
        if ($status !== 0) {
112
            return $status;
113
        }
114
115
        $files = FileManager::scanFolder($this->srcFolder);
116
117
        if (\count($files) === 0) {
118
            echo 'ERROR: No files on folder' . \PHP_EOL . \PHP_EOL;
119
            return self::RETURN_NO_FILES;
120
        }
121
122
        return $this->orderJson($files);
123
    }
124
125
    /**
126
     * Return description about this class.
127
     *
128
     * @return string
129
     */
130
    public function getDescription(): string
131
    {
132
        return 'Order JSON content files by key.';
133
    }
134
135
    /**
136
     * Print help information to the user.
137
     *
138
     * @return string
139
     */
140
    public function getHelpMsg(): string
141
    {
142
        $array = \explode('\\', __CLASS__);
143
        $class = array_pop($array);
144
        return 'Use as: php vendor/bin/console ' . $class . ' [OPTIONS]' . \PHP_EOL
145
            . 'Available options:' . \PHP_EOL
146
            . '   -h, --help        Show this help.' . \PHP_EOL
147
            . \PHP_EOL
148
            . '   OPTION1           Source path' . \PHP_EOL
149
            . '   OPTION2           Destiny path' . \PHP_EOL
150
            . \PHP_EOL;
151
    }
152
153
    /**
154
     * Check if options are looking for help.
155
     *
156
     * @param array $params
157
     *
158
     * @return int
159
     */
160
    private function checkOptions(array $params = []): int
161
    {
162
        if (isset($params[0])) {
163
            switch ($params[0]) {
164
                case '-h':
165
                case '--help':
166
                    echo $this->getHelpMsg();
167
                    return -1;
168
            }
169
        }
170
        return 0;
171
    }
172
173
    /**
174
     * Launch basic checks.
175
     *
176
     * @return int
177
     */
178
    private function check(): int
179
    {
180
        if ($this->srcFolder === null) {
181
            echo 'ERROR: Source folder not setted.' . \PHP_EOL . \PHP_EOL;
182
            return self::RETURN_SRC_FOLDER_NOT_SET;
183
        }
184
        if ($this->dstFolder === null) {
185
            echo 'ERROR: Destiny folder not setted.' . \PHP_EOL . \PHP_EOL;
186
            return self::RETURN_DST_FOLDER_NOT_SET;
187
        }
188
        if (!is_dir($this->srcFolder)) {
189
            echo 'ERROR: Source folder ' . $this->srcFolder . ' not exists.' . \PHP_EOL . \PHP_EOL;
190
            return self::RETURN_SRC_FOLDER_NOT_EXISTS;
191
        }
192
        if (!is_file($this->dstFolder) && !@mkdir($this->dstFolder) && !is_dir($this->dstFolder)) {
193
            echo "ERROR: Can't create folder " . $this->dstFolder . '.' . \PHP_EOL . \PHP_EOL;
194
            return self::RETURN_CANT_CREATE_FOLDER;
195
        }
196
        return self::RETURN_SUCCESS;
197
    }
198
199
    /**
200
     * Order JSON files
201
     *
202
     * @param array $files
203
     *
204
     * @return int
205
     */
206
    private function orderJson(array $files): int
207
    {
208
        foreach ($files as $fileName) {
209
            $arrayContent = $this->readJSON($this->srcFolder . $fileName);
210
            \ksort($arrayContent);
211
            if (!$this->saveJSON($arrayContent, $this->dstFolder . $fileName)) {
212
                echo "ERROR: Can't save file " . $fileName . \PHP_EOL;
213
            }
214
        }
215
216
        echo 'Finished! Look at "' . $this->dstFolder . '"' . \PHP_EOL;
217
        return self::RETURN_SUCCESS;
218
    }
219
220
    /**
221
     * Reads a JSON from disc and return it content as array.
222
     *
223
     * @param string $pathName
224
     *
225
     * @return array
226
     */
227
    private function readJSON(string $pathName): array
228
    {
229
        $data = json_decode(file_get_contents($pathName), true);
230
        return \is_array($data) ? (array) $data : [];
231
    }
232
233
    /**
234
     * Write a JSON from an array to disc and return its result.
235
     *
236
     * @param array  $data
237
     * @param string $pathName
238
     *
239
     * @return int
240
     */
241
    private function saveJSON(array $data, string $pathName): int
242
    {
243
        $jsonData = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
244
        return (int) file_put_contents($pathName, $jsonData);
245
    }
246
}
247