-
Notifications
You must be signed in to change notification settings - Fork 1
/
TaskPromise.php
70 lines (63 loc) · 1.59 KB
/
TaskPromise.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace Aternos\Taskmaster\Communication\Promise;
use Aternos\Taskmaster\Task\TaskInterface;
use Exception;
use Throwable;
/**
* Class TaskPromise
*
* Promise implementation for tasks, resolves with the task result or
* rejects with the task error. Success and exception handlers are called
* with the task as second argument.
*
* @package Aternos\Taskmaster\Communication\Promise
*/
class TaskPromise extends Promise
{
/**
* @param TaskInterface $task
* @param ResponsePromise|null $responsePromise
* @throws Throwable
*/
public function __construct(protected TaskInterface $task, ?ResponsePromise $responsePromise = null)
{
if ($responsePromise) {
$this->setResponsePromise($responsePromise);
}
}
/**
* @param ResponsePromise $responsePromise
* @return $this
* @throws Throwable
*/
public function setResponsePromise(ResponsePromise $responsePromise): static
{
$responsePromise->then(function ($response) {
$this->resolve($response->getData());
})->catch(function (Exception $exception) {
$this->reject($exception);
});
return $this;
}
/**
* @return TaskInterface
*/
public function getTask(): TaskInterface
{
return $this->task;
}
/**
* @inheritDoc
*/
protected function getAdditionalRejectArguments(): array
{
return [$this->task];
}
/**
* @inheritDoc
*/
protected function getAdditionalResolveArguments(): array
{
return [$this->task];
}
}