Completed
Push — master ( bc3789...0c0eda )
by Viacheslav
02:32
created

ValidateSchema::performAction()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 9.85

Importance

Changes 0
Metric Value
cc 5
eloc 21
nc 8
nop 0
dl 0
loc 27
ccs 8
cts 19
cp 0.4211
crap 9.85
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Swaggest\JsonCli;
4
5
use Swaggest\JsonSchema\InvalidValue;
6
use Swaggest\JsonSchema\Schema;
7
use Yaoi\Command;
8
9
class ValidateSchema extends Base
10
{
11
    public $schema;
12
    public $data;
13
14
    /**
15
     * @param Command\Definition $definition
16
     * @param \stdClass|static $options
17
     */
18 1
    static function setUpDefinition(Command\Definition $definition, $options)
19
    {
20 1
        $options->data = Command\Option::create()->setIsUnnamed()->setIsRequired()
21 1
            ->setDescription('Path to data (JSON/YAML)');
22 1
        $options->schema = Command\Option::create()->setIsUnnamed()
23 1
            ->setDescription('Path to schema, default JSON Schema');
24 1
    }
25
26
27 1
    public function performAction()
28
    {
29 1
        if ($this->schema) {
30 1
            $schemaData = $this->readData($this->schema);
31
            try {
32 1
                $schema = Schema::import($schemaData);
33
            } catch (InvalidValue $e) {
34
                $this->response->error('Invalid schema');
35
                $this->response->addContent($e->getMessage());
36
                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...
37
            } catch (\Exception $e) {
38
                $this->response->error('Failed to import schema:' . $e->getMessage());
39 1
                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...
40
            }
41
        } else {
42
            $schema = new Schema();
43
        }
44
45 1
        $data = $this->readData($this->data);
46
47
        try {
48 1
            $schema->in($data);
49 1
            $this->response->success('Data is valid');
50
        } catch (InvalidValue $exception) {
51
            $this->response->error('Data is invalid');
52
            $this->response->addContent($exception->getMessage());
53
            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...
54
        }
55
    }
56
}