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

Minify   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 36.84%

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 7
cts 19
cp 0.3684
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
B performAction() 0 19 5
A setUpDefinition() 0 7 1
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
}