Passed
Pull Request — master (#3)
by Viacheslav
02:25 queued 58s
created

Base   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 81.82%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 37
c 1
b 0
f 1
dl 0
loc 72
ccs 27
cts 33
cp 0.8182
rs 10
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A postPerform() 0 19 5
A readData() 0 3 1
A setUpDefinition() 0 8 1
A readJsonOrYaml() 0 16 5
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
     */
20 1
    static function setUpDefinition(Command\Definition $definition, $options)
21
    {
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 1
    }
29
30
31
    protected $out;
32
33 6
    protected function readData($path)
34
    {
35 6
        return self::readJsonOrYaml($path, $this->response);
36
    }
37
38
    /**
39
     * @param $path
40
     * @param Response $response
41
     * @return mixed
42
     */
43 9
    public static function readJsonOrYaml($path, $response)
44
    {
45 9
        $fileData = file_get_contents($path);
46 9
        if (!$fileData) {
47
            $response->error('Unable to read ' . $path);
48
            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 9
        if (substr($path, -5) === '.yaml' || substr($path, -4) === '.yml') {
51 1
            $jsonData = Yaml::parse($fileData, Yaml::PARSE_OBJECT + Yaml::PARSE_OBJECT_FOR_MAP);
52 8
        } elseif (substr($path, -11) === '.serialized') {
53
            $jsonData = unserialize($fileData);
54
        } else {
55 8
            $jsonData = json_decode($fileData);
56
        }
57
58 9
        return $jsonData;
59
    }
60
61
62 8
    protected function postPerform()
63
    {
64 8
        $options = JSON_UNESCAPED_SLASHES;
65 8
        if ($this->pretty) {
66 5
            $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 8
        } elseif ($this->toSerialized) {
72
            $result = serialize($this->out);
73
        } else {
74 8
            $result = json_encode($this->out, $options);
75
        }
76
77 8
        if ($this->output) {
78
            file_put_contents($this->output, $result);
79
        } else {
80 8
            echo $result;
81
        }
82
    }
83
}