Lines 100.00% 11 / 11
Functions and Methods 100.00% 8 / 8
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ResourceScope 100.00% 11 / 11 100.00% 8 / 8 11 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 forApp 100.00% 1 / 1 100.00% 1 / 1 1
 shared 100.00% 1 / 1 100.00% 1 / 1 1
 tryFromWireValue 100.00% 4 / 4 100.00% 1 / 1 3
 isShared 100.00% 1 / 1 100.00% 1 / 1 1
 equals 100.00% 1 / 1 100.00% 1 / 1 1
 wireValue 100.00% 1 / 1 100.00% 1 / 1 2
 __toString 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Model;
6
7use Stewart\Contracts\App\AppId;
8use Stringable;
9
10final readonly class ResourceScope implements Stringable
11{
12    private const string SHARED = '@shared';
13
14    private function __construct(public ?AppId $appId) {}
15
16    public static function forApp(AppId $appId): self
17    {
18        return new self($appId);
19    }
20
21    public static function shared(): self
22    {
23        return new self(null);
24    }
25
26    public static function tryFromWireValue(string $value): ?self
27    {
28        if ($value === self::SHARED) {
29            return self::shared();
30        }
31
32        $appId = AppId::tryFromString($value);
33
34        return $appId === null ? null : self::forApp($appId);
35    }
36
37    public function isShared(): bool
38    {
39        return $this->appId === null;
40    }
41
42    public function equals(self $other): bool
43    {
44        return $this->appId?->value === $other->appId?->value;
45    }
46
47    public function wireValue(): string
48    {
49        return $this->appId === null ? self::SHARED : $this->appId->value;
50    }
51
52    public function __toString(): string
53    {
54        return $this->wireValue();
55    }
56}