Diff::performAction()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 12.7161

Importance

Changes 0
Metric Value
cc 6
eloc 17
nc 5
nop 0
dl 0
loc 25
ccs 6
cts 14
cp 0.4286
crap 12.7161
rs 9.0777
c 0
b 0
f 0
1
<?php
2
3
namespace Swaggest\JsonCli;
4
5
6
use Yaoi\Command;
7
8
class Diff extends BaseDiff
9
{
10
    public $prettyShort;
11
    public $merge;
12
13
    /**
14
     * @param Command\Definition $definition
15
     * @param \stdClass|static $options
16 1
     */
17
    public static function setUpDefinition(Command\Definition $definition, $options)
18 1
    {
19 1
        parent::setUpDefinition($definition, $options);
20 1
        $definition->description = 'Make patch from two json documents, output to STDOUT';
21 1
        $options->prettyShort = Command\Option::create()
22 1
            ->setDescription('Pretty short format');
23
        $options->merge = Command\Option::create()
24
            ->setDescription('Use merge patch (RFC 7386)');
25 1
    }
26
27 1
28 1
    public function performAction()
29
    {
30
        $this->prePerform();
31
        if (null === $this->diff) {
32 1
            return;
33
        }
34 1
35 1
        if ($this->merge) {
36
            $this->out = $this->diff->getMergePatch();
37
        } else {
38
            $this->out = $this->diff->getPatch();
39
            $outJson = $this->out->jsonSerialize();
40
            if ($this->prettyShort && !empty($outJson)) {
41
                $out = '[';
42
                foreach ($outJson as $item) {
43
                    $out .= "\n    " . json_encode($item, JSON_UNESCAPED_SLASHES) . ',';
44
                }
45
                $out = substr($out, 0, -1);
46 1
                $out .= "\n]";
47 1
                $this->response->addContent($out);
48
                return;
49
            }
50
        }
51
52
        $this->postPerform();
53
    }
54
}