Lines 83.33% 15 / 18
Functions and Methods 57.14% 4 / 7
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
EntityAliasesChange 83.33% 15 / 18 57.14% 4 / 7 10.46 0.00% 0 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 replacingWith 100.00% 1 / 1 100.00% 1 / 1 1
 withAdded 83.33% 5 / 6 0.00% 0 / 1 2.02
 withRemoved 83.33% 5 / 6 0.00% 0 / 1 2.02
 needsCurrentAliases 0.00% 0 / 1 0.00% 0 / 1 2
 resolveAgainst 100.00% 1 / 1 100.00% 1 / 1 1
 applyTo 100.00% 2 / 2 100.00% 1 / 1 2
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Contracts\Registry\Update;
6
7use Stewart\Contracts\Registry\Collection\EntityAliasCollection;
8use Stewart\Contracts\Registry\EntityAlias;
9use Stewart\Contracts\Wire\ListOf;
10
11final readonly class EntityAliasesChange
12{
13    /**
14     * @param list<EntityAlias>|null $replacement
15     * @param list<EntityAlias> $added
16     * @param list<EntityAlias> $removed
17     */
18    public function __construct(
19        #[ListOf(EntityAlias::class)]
20        public ?array $replacement = null,
21        #[ListOf(EntityAlias::class)]
22        public array $added = [],
23        #[ListOf(EntityAlias::class)]
24        public array $removed = [],
25    ) {}
26
27    public static function replacingWith(EntityAliasCollection $aliases): self
28    {
29        return new self($aliases->listValues());
30    }
31
32    public function withAdded(EntityAliasCollection $aliases): self
33    {
34        if ($this->replacement !== null) {
35            return self::replacingWith(EntityAliasCollection::fromList($this->replacement)->withAddedMembers($aliases));
36        }
37
38        return new self(
39            added: EntityAliasCollection::fromList($this->added)->withAddedMembers($aliases)->listValues(),
40            removed: EntityAliasCollection::fromList($this->removed)->withoutMembers($aliases)->listValues(),
41        );
42    }
43
44    public function withRemoved(EntityAliasCollection $aliases): self
45    {
46        if ($this->replacement !== null) {
47            return self::replacingWith(EntityAliasCollection::fromList($this->replacement)->withoutMembers($aliases));
48        }
49
50        return new self(
51            added: EntityAliasCollection::fromList($this->added)->withoutMembers($aliases)->listValues(),
52            removed: EntityAliasCollection::fromList($this->removed)->withAddedMembers($aliases)->listValues(),
53        );
54    }
55
56    public function needsCurrentAliases(): bool
57    {
58        return $this->replacement === null;
59    }
60
61    public function resolveAgainst(EntityAliasCollection $current): self
62    {
63        return self::replacingWith($this->applyTo($current));
64    }
65
66    public function applyTo(EntityAliasCollection $current): EntityAliasCollection
67    {
68        $base = $this->replacement === null ? $current : EntityAliasCollection::fromList($this->replacement);
69
70        return $base->withAddedMembers(EntityAliasCollection::fromList($this->added))->withoutMembers(EntityAliasCollection::fromList($this->removed));
71    }
72}