File size: 9,819 Bytes
cf14c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php

declare(strict_types=1);

namespace Plugin\Domain;

use ArrayIterator;
use DateTime;
use JsonException;
use DateTimeInterface;
use Iterator;
use Plugin\Domain\Exceptions\InvalidPluginComposerJsonFileException;
use Plugin\Domain\Exceptions\PluginComposerFileNotFoundException;
use Plugin\Domain\ValueObjects\Author;
use Plugin\Domain\ValueObjects\Tagline;
use Plugin\Domain\ValueObjects\EntryClass;
use Plugin\Domain\ValueObjects\License;
use Plugin\Domain\ValueObjects\Name;
use Plugin\Domain\ValueObjects\Description;
use Plugin\Domain\ValueObjects\Status;
use Plugin\Domain\ValueObjects\SupportChannel;
use Plugin\Domain\ValueObjects\SupportChannelType;
use Plugin\Domain\ValueObjects\Title;
use Plugin\Domain\ValueObjects\Type;
use Plugin\Domain\ValueObjects\Url;
use Plugin\Domain\ValueObjects\Version;

class Context
{
    private ?string $composerFilePath = null;
    private object $composerJson;

    public readonly Type $type;
    public readonly Name $name;
    private Status $status;
    public readonly Description $description;
    public readonly Version $version;
    public readonly Url $homepage;
    public readonly ?DateTimeInterface $releasedAt;
    public readonly Tagline $tagline;
    public readonly Title $title;
    public readonly Url $logo;
    public readonly Url $icon;
    public readonly EntryClass $entryClass;

    /** @var SupportChannel[] $supportChannels */
    public readonly array $supportChannels;

    /** @var License[] $licenses */
    public readonly array $licenses;

    /** @var Author[] $authors */
    public readonly array $authors;

    /**
     * @param string $pathOrContent
     * @return void
     * @throws PluginComposerFileNotFoundException
     * @throws InvalidPluginComposerJsonFileException
     */
    public function __construct(string $pathOrContent)
    {
        if (is_dir($pathOrContent)) {
            $pathOrContent = $pathOrContent . '/composer.json';
        }

        if (is_file($pathOrContent)) {
            $content = file_get_contents($pathOrContent);
            $this->composerFilePath = $pathOrContent;
        } else {
            $content = $pathOrContent;
        }

        if ($content === false) {
            throw new PluginComposerFileNotFoundException(
                "Couldn't found composer.json file at " . $pathOrContent
            );
        }

        $json = $this->validate($content);
        $this->populate($json);
        $this->composerJson = $json;
    }

    public function getStatus(): Status
    {
        return $this->status;
    }

    public function setStatus(Status $status): Context
    {
        if ($status != $this->status && $this->composerFilePath !== null) {
            $this->composerJson->extra->status = strtolower($status->value);

            file_put_contents(
                $this->composerFilePath,
                json_encode(
                    $this->composerJson,
                    JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
                )
            );
        }

        $this->status = $status;
        return $this;
    }

    /** @return Iterator<SupportChannel>  */
    public function getSupportChannels(): Iterator
    {
        return new ArrayIterator($this->supportChannels);
    }

    /**  @return Iterator<License> */
    public function getLicenses(): Iterator
    {
        return new ArrayIterator($this->licenses);
    }

    /**  @return Iterator<Author> */
    public function getAuthors(): Iterator
    {
        return new ArrayIterator($this->authors);
    }

    private function validate(string $jsonFileContent): object
    {
        try {
            $json = json_decode(
                $jsonFileContent,
                null,
                512,
                JSON_THROW_ON_ERROR
            );
        } catch (JsonException $e) {
            throw new InvalidPluginComposerJsonFileException(
                message: "Failed to decode composer.json file with following error: " . $e->getMessage(),
                previous: $e
            );
        }

        if (!isset($json->type) || !in_array($json->type, ['aikeedo-plugin', 'aikeedo-theme'])) {
            throw new InvalidPluginComposerJsonFileException(
                "Invalid composer.json file: "
                    . "<type> is not set or is neither of aikeedo-plugin or aikeedo-theme."
            );
        }

        if (!isset($json->name)) {
            throw new InvalidPluginComposerJsonFileException(
                "Invalid composer.json file: <name> is not set."
            );
        }

        if (
            $json->type == 'aikeedo-plugin'
            && (!isset($json->extra->{'entry-class'}) || !$json->extra->{'entry-class'})
        ) {
            throw new InvalidPluginComposerJsonFileException(
                "Invalid composer.json file: <extra.entry-class> is not set."
            );
        }

        if (!isset($json->require->{"heyaikeedo/composer"})) {
            throw new InvalidPluginComposerJsonFileException(
                "Invalid composer.json file: "
                    . " All plugins/themes must require <heyaikeedo/composer> library."
            );
        }

        return $json;
    }

