Lines 96.00% 24 / 25
Functions and Methods 83.33% 5 / 6
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
AppResources 96.00% 24 / 25 83.33% 5 / 6 9 0.00% 0 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 activateScope 100.00% 5 / 5 100.00% 1 / 1 2
 pauseScope 100.00% 4 / 4 100.00% 1 / 1 2
 resumeScope 80.00% 4 / 5 0.00% 0 / 1 2.03
 releaseScope 100.00% 5 / 5 100.00% 1 / 1 1
 releaseAll 100.00% 5 / 5 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Worker;
6
7use Stewart\Runtime\Dispatch\LocalDispatcher;
8use Stewart\Runtime\Model\ResourceScope;
9use Stewart\Runtime\Schedule\EntityTimeScheduler;
10use Stewart\Runtime\Schedule\ScheduleRegistry;
11use Stewart\Runtime\Scope\ScopeLifecycle;
12use Stewart\Runtime\Worker\Exposure\ExposedHandleRegistry;
13
14final readonly class AppResources
15{
16    public function __construct(
17        private ScopeLifecycle $scopes,
18        private LocalDispatcher $dispatcher,
19        private ScheduleRegistry $schedules,
20        private EntityTimeScheduler $entityTimes,
21        private ExposedHandleRegistry $exposedHandles,
22    ) {}
23
24    public function activateScope(ResourceScope $scope): void
25    {
26        $this->scopes->activateScope($scope);
27
28        if (!$this->scopes->findOrCreateStateOf($scope)->isLive()) {
29            return;
30        }
31
32        $this->dispatcher->startDrainingQueuesOf($scope);
33        $this->schedules->startEntriesOf($scope);
34    }
35
36    public function pauseScope(ResourceScope $scope): void
37    {
38        if ($this->scopes->findOrCreateStateOf($scope)->isClosed()) {
39            return;
40        }
41
42        $this->scopes->pauseScope($scope);
43        $this->dispatcher->discardPendingWorkOf($scope);
44    }
45
46    public function resumeScope(ResourceScope $scope): void
47    {
48        if ($this->scopes->findOrCreateStateOf($scope)->isClosed()) {
49            return;
50        }
51
52        $this->scopes->resumeScope($scope);
53        $this->entityTimes->refreshTasksOf($scope);
54        $this->schedules->runHeldOccurrencesOf($scope);
55    }
56
57    public function releaseScope(ResourceScope $scope): void
58    {
59        $this->scopes->releaseScope($scope);
60        $this->entityTimes->cancelTasksOf($scope);
61        $this->dispatcher->cancelSubscriptionsOf($scope);
62        $this->schedules->cancelEntriesOf($scope);
63        $this->exposedHandles->releaseHandlesOf($scope);
64    }
65
66    public function releaseAll(): void
67    {
68        $this->scopes->stopAll();
69        $this->entityTimes->cancelAll();
70        $this->dispatcher->cancelAll();
71        $this->schedules->cancelAll();
72        $this->exposedHandles->releaseAll();
73    }
74}