Completed
Push — master ( a64d37...bc15d8 )
by Viacheslav
06:34
created

Base::readJsonOrYaml()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.2

Importance

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