Completed
Push — master ( 526a15...c763be )
by Viacheslav
01:41
created

Replace::performAction()   D

Complexity

Conditions 9
Paths 14

Size

Total Lines 42
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 29
nc 14
nop 0
dl 0
loc 42
ccs 0
cts 28
cp 0
crap 90
rs 4.909
c 0
b 0
f 0
1
<?php
2
3
namespace Swaggest\JsonCli;
4
5
use Swaggest\JsonDiff\JsonValueReplace;
6
use Symfony\Component\Yaml\Yaml;
7
use Yaoi\Command;
8
9
class Replace extends Command
10
{
11
    public $path;
12
    public $output;
13
14
    public $search;
15
    public $replace;
16
    public $pathFilter;
17
18
    /**
19
     * @param Command\Definition $definition
20
     * @param \stdClass|static $options
21
     */
22 1
    static function setUpDefinition(Command\Definition $definition, $options)
23
    {
24 1
        $options->path = Command\Option::create()->setIsUnnamed()->setIsRequired()
25 1
            ->setDescription('Path to JSON/YAML file');
26 1
        $options->search = Command\Option::create()->setIsUnnamed()->setIsRequired()
27 1
            ->setDescription('Search JSON value');
28 1
        $options->replace = Command\Option::create()->setIsUnnamed()->setIsRequired()
29 1
            ->setDescription('Replace JSON value');
30 1
        $options->pathFilter = Command\Option::create()->setType()
31 1
            ->setDescription('JSON path filter regex, example "/definitions/.*/properties/deletedAt"');
32 1
        $options->output = Command\Option::create()->setType()
33 1
            ->setDescription('Path to output, default STDOUT');
34 1
        $definition->description = 'Minify JSON document';
35 1
    }
36
37
    public function performAction()
38
    {
39
        if (!file_exists($this->path)) {
40
            $this->response->error('File not found: ' . $this->path);
41
            return;
42
        }
43
        $fileData = file_get_contents($this->path);
44
        if (!$fileData) {
45
            $this->response->error('Unable to read ' . $this->path);
46
            return;
47
        }
48
        if (substr($this->path, -5) === '.yaml' || substr($this->path, -4) === '.yml') {
49
            $jsonData = Yaml::parse($fileData, Yaml::PARSE_OBJECT);
50
        } else {
51
            $jsonData = json_decode($fileData);
52
        }
53
54
        $search = json_decode($this->search);
55
        if (json_last_error()) {
56
            $this->response->error('Invalid JSON: ' . $this->search);
57
            return;
58
        }
59
        $replace = json_decode($this->replace);
60
        if (json_last_error()) {
61
            $this->response->error('Invalid JSON: ' . $this->replace);
62
            return;
63
        }
64
65
        $pathFilter = null;
66
        if ($this->pathFilter) {
67
            $pathFilter = '|' . $this->pathFilter . '|';
68
        }
69
70
        $replacer = new JsonValueReplace($search, $replace, $pathFilter);
71
        $jsonData = $replacer->process($jsonData);
72
73
        $result = json_encode($jsonData, JSON_UNESCAPED_SLASHES);
74
75
        if ($this->output) {
76
            file_put_contents($this->output, $result);
77
        } else {
78
            $this->response->addContent($result);
79
        }
80
    }
81
82
}