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

ResolvePos::setUpDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Swaggest\JsonCli;
4
5
6
use Swaggest\JsonDiff\Exception;
7
use Swaggest\JsonDiff\JsonPointer;
8
use Yaoi\Command;
9
use Yaoi\Command\Definition;
10
use Yaoi\Io\Content\Rows;
11
12
class ResolvePos extends Command
13
{
14
    public $path;
15
    public $pointer;
16
    public $dumpAll;
17
18
    /**
19
     * @param Definition $definition
20
     * @param \stdClass|static $options
21
     */
22 1
    static function setUpDefinition(Definition $definition, $options)
23
    {
24 1
        $options->path = Command\Option::create()->setType()->setIsUnnamed()
25 1
            ->setDescription('Path to JSON file');
26 1
        $options->pointer = Command\Option::create()->setType()->setIsUnnamed()
27 1
            ->setDescription('JSON Pointer, example /key4/1/a');
28 1
        $options->dumpAll = Command\Option::create()->setDescription('Dump all pointer positions from JSON');
29 1
    }
30
31 1
    public function performAction()
32
    {
33 1
        $listener = new FilePosition\PositionResolver();
34 1
        $stream = fopen($this->path, 'r');
35
        try {
36 1
            $parser = new \JsonStreamingParser\Parser($stream, $listener);
0 ignored issues
show
Bug introduced by
It seems like $stream can also be of type false; however, parameter $stream of JsonStreamingParser\Parser::__construct() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

36
            $parser = new \JsonStreamingParser\Parser(/** @scrutinizer ignore-type */ $stream, $listener);
Loading history...
37 1
            $parser->parse();
38 1
            fclose($stream);
0 ignored issues
show
Bug introduced by
It seems like $stream can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
            fclose(/** @scrutinizer ignore-type */ $stream);
Loading history...
39
        } catch (\Exception $e) {
40
            fclose($stream);
41
            $this->response->error($e->getMessage());
42
            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...
43
        }
44
45 1
        if ($this->dumpAll) {
46
            $rows = array();
47
            foreach ($listener->resolved as $pointer => $pos) {
48
                $rows[] = array(
49
                    'Pos' => $pos,
50
                    'Ptr' => $pointer,
51
                );
52
            }
53
            $this->response->addContent(new Rows(new \ArrayIterator($rows)));
54
        } else {
55
            try {
56
                // Convert to non-URI pointer
57 1
                $pointer = JsonPointer::buildPath(JsonPointer::splitPath($this->pointer));
58
            } catch (Exception $e) {
59
                $this->response->error($e->getMessage());
60
                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...
61
            }
62
63 1
            if (isset($listener->resolved[$pointer])) {
64 1
                $this->response->addContent($listener->resolved[$pointer]);
65
            } else {
66
                $this->response->error('Pointer not found');
67
                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...
68
            }
69
        }
70
71
72 1
    }
73
74
75
}