Passed
Push — master ( 2d6ed9...31b4e9 )
by PHPinnacle
05:04
created

Token::guard()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
nc 2
nop 0
dl 0
loc 4
c 0
b 0
f 0
cc 2
ccs 3
cts 3
cp 1
crap 2
rs 10
1
<?php
2
/**
3
 * This file is part of PHPinnacle/Ensign.
4
 *
5
 * (c) PHPinnacle Team <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types = 1);
12
13
namespace PHPinnacle\Ensign;
14
15
use Amp\CancellationTokenSource;
16
17
final class Token
18
{
19
    /**
20
     * @var CancellationTokenSource
21
     */
22
    private $source;
23
24
    /**
25
     * @var \Amp\CancellationToken
26
     */
27
    private $token;
28
29
    /**
30
     * @var int
31
     */
32
    private $task;
33
34
    /**
35
     * @var string
36
     */
37
    private $reason;
38
39
    /**
40
     * TaskToken constructor.
41
     */
42 9
    public function __construct()
43
    {
44 9
        $this->source = new CancellationTokenSource();
45 9
        $this->token  = $this->source->getToken();
46 9
    }
47
48
    /**
49
     * @param int    $task
50
     * @param string $reason
51
     * @return void
52
     */
53 1
    public function cancel(int $task, string $reason): void
54
    {
55 1
        $this->task   = $task;
56 1
        $this->reason = $reason;
57
58 1
        $this->source->cancel();
59 1
    }
60
61
    /**
62
     * @return void
63
     */
64 9
    public function guard(): void
65
    {
66 9
        if ($this->token->isRequested()) {
67 1
            throw new Exception\TaskCanceled($this->task, $this->reason);
68
        };
69 8
    }
70
}
71