Completed
Push — master ( 526a15...c763be )
by Viacheslav
01:41
created

Minify::performAction()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 5
nop 0
dl 0
loc 19
ccs 0
cts 12
cp 0
crap 30
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Swaggest\JsonCli;
4
5
use Symfony\Component\Yaml\Yaml;
6
use Yaoi\Command;
7
use Yaoi\Command\Definition;
8
9
class Minify extends Command
10
{
11
    public $path;
12
    public $output;
13
14
    /**
15
     * @param Definition $definition
16
     * @param \stdClass|static $options
17
     */
18 1
    static function setUpDefinition(Definition $definition, $options)
19
    {
20 1
        $options->path = Command\Option::create()->setIsUnnamed()->setIsRequired()
21 1
            ->setDescription('Path to JSON/YAML file');
22 1
        $options->output = Command\Option::create()->setType()
23 1
            ->setDescription('Path to output, default STDOUT');
24 1
        $definition->description = 'Minify JSON document';
25 1
    }
26
27
    public function performAction()
28
    {
29
        $fileData = file_get_contents($this->path);
30
        if (!$fileData) {
31
            $this->response->error('Unable to read ' . $this->path);
32
            return;
33
        }
34
        if (substr($this->path, -5) === '.yaml' || substr($this->path, -4) === '.yml') {
35
            $jsonData = Yaml::parse($fileData, Yaml::PARSE_OBJECT);
36
        } else {
37
            $jsonData = json_decode($fileData);
38
        }
39
40
        $result = json_encode($jsonData, JSON_UNESCAPED_SLASHES);
41
42
        if ($this->output) {
43
            file_put_contents($this->output, $result);
44
        } else {
45
            $this->response->addContent($result);
46
        }
47
    }
48
49
}