Passed
Push — main ( b98937...16d287 )
by Axel
04:40
created

MetaData::getClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\CoreBundle\Composer;
15
16
use ArrayAccess;
17
use Exception;
18
use Symfony\Component\HttpKernel\Exception\PreconditionRequiredHttpException;
19
use Translation\Extractor\Annotation\Ignore;
20
use Zikula\CoreBundle\Translation\TranslatorTrait;
21
use function Symfony\Component\String\s;
22
23
class MetaData implements ArrayAccess
24
{
25
    use TranslatorTrait;
26
27
    private string $name;
28
29
    private string $version;
30
31
    private string $description;
32
33
    private string $type;
34
35
    private string $class;
36
37
    private string $namespace;
38
39
    private array $autoload;
40
41
    private string $displayName;
42
43
    private string $url;
44
45
    private string $icon;
46
47
    private array $capabilities;
48
49
    public function __construct(array $json = [])
50
    {
51
        $this->name = $json['name'];
52
        $this->version = $json['version'] ?? '';
53
        $this->description = $json['description'] ?? '';
54
        $this->type = $json['type'];
55
        $this->class = $json['extra']['zikula']['class'];
56
        $this->namespace = s($this->class)->beforeLast('\\', true)->toString();
57
        $this->autoload = $json['autoload'];
58
        $this->displayName = $json['extra']['zikula']['displayname'] ?? '';
59
        $this->url = $json['extra']['zikula']['url'] ?? '';
60
        $this->icon = $json['extra']['zikula']['icon'] ?? '';
61
        $this->capabilities = $json['extra']['zikula']['capabilities'] ?? [];
62
    }
63
64
    public function getName(): string
65
    {
66
        return $this->name;
67
    }
68
69
    public function getVersion(): string
70
    {
71
        return $this->version;
72
    }
73
74
    public function getPsr0(): array
75
    {
76
        return $this->autoload['psr-0'] ?? [];
77
    }
78
79
    public function getPsr4(): array
80
    {
81
        return $this->autoload['psr-4'] ?? [];
82
    }
83
84
    public function getAutoload(): array
85
    {
86
        return $this->autoload;
87
    }
88
89
    public function getClass(): string
90
    {
91
        return $this->class;
92
    }
93
94
    public function getNamespace(): string
95
    {
96
        return $this->namespace;
97
    }
98
99
    public function getType(): string
100
    {
101
        return $this->type;
102
    }
103
104
    public function getDescription(): string
105
    {
106
        $this->confirmTranslator();
107
108
        $description = $this->trans(/** @Ignore */ $this->description);
109
110
        return empty($description) ? $this->description : $description;
111
    }
112
113
    public function setDescription(string $description): void
114
    {
115
        $this->description = $description;
116
    }
117
118
    public function getDisplayName(): string
119
    {
120
        $this->confirmTranslator();
121
122
        $displayName = $this->trans(/** @Ignore */ $this->displayName);
123
124
        return empty($displayName) ? $this->displayName : $displayName;
125
    }
126
127
    public function setDisplayName(string $name): void
128
    {
129
        $this->displayName = $name;
130
    }
131
132
    public function getUrl(bool $translated = true): string
133
    {
134
        if ($translated) {
135
            $this->confirmTranslator();
136
            $url = $this->trans(/** @Ignore */ $this->url);
137
138
            return empty($url) ? $this->url : $url;
139
        }
140
141
        return $this->url;
142
    }
143
144
    public function setUrl(string $url): void
145
    {
146
        $this->url = $url;
147
    }
148
149
    public function getIcon(): string
150
    {
151
        return $this->icon;
152
    }
153
154
    public function setIcon(string $icon): void
155
    {
156
        $this->icon = $icon;
157
    }
158
159
    public function getCapabilities(): array
160
    {
161
        return $this->capabilities;
162
    }
163
164
    private function confirmTranslator(): void
165
    {
166
        if (!isset($this->translator)) {
167
            throw new PreconditionRequiredHttpException(sprintf('The translator property is not set correctly in %s', __CLASS__));
168
        }
169
    }
170
171
    public function offsetExists(mixed $offset): bool
172
    {
173
        return isset($this->{$offset});
174
    }
175
176
    public function offsetGet(mixed $offset): mixed
177
    {
178
        $method = 'get' . ucwords($offset);
179
180
        return $this->{$method}();
181
    }
182
183
    public function offsetSet(mixed $offset, mixed $value): void
184
    {
185
        throw new Exception('Setting values by array access is not allowed.');
186
    }
187
188
    public function offsetUnset(mixed $offset): void
189
    {
190
        throw new Exception('Unsetting values by array access is not allowed.');
191
    }
192
}
193