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

Apply   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 18
cts 22
cp 0.8182
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpDefinition() 0 12 1
A performAction() 0 18 3
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
}