Lines 80.00% 4 / 5
Functions and Methods 75.00% 3 / 4
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ComponentTracker 80.00% 4 / 5 75.00% 3 / 4 5.20 0.00% 0 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 recordState 100.00% 2 / 2 100.00% 1 / 1 2
 recordStateKeepingVersion 100.00% 1 / 1 100.00% 1 / 1 1
 isActive 0.00% 0 / 1 0.00% 0 / 1 2
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Broker\Component;
6
7use Stewart\Client\Component\ComponentVersion;
8use Stewart\Contracts\Time\Clock;
9use Stewart\Runtime\Lifecycle\ComponentState;
10
11final class ComponentTracker
12{
13    public private(set) ComponentDetection $detection;
14
15    public function __construct(private readonly Clock $clock)
16    {
17        $this->detection = new ComponentDetection(ComponentState::Unchecked, null, $clock->getNow());
18    }
19
20    public function recordState(ComponentState $state, ?ComponentVersion $version): void
21    {
22        $since = $state === $this->detection->state ? $this->detection->since : $this->clock->getNow();
23        $this->detection = new ComponentDetection($state, $version, $since);
24    }
25
26    public function recordStateKeepingVersion(ComponentState $state): void
27    {
28        $this->recordState($state, $this->detection->version);
29    }
30
31    public function isActive(): bool
32    {
33        return $this->detection->state === ComponentState::Active;
34    }
35}