Completed
Push — master ( 814431...bcdc84 )
by Viacheslav
10:46 queued 15s
created

Base::loadSchema()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 2
nop 2
dl 0
loc 21
ccs 0
cts 0
cp 0
crap 12
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace Swaggest\JsonCli;
4
5
use Swaggest\JsonCli\Json\LoadFile;
6
use Swaggest\JsonCli\JsonSchema\ResolverMux;
7
use Swaggest\JsonDiff\Exception;
8
use Swaggest\JsonDiff\JsonMergePatch;
9
use Swaggest\JsonDiff\JsonPatch;
10
use Swaggest\JsonSchema\Context;
11
use Swaggest\JsonSchema\RemoteRef\BasicFetcher;
12
use Swaggest\JsonSchema\RemoteRef\Preloaded;
13
use Swaggest\JsonSchema\Schema;
14
use Symfony\Component\Yaml\Yaml;
15
use Yaoi\Command;
16
use Yaoi\Io\Response;
17
18
abstract class Base extends Command
19 1
{
20
    use LoadFile;
21 1
22 1
    public $pretty;
23 1
    public $toYaml;
24 1
    public $toSerialized;
25 1
    public $output;
26 1
27 1
    /**
28
     * @param Command\Definition $definition
29
     * @param \stdClass|static $options
30
     */
31
    static function setUpDefinition(Command\Definition $definition, $options)
32 6
    {
33
        $options->pretty = Command\Option::create()
34 6
            ->setDescription('Pretty-print result JSON');
35
        $options->output = Command\Option::create()->setType()
36
            ->setDescription('Path to output result, default STDOUT');
37
        $options->toYaml = Command\Option::create()->setDescription('Output in YAML format');
38 6
        $options->toSerialized = Command\Option::create()->setDescription('Output in PHP serialized format');
39 6
    }
40
41
42
    protected $out;
43 6
44
    /**
45 6
     * @param string $path
46
     * @return mixed
47
     * @throws ExitCode
48 6
     */
49
    protected function readData($path)
50
    {
51 6
        return self::readJsonOrYaml($path, $this->response);
52
    }
53
54 8
    /**
55
     * @param string $path
56 8
     * @param Response $response
57 8
     * @return mixed
58 5
     * @throws ExitCode
59
     */
60
    public static function readJsonOrYaml($path, $response)
61 8
    {
62
        $fileData = file_get_contents($path);
63 8
        if (!$fileData) {
64
            $response->error('Unable to read ' . $path);
65
            throw new ExitCode('', 1);
66 8
        }
67
        if (substr($path, -5) === '.yaml' || substr($path, -4) === '.yml') {
68
            $jsonData = Yaml::parse($fileData, Yaml::PARSE_OBJECT + Yaml::PARSE_OBJECT_FOR_MAP);
69 8
        } elseif (substr($path, -11) === '.serialized') {
70
            $jsonData = unserialize($fileData);
71
        } else {
72 8
            $jsonData = json_decode($fileData);
73
        }
74 8
75
        return $jsonData;
76
    }
77
78
79
    protected function postPerform()
80
    {
81
        $options = JSON_UNESCAPED_SLASHES + JSON_UNESCAPED_UNICODE;
82
        if ($this->pretty) {
83
            $options += JSON_PRETTY_PRINT;
84
        }
85
86
        if ($this->toYaml) {
87
            $result = Yaml::dump($this->out, 2, 2, Yaml::DUMP_OBJECT_AS_MAP);
88
        } elseif ($this->toSerialized) {
89
            $result = serialize($this->out);
90
        } else {
91
            $result = json_encode($this->out, $options);
92
        }
93
94
        if ($this->output) {
95
            file_put_contents($this->output, $result);
96
        } else {
97
            echo $result;
98
        }
99
    }
100
101
102
    /** @var string */
103
    public $schema;
104
    /** @var string[] */
105
    public $defPtr = ['#/definitions'];
106
    /** @var string[] */
107
    public $ptrInSchema;
108
109
    /**
110
     * @param Command\Definition $definition
111
     * @param static|mixed $options
112
     */
113
    protected static function setupGenOptions(Command\Definition $definition, $options)
0 ignored issues
show
Unused Code introduced by
The parameter $definition is not used and could be removed. ( Ignorable by Annotation )

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

113
    protected static function setupGenOptions(/** @scrutinizer ignore-unused */ Command\Definition $definition, $options)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
114
    {
115
        $options->schema = Command\Option::create()
0 ignored issues
show
Documentation Bug introduced by
It seems like Yaoi\Command\Option::cre...amed()->setIsRequired() of type Yaoi\Command\Option is incompatible with the declared type string of property $schema.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
116
            ->setDescription('Path to JSON schema file')->setIsUnnamed()->setIsRequired();
117
118
        $options->ptrInSchema = Command\Option::create()->setType()->setIsVariadic()
0 ignored issues
show
Documentation Bug introduced by
It seems like Yaoi\Command\Option::cre...oot schema, default #') of type Yaoi\Command\Option is incompatible with the declared type string[] of property $ptrInSchema.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
119
            ->setDescription('JSON pointers to structure in in root schema, default #');
120
121
        $options->defPtr = Command\Option::create()->setType()->setIsVariadic()
0 ignored issues
show
Documentation Bug introduced by
It seems like Yaoi\Command\Option::cre...default #/definitions') of type Yaoi\Command\Option is incompatible with the declared type string[] of property $defPtr.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
122
            ->setDescription('Definitions pointers to strip from symbol names, default #/definitions');
123
124
        static::setupLoadFileOptions($options);
125
    }
126
127
    /**
128
     * @param bool $skipRoot
129
     * @param string $baseName
130
     * @return \Swaggest\JsonSchema\SchemaContract
131
     * @throws Exception
132
     * @throws ExitCode
133
     * @throws \Swaggest\JsonSchema\Exception
134
     * @throws \Swaggest\JsonSchema\InvalidValue
135
     */
136
    protected function loadSchema(&$skipRoot, &$baseName)
137
    {
138
        $resolver = new ResolverMux();
139
140
        $dataValue = $this->loadFile();
141
        $data = $dataValue;
142
        
143
        if (!empty($this->ptrInSchema)) {
144
            $baseName = basename($this->schema);
145
            $skipRoot = true;
146
            $preloaded = new Preloaded();
147
            $preloaded->setSchemaData($baseName, $dataValue);
148
            $resolver->resolvers[] = $preloaded;
149
            $data = new \stdClass();
150
            foreach ($this->ptrInSchema as $i => $ptr) {
151
                $data->oneOf[$i] = (object)[Schema::PROP_REF => $baseName . $ptr];
152
            }
153
        }
154
155
        $resolver->resolvers[] = new BasicFetcher();
156
        return Schema::import($data, new Context($resolver));
157
    }
158
}