Lines 100.00% 12 / 12
Functions and Methods 100.00% 8 / 8
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ScopeState 100.00% 12 / 12 100.00% 8 / 8 10 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 activate 100.00% 3 / 3 100.00% 1 / 1 2
 pause 100.00% 3 / 3 100.00% 1 / 1 2
 resume 100.00% 1 / 1 100.00% 1 / 1 1
 close 100.00% 1 / 1 100.00% 1 / 1 1
 isLive 100.00% 1 / 1 100.00% 1 / 1 1
 isPaused 100.00% 1 / 1 100.00% 1 / 1 1
 isClosed 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Scope;
6
7use LogicException;
8use Stewart\Runtime\Model\ResourceScope;
9
10// Shared by every queue and schedule of one scope, so none of them keeps its own copy of these flags.
11final class ScopeState
12{
13    private bool $paused = false;
14
15    public function __construct(
16        public readonly ResourceScope $scope,
17        private ScopePhase $phase = ScopePhase::Starting,
18    ) {}
19
20    public function activate(): void
21    {
22        if ($this->phase === ScopePhase::Closed) {
23            throw new LogicException(\sprintf('Scope "%s" was released and cannot be activated again.', $this->scope));
24        }
25
26        $this->phase = ScopePhase::Live;
27    }
28
29    public function pause(): void
30    {
31        if ($this->scope->isShared()) {
32            throw new LogicException('The shared scope cannot be paused.');
33        }
34
35        $this->paused = true;
36    }
37
38    public function resume(): void
39    {
40        $this->paused = false;
41    }
42
43    public function close(): void
44    {
45        $this->phase = ScopePhase::Closed;
46    }
47
48    public function isLive(): bool
49    {
50        return $this->phase === ScopePhase::Live;
51    }
52
53    public function isPaused(): bool
54    {
55        return $this->paused;
56    }
57
58    public function isClosed(): bool
59    {
60        return $this->phase === ScopePhase::Closed;
61    }
62}