Lines 100.00% 3 / 3
Functions and Methods 100.00% 2 / 2
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
StewartIdentity 100.00% 3 / 3 100.00% 2 / 2 5 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 wasCausedByStewart 100.00% 2 / 2 100.00% 1 / 1 4
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Support\State;
6
7use Stewart\Contracts\Event\HaEvent;
8use Stewart\Contracts\State\EntityState;
9use Stewart\Contracts\State\StateChange;
10
11// Every call and fired event carries the token's user id, so it is known before callService() or fireEvent() returns.
12/** @internal */
13final readonly class StewartIdentity
14{
15    public function __construct(public ?string $haUserId) {}
16
17    public function wasCausedByStewart(StateChange|EntityState|HaEvent $subject): bool
18    {
19        $context = $subject instanceof StateChange ? $subject->findCausingContext() : $subject->context;
20
21        return $this->haUserId !== null && $this->haUserId !== '' && $context?->userId === $this->haUserId;
22    }
23}