Completed
Push — master ( d6e24f...c5cb7d )
by Viacheslav
02:06
created

BaseDiff   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 73.08%

Importance

Changes 0
Metric Value
dl 0
loc 49
ccs 19
cts 26
cp 0.7308
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
B prePerform() 0 26 5
A setUpDefinition() 0 12 1
1
<?php
2
3
namespace Swaggest\JsonCli;
4
5
use Swaggest\JsonDiff\Exception;
6
use Swaggest\JsonDiff\JsonDiff;
7
use Yaoi\Command;
8
9
abstract class BaseDiff extends Base
10
{
11
    public $originalPath;
12
    public $newPath;
13
    public $rearrangeArrays;
14
15 1
    static function setUpDefinition(Command\Definition $definition, $options)
16
    {
17 1
        $options->originalPath = Command\Option::create()->setIsUnnamed()->setIsRequired()
18 1
            ->setDescription('Path to old (original) json file');
19
20 1
        $options->newPath = Command\Option::create()->setIsUnnamed()->setIsRequired()
21 1
            ->setDescription('Path to new json file');
22
23 1
        $options->rearrangeArrays = Command\Option::create()
24 1
            ->setDescription('Rearrange arrays to match original');
25
26 1
        parent::setUpDefinition($definition, $options);
27 1
    }
28
29
    /** @var JsonDiff */
30
    protected $diff;
31
32 3
    protected function prePerform()
33
    {
34 3
        $originalJson = file_get_contents($this->originalPath);
35 3
        if (!$originalJson) {
36
            $this->response->error('Unable to read ' . $this->originalPath);
37
            return;
38
        }
39
40 3
        $newJson = file_get_contents($this->newPath);
41 3
        if (!$newJson) {
42
            $this->response->error('Unable to read ' . $this->newPath);
43
            return;
44
        }
45
46 3
        $options = 0;
47 3
        if ($this->rearrangeArrays) {
48 3
            $options += JsonDiff::REARRANGE_ARRAYS;
49
        }
50
        try {
51 3
            $this->diff = new JsonDiff(json_decode($originalJson), json_decode($newJson), $options);
52
        } catch (Exception $e) {
53
            $this->response->error($e->getMessage());
54
            return;
55
        }
56
57 3
        $this->out = '';
58 3
    }
59
60
}