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

Apply::performAction()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.3332

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 6
nop 0
dl 0
loc 18
ccs 8
cts 12
cp 0.6667
crap 3.3332
rs 9.4285
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
28 1
        parent::setUpDefinition($definition, $options);
29
30 1
        $definition->description = 'Apply patch to base json document, output to STDOUT';
31 1
        $options->tolerateErrors = Command\Option::create()
32 1
            ->setDescription('Continue on error');
33 1
    }
34
35 1
    public function performAction()
36
    {
37 1
        $patchJson = $this->readData($this->patchPath);
38 1
        $base = $this->readData($this->basePath);
39
40
        try {
41 1
            $patch = JsonPatch::import($patchJson);
42 1
            $errors = $patch->apply($base, !$this->tolerateErrors);
43 1
            foreach ($errors as $error) {
44
                $this->response->error($error->getMessage());
45
            }
46 1
            $this->out = $base;
47
        } catch (Exception $e) {
48
            $this->response->error($e->getMessage());
49
            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...
50
        }
51
52 1
        $this->postPerform();
53 1
    }
54
55
}