Completed
Push — master ( c763be...612cc4 )
by Viacheslav
02:22
created

Base::postPerform()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.128

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 8
nop 0
dl 0
loc 17
ccs 8
cts 10
cp 0.8
crap 4.128
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Swaggest\JsonCli;
4
5
6
use Swaggest\JsonDiff\Exception;
7
use Swaggest\JsonDiff\JsonDiff;
8
use Symfony\Component\Yaml\Yaml;
9
use Yaoi\Command;
10
11
abstract class Base extends Command
12
{
13
    public $originalPath;
14
    public $newPath;
15
    public $pretty;
16
    public $rearrangeArrays;
17
    public $toYaml;
18
    public $output;
19
20 1
    static function setUpDefinition(Command\Definition $definition, $options)
21
    {
22 1
        $options->originalPath = Command\Option::create()->setIsUnnamed()->setIsRequired()
23 1
            ->setDescription('Path to old (original) json file');
24 1
        $options->newPath = Command\Option::create()->setIsUnnamed()->setIsRequired()
25 1
            ->setDescription('Path to new json file');
26 1
        $options->pretty = Command\Option::create()
27 1
            ->setDescription('Pretty-print result JSON');
28 1
        $options->rearrangeArrays = Command\Option::create()
29 1
            ->setDescription('Rearrange arrays to match original');
30 1
        $options->output = Command\Option::create()
31 1
            ->setDescription('Path to output result, default STDOUT');
32 1
        $options->toYaml = Command\Option::create()->setDescription('Output in YAML format');
33 1
    }
34
35
36
    /** @var JsonDiff */
37
    protected $diff;
38
    protected $out;
39
40 3
    protected function prePerform()
41
    {
42 3
        $originalJson = file_get_contents($this->originalPath);
43 3
        if (!$originalJson) {
44
            $this->response->error('Unable to read ' . $this->originalPath);
45
            return;
46
        }
47
48 3
        $newJson = file_get_contents($this->newPath);
49 3
        if (!$newJson) {
50
            $this->response->error('Unable to read ' . $this->newPath);
51
            return;
52
        }
53
54 3
        $options = 0;
55 3
        if ($this->rearrangeArrays) {
56 3
            $options += JsonDiff::REARRANGE_ARRAYS;
57
        }
58
        try {
59 3
            $this->diff = new JsonDiff(json_decode($originalJson), json_decode($newJson), $options);
60
        } catch (Exception $e) {
61
            $this->response->error($e->getMessage());
62
            return;
63
        }
64
65 3
        $this->out = '';
66 3
    }
67
68 4
    protected function postPerform()
69
    {
70 4
        $options = JSON_UNESCAPED_SLASHES;
71 4
        if ($this->pretty) {
72 4
            $options += JSON_PRETTY_PRINT;
73
        }
74
75 4
        if ($this->toYaml) {
76
            $result = Yaml::dump($this->out, 2, 2, Yaml::DUMP_OBJECT_AS_MAP);
77
        } else {
78 4
            $result = json_encode($this->out, $options);
79
        }
80
81 4
        if ($this->output) {
82
            file_put_contents($this->output, $result);
83
        } else {
84 4
            echo $result;
85
        }
86
    }
87
}