Lines 100.00% 9 / 9
Methods 100.00% 8 / 8
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 recordChange 100.00% 1 / 1 100.00% 1 / 1 1
 recordDefinition 100.00% 2 / 2 100.00% 1 / 1 1
 recordEntityId 100.00% 1 / 1 100.00% 1 / 1 1
 markOrphaned 100.00% 1 / 1 100.00% 1 / 1 1
 isOrphaned 100.00% 1 / 1 100.00% 1 / 1 1
 isOwnedBy 100.00% 1 / 1 100.00% 1 / 1 1
 buildUpsertChange 100.00% 1 / 1 100.00% 1 / 1 2
13final class LiveExposure
14{
15    public private(set) ?EntityId $entityId = null;
16
17    public function __construct(
18        public private(set) ?WorkerId $owner,
19        public readonly ExposedEntityAddress $address,
20        public private(set) ExposedEntityDefinition $definition,
21        public private(set) ExposedStateChange $latestChange,
22    ) {}
23
24    public function recordChange(ExposedStateChange $change): void
25    {
26        $this->latestChange = $this->latestChange->withLaterChange($change);
27    }
28
29    // The kept state may not fit the new config, so the component keeps or drops its own on the next upsert.
30    public function recordDefinition(ExposedEntityDefinition $definition): void
31    {
32        $this->definition = $definition;
33        $this->latestChange = new ExposedStateChange(attributes: $this->latestChange->attributes, available: $this->latestChange->available);
34    }
35
36    public function recordEntityId(EntityId $entityId): void
37    {
38        $this->entityId = $entityId;
39    }
40
41    public function markOrphaned(): void
42    {
43        $this->owner = null;
44    }
45
46    public function isOrphaned(): bool
47    {
48        return $this->owner === null;
49    }
50
51    public function isOwnedBy(WorkerId $workerId): bool
52    {
53        return $this->owner?->equals($workerId) === true;
54    }
55
56    public function buildUpsertChange(): ExposedStateChange
57    {
58        return $this->isOrphaned() ? $this->latestChange->withLaterChange(new ExposedStateChange(available: false)) : $this->latestChange;
59    }
60}