Passed
Push — develop ( 88717d...ee51e8 )
by Freddie
03:18
created

Schema::title()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of FlexPHP.
4
 *
5
 * (c) Freddie Gar <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace FlexPHP\Schema;
11
12
use FlexPHP\Schema\Constants\Keyword;
13
use FlexPHP\Schema\Exception\InvalidFileSchemaException;
14
use FlexPHP\Schema\Exception\InvalidSchemaException;
15
use FlexPHP\Schema\Validations\SchemaAttributeValidation;
16
use Symfony\Component\Yaml\Exception\ParseException;
17
use Symfony\Component\Yaml\Yaml;
18
19
class Schema implements SchemaInterface
20
{
21
    /**
22
     * @var array<array>
23
     */
24
    private $schema;
25
26
    /**
27
     * @var string
28
     */
29
    private $name;
30
31
    /**
32
     * @var string
33
     */
34
    private $title;
35
36
    /**
37
     * @var array<array>
38
     */
39
    private $attributes;
40
41 14
    public function fromArray(array $schema): void
42
    {
43 14
        $this->schema = $schema;
44 14
    }
45
46 3
    public function fromFile(string $filename): void
47
    {
48
        try {
49 3
            $yaml = new Yaml();
50 3
            $schema = $yaml->parseFile($filename);
51 1
        } catch (ParseException $e) {
52 1
            throw new InvalidFileSchemaException();
53
        }
54
55 2
        $this->schema = $schema;
56 2
    }
57
58 15
    public function load(): void
59
    {
60 15
        if (empty($this->schema)) {
61 1
            throw new InvalidSchemaException('Schema is empty');
62
        }
63
64 14
        $this->setName((string) \key($this->schema) ?? null);
0 ignored issues
show
Bug introduced by
It seems like (string)key($this->schema) ?? null can also be of type null; however, parameter $name of FlexPHP\Schema\Schema::setName() does only seem to accept string, 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

64
        $this->setName(/** @scrutinizer ignore-type */ (string) \key($this->schema) ?? null);
Loading history...
65 11
        $this->setTitle($this->schema[$this->name()][Keyword::TITLE] ?? null);
66 7
        $this->setAttributes($this->schema[$this->name()][Keyword::ATTRIBUTES] ?? []);
67 2
    }
68
69 11
    public function name(): string
70
    {
71 11
        return $this->name;
72
    }
73
74 2
    public function title(): string
75
    {
76 2
        return $this->title;
77
    }
78
79 2
    public function attributes(): array
80
    {
81 2
        return $this->attributes;
82
    }
83
84 14
    private function setName(string $name): void
85
    {
86 14
        if (empty(\trim($name))) {
87 3
            throw new InvalidSchemaException('Schema name is required');
88
        }
89
90 11
        $this->name = $name;
91 11
    }
92
93 11
    private function setTitle(?string $title): void
94
    {
95 11
        if (!\is_string($title)) {
96 2
            throw new InvalidSchemaException(\sprintf('Schema %s:title must be a string', $this->name()));
97
        }
98
99 9
        if (empty(\trim($title))) {
100 2
            throw new InvalidSchemaException(\sprintf('Schema %s:title is required', $this->name()));
101
        }
102
103 7
        $this->title = $title;
104 7
    }
105
106 7
    private function setAttributes(array $attributes): void
107
    {
108 7
        if (empty($attributes)) {
109 3
            throw new InvalidSchemaException(\sprintf('Schema %s:attributes are required', $this->name()));
110
        }
111
112 4
        foreach ($attributes as $attribute) {
113 4
            $validation = new SchemaAttributeValidation($attribute);
114 4
            $validation->validate();
115
        }
116
117 2
        $this->attributes = $attributes;
118 2
    }
119
}
120