    private function populate(object $json): void
    {
        // @phpstan-ignore-next-line
        $this->type = $json->type === 'aikeedo-theme'
            ? Type::THEME : Type::PLUGIN;

        // @phpstan-ignore-next-line
        $this->name = new Name($json->name);

        $this->status = isset($json->extra->status)
            ? Status::from($json->extra->status)
            : Status::INACTIVE;

        // @phpstan-ignore-next-line
        $this->description = new Description(
            $json->description ?? null
        );

        // @phpstan-ignore-next-line
        $this->version = new Version($json->version ?? null);

        // @phpstan-ignore-next-line
        $this->homepage = new Url($json->homepage ?? null);

        // @phpstan-ignore-next-line
        $this->releasedAt = isset($json->time) && $json->time ? new DateTime($json->time) : null;

        // @phpstan-ignore-next-line
        $this->tagline = new Tagline($json->extra->tagline ?? null);

        // @phpstan-ignore-next-line
        $this->title = new Title($json->extra->title ?? null);

        // @phpstan-ignore-next-line
        $this->icon = new Url($json->extra->icon ?? null);

        // @phpstan-ignore-next-line
        $this->logo = new Url($json->extra->logo ?? null);

        // @phpstan-ignore-next-line
        $this->entryClass = new EntryClass($json->extra->{'entry-class'} ?? null);

        // Populate support channels
        $supportChannels = [];
        if (isset($json->support)) {
            if (isset($json->support->chat)) {
                $supportChannels[] = new SupportChannel(
                    SupportChannelType::CHAT,
                    $json->support->chat
                );
            }

            if (isset($json->support->docs)) {
                $supportChannels[] = new SupportChannel(
                    SupportChannelType::DOCS,
                    $json->support->docs
                );
            }

            if (isset($json->support->email)) {
                $supportChannels[] = new SupportChannel(
                    SupportChannelType::EMAIL,
                    $json->support->email
                );
            }

            if (isset($json->support->forum)) {
                $supportChannels[] = new SupportChannel(
                    SupportChannelType::FORUM,
                    $json->support->forum
                );
            }

            if (isset($json->support->irc)) {
                $supportChannels[] = new SupportChannel(
                    SupportChannelType::IRC,
                    $json->support->irc
                );
            }

            if (isset($json->support->issues)) {
                $supportChannels[] = new SupportChannel(
                    SupportChannelType::ISSUES,
                    $json->support->issues
                );
            }

            if (isset($json->support->rss)) {
                $supportChannels[] = new SupportChannel(
                    SupportChannelType::RSS,
                    $json->support->rss
                );
            }

            if (isset($json->support->source)) {
                $supportChannels[] = new SupportChannel(
                    SupportChannelType::SOURCE,
                    $json->support->source
                );
            }

            if (isset($json->support->wiki)) {
                $supportChannels[] = new SupportChannel(
                    SupportChannelType::WIKI,
                    $json->support->wiki
                );
            }
        }

        // @phpstan-ignore-next-line
        $this->supportChannels = $supportChannels;

        // Populate licenses
        $licenses = [];
        if (isset($json->license)) {
            /** @var string|string[] $licenses */
            $licenses = $json->license;

            if (!is_array($licenses)) {
                $licenses = [$licenses];
            }

            $licenses = array_map(
                fn ($license) => new License($license),
                $licenses
            );
        }

        // @phpstan-ignore-next-line
        $this->licenses = $licenses;

        // Populate authors
        $authors = [];
        if (isset($json->authors)) {
            $authors = array_map(
                fn ($author) => new Author(
                    $author->name,
                    $author->email ?? null,
                    $author->homepage ?? null,
                    $author->role ?? null
                ),
                $json->authors
            );
        }

        // @phpstan-ignore-next-line
        $this->authors = $authors;
    }
}