Completed
Push — master ( 8ae96d...916040 )
by Viacheslav
02:08
created

Apply::performAction()   C

Complexity

Conditions 7
Paths 11

Size

Total Lines 37
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 12.419

Importance

Changes 0
Metric Value
cc 7
eloc 25
nc 11
nop 0
dl 0
loc 37
ccs 13
cts 25
cp 0.52
crap 12.419
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace Swaggest\JsonCli;
4
5
use Swaggest\JsonDiff\Exception;
6
use Swaggest\JsonDiff\JsonPatch;
7
use Yaoi\Command;
8
use Yaoi\Command\Definition;
9
10
class Apply extends Base
11
{
12
    public $patchPath;
13
    public $basePath;
14
    public $isURIFragmentId = false;
15
    public $tolerateErrors;
16
17
    /**
18
     * @param Definition $definition
19
     * @param \stdClass|static $options
20
     */
21 1
    static function setUpDefinition(Definition $definition, $options)
22
    {
23 1
        $options->patchPath = Command\Option::create()->setType()->setIsUnnamed()
24 1
            ->setDescription('Path to JSON patch file');
25 1
        $options->basePath = Command\Option::create()->setType()->setIsUnnamed()
26 1
            ->setDescription('Path to JSON base file');
27 1
        parent::setUpDefinition($definition, $options);
28 1
        $definition->description = 'Apply patch to base json document, output to STDOUT';
29 1
        $options->tolerateErrors = Command\Option::create()
30 1
            ->setDescription('Continue on error');
31 1
        unset($options->originalPath);
32 1
        unset($options->newPath);
33
34 1
    }
35
36 1
    public function performAction()
37
    {
38 1
        if (!file_exists($this->patchPath)) {
39
            $this->response->error('File not found: ' . $this->patchPath);
40
            return;
41
        }
42 1
        if (!file_exists($this->basePath)) {
43
            $this->response->error('File not found: ' . $this->basePath);
44
            return;
45
        }
46
47 1
        $patchJson = file_get_contents($this->patchPath);
48 1
        if (!$patchJson) {
49
            $this->response->error('Unable to read ' . $this->patchPath);
50
            return;
51
        }
52
53 1
        $baseJson = file_get_contents($this->basePath);
54 1
        if (!$baseJson) {
55
            $this->response->error('Unable to read ' . $this->basePath);
56
            return;
57
        }
58
59
        try {
60 1
            $patch = JsonPatch::import(json_decode($patchJson));
61 1
            $base = json_decode($baseJson);
62 1
            $errors = $patch->apply($base, !$this->tolerateErrors);
63 1
            foreach ($errors as $error) {
64
                $this->response->error($error->getMessage());
65
            }
66 1
            $this->out = $base;
67
        } catch (Exception $e) {
68
            $this->response->error($e->getMessage());
69
            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...
70
        }
71
72 1
        $this->postPerform();
73 1
    }
74
75
}