Completed
Push — master ( f0fe19...0f2424 )
by Viacheslav
02:41
created

Base   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 73.52%

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 65
ccs 25
cts 34
cp 0.7352
rs 10
c 0
b 0
f 0
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A postPerform() 0 19 5
A readData() 0 20 6
A setUpDefinition() 0 8 1
1
<?php
2
3
namespace Swaggest\JsonCli;
4
5
use Symfony\Component\Yaml\Yaml;
6
use Yaoi\Command;
7
8
abstract class Base extends Command
9
{
10
    public $pretty;
11
    public $toYaml;
12
    public $toSerialized;
13
    public $output;
14
15
    /**
16
     * @param Command\Definition $definition
17
     * @param \stdClass|static $options
18
     */
19 1
    static function setUpDefinition(Command\Definition $definition, $options)
20
    {
21 1
        $options->pretty = Command\Option::create()
22 1
            ->setDescription('Pretty-print result JSON');
23 1
        $options->output = Command\Option::create()->setType()
24 1
            ->setDescription('Path to output result, default STDOUT');
25 1
        $options->toYaml = Command\Option::create()->setDescription('Output in YAML format');
26 1
        $options->toSerialized = Command\Option::create()->setDescription('Output in PHP serialized format');
27 1
    }
28
29
30
    protected $out;
31
32 6
    protected function readData($path)
33
    {
34 6
        if (!file_exists($path)) {
35
            $this->response->error('Unable to find ' . $path);
36
            die(1);
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...
37
        }
38 6
        $fileData = file_get_contents($path);
39 6
        if (!$fileData) {
40
            $this->response->error('Unable to read ' . $path);
41
            die(1);
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...
42
        }
43 6
        if (substr($path, -5) === '.yaml' || substr($path, -4) === '.yml') {
44
            $jsonData = Yaml::parse($fileData, Yaml::PARSE_OBJECT + Yaml::PARSE_OBJECT_FOR_MAP);
45 6
        } elseif (substr($path, -11) === '.serialized') {
46
            $jsonData = unserialize($fileData);
47
        } else {
48 6
            $jsonData = json_decode($fileData);
49
        }
50
51 6
        return $jsonData;
52
    }
53
54 8
    protected function postPerform()
55
    {
56 8
        $options = JSON_UNESCAPED_SLASHES;
57 8
        if ($this->pretty) {
58 5
            $options += JSON_PRETTY_PRINT;
59
        }
60
61 8
        if ($this->toYaml) {
62
            $result = Yaml::dump($this->out, 2, 2, Yaml::DUMP_OBJECT_AS_MAP);
63 8
        } elseif ($this->toSerialized) {
64
            $result = serialize($this->out);
65
        } else {
66 8
            $result = json_encode($this->out, $options);
67
        }
68
69 8
        if ($this->output) {
70
            file_put_contents($this->output, $result);
71
        } else {
72 8
            echo $result;
73
        }
74
    }
75
}