Completed
Push — master ( d8ac96...d1062b )
by Axel
10:45 queued 05:45
created

MetaData::getShortName()   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 Foundation - 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\Bundle\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\Bundle\CoreBundle\Translation\TranslatorTrait;
21
22
class MetaData implements ArrayAccess
23
{
24
    use TranslatorTrait;
25
26
    public const TYPE_MODULE = 2;
27
28
    public const TYPE_SYSTEM_MODULE = 3;
29
30
    public const TYPE_THEME = 4;
31
32
    public const TYPE_SYSTEM_THEME = 5;
33
34
    public const EXTENSION_TYPE_MODULE = 'zikula-module';
35
36
    public const EXTENSION_TYPE_THEME = 'zikula-theme';
37
38
    public const DEPENDENCY_REQUIRED = 1;
39
40
    public const DEPENDENCY_RECOMMENDED = 2;
41
42
    public const DEPENDENCY_CONFLICTS = 3;
43
44
    private $name;
45
46
    private $version;
47
48
    private $description;
49
50
    private $type;
51
52
    private $dependencies;
53
54
    private $shortName;
55
56
    private $class;
57
58
    private $namespace;
59
60
    private $autoload;
61
62
    private $displayName;
63
64
    private $url;
65
66
    private $oldNames;
67
68
    private $icon;
69
70
    private $capabilities;
71
72
    private $securitySchema;
73
74
    private $extensionType;
75
76
    private $coreCompatibility;
77
78
    public function __construct(array $json = [])
79
    {
80
        $this->name = $json['name'];
81
        $this->version = $json['version'] ?? '';
82
        $this->description = $json['description'] ?? '';
83
        $this->type = $json['type'];
84
        $this->dependencies = $this->formatDependencies($json);
85
        $this->shortName = $json['extra']['zikula']['short-name'];
86
        $this->class = $json['extra']['zikula']['class'];
87
        $this->namespace = mb_substr($this->class, 0, mb_strrpos($this->class, '\\') + 1);
88
        $this->autoload = $json['autoload'];
89
        $this->displayName = $json['extra']['zikula']['displayname'] ?? '';
90
        $this->url = $json['extra']['zikula']['url'] ?? '';
91
        $this->oldNames = $json['extra']['zikula']['oldnames'] ?? [];
92
        $this->icon = $json['extra']['zikula']['icon'] ?? '';
93
        $this->capabilities = $json['extra']['zikula']['capabilities'] ?? [];
94
        $this->securitySchema = $json['extra']['zikula']['securityschema'] ?? [];
95
        $this->extensionType = $json['extensionType'] ?? self::TYPE_MODULE;
96
        $this->coreCompatibility = $json['extra']['zikula']['core-compatibility'] ?? null;
97
    }
98
99
    public function getName(): string
100
    {
101
        return $this->name;
102
    }
103
104
    public function getVersion(): string
105
    {
106
        return $this->version;
107
    }
108
109
    public function getShortName(): string
110
    {
111
        return $this->shortName;
112
    }
113
114
    public function getPsr0(): array
115
    {
116
        return $this->autoload['psr-0'] ?? [];
117
    }
118
119
    public function getPsr4(): array
120
    {
121
        return $this->autoload['psr-4'] ?? [];
122
    }
123
124
    public function getAutoload(): array
125
    {
126
        return $this->autoload;
127
    }
128
129
    public function getClass(): string
130
    {
131
        return $this->class;
132
    }
133
134
    public function getNamespace(): string
135
    {
136
        return $this->namespace;
137
    }
138
139
    public function getType(): string
140
    {
141
        return $this->type;
142
    }
143
144
    public function getDescription(): string
145
    {
146
        $this->confirmTranslator();
147
148
        $description = $this->trans(/** @Ignore */$this->description);
149
150
        return empty($description) ? $this->description : $description;
151
    }
152
153
    public function setDescription(string $description): void
154
    {
155
        $this->description = $description;
156
    }
157
158
    public function getDependencies(): array
159
    {
160
        return $this->dependencies;
161
    }
162
163
    public function getDisplayName(): string
164
    {
165
        $this->confirmTranslator();
166
167
        $displayName = $this->trans(/** @Ignore */$this->displayName);
168
169
        return empty($displayName) ? $this->displayName : $displayName;
170
    }
171
172
    public function setDisplayName(string $name): void
173
    {
174
        $this->displayName = $name;
175
    }
176
177
    public function getUrl(bool $translated = true): string
178
    {
179
        if ($translated) {
180
            $this->confirmTranslator();
181
            $url = $this->trans(/** @Ignore */$this->url);
182
183
            return empty($url) ? $this->url : $url;
184
        }
185
186
        return $this->url;
187
    }
188
189
    public function setUrl(string $url): void
190
    {
191
        $this->url = $url;
192
    }
193
194
    public function getOldNames(): array
195
    {
196
        return $this->oldNames;
197
    }
198
199
    public function getIcon(): string
200
    {
201
        return $this->icon;
202
    }
203
204
    public function setIcon(string $icon): void
205
    {
206
        $this->icon = $icon;
207
    }
208
209
    public function getCapabilities(): array
210
    {
211
        return $this->capabilities;
212
    }
213
214
    public function getSecuritySchema(): array
215
    {
216
        return $this->securitySchema;
217
    }
218
219
    public function getExtensionType(): int
220
    {
221
        return $this->extensionType;
222
    }
223
224
    public function getCoreCompatibility(): ?string
225
    {
226
        return $this->coreCompatibility;
227
    }
228
229
    private function formatDependencies(array $json = []): array
230
    {
231
        $dependencies = [];
232
        if (!empty($json['require'])) {
233
            foreach ($json['require'] as $package => $version) {
234
                $dependencies[] = [
235
                    'modname' => $package,
236
                    'minversion' => $version,
237
                    'maxversion' => $version,
238
                    'status' => self::DEPENDENCY_REQUIRED
239
                ];
240
            }
241
        } else {
242
            $dependencies[] = [
243
                'modname' => 'zikula/core',
244
                'minversion' => '>=1.4.1 <3.0.0',
245
                'maxversion' => '>=1.4.1 <3.0.0',
246
                'status' => self::DEPENDENCY_REQUIRED
247
            ];
248
        }
249
        if (!empty($json['suggest'])) {
250
            foreach ($json['suggest'] as $package => $reason) {
251
                if (mb_strpos($package, ':')) {
252
                    list($name, $version) = explode(':', $package, 2);
253
                } else {
254
                    $name = $package;
255
                    $version = '-1';
256
                }
257
                $dependencies[] = [
258
                    'modname' => $name,
259
                    'minversion' => $version,
260
                    'maxversion' => $version,
261
                    'reason' => $reason,
262
                    'status' => self::DEPENDENCY_RECOMMENDED
263
                ];
264
            }
265
        }
266
267
        return $dependencies;
268
    }
269
270
    private function confirmTranslator(): void
271
    {
272
        if (!isset($this->translator)) {
273
            throw new PreconditionRequiredHttpException(sprintf('The translator property is not set correctly in %s', __CLASS__));
274
        }
275
    }
276
277
    /**
278
     * Module MetaData as array
279
     */
280
    public function getFilteredVersionInfoArray(): array
281
    {
282
        return [
283
            'name' => $this->getShortName(),
284
            'type' => $this->getExtensionType(),
285
            'displayname' => $this->getDisplayName(),
286
            'oldnames' => $this->getOldNames(),
287
            'description' => $this->getDescription(),
288
            'version' => $this->getVersion(),
289
            'url' => $this->getUrl(),
290
            'capabilities' => $this->getCapabilities(),
291
            'securityschema' => $this->getSecuritySchema(),
292
            'dependencies' => $this->getDependencies(),
293
            'coreCompatibility' => $this->getCoreCompatibility()
294
        ];
295
    }
296
297
    public function offsetExists($offset)
298
    {
299
        return isset($this->{$offset});
300
    }
301
302
    public function offsetGet($offset)
303
    {
304
        $method = 'get' . ucwords($offset);
305
306
        return $this->{$method}();
307
    }
308
309
    public function offsetSet($offset, $value): void
310
    {
311
        throw new Exception('Setting values by array access is not allowed.');
312
    }
313
314
    public function offsetUnset($offset): void
315
    {
316
        throw new Exception('Unsetting values by array access is not allowed.');
317
    }
318
}
319