1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anticaptcha\Response; |
4
|
|
|
|
5
|
|
|
use Anticaptcha\Client; |
6
|
|
|
use BadMethodCallException; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use JsonException; |
9
|
|
|
use Psr\Http\Client\ClientExceptionInterface; |
10
|
|
|
use RuntimeException; |
11
|
|
|
|
12
|
|
|
final class GetTaskResultResponse extends AbstractResponse |
13
|
|
|
{ |
14
|
|
|
public const TASK_STATUS_READY = 'ready'; |
15
|
|
|
public const TASK_STATUS_PROCESSING = 'processing'; |
16
|
|
|
|
17
|
|
|
public ?string $status = null; |
18
|
|
|
public ?array $solution = null; |
19
|
|
|
public ?string $cost = null; |
20
|
|
|
public ?string $ip = null; |
21
|
|
|
public ?int $createTime = null; |
22
|
|
|
public ?int $endTime = null; |
23
|
|
|
public ?int $solveCount = null; |
24
|
|
|
|
25
|
|
|
private ?int $taskId = null; |
26
|
|
|
private ?Client $client = null; |
27
|
|
|
|
28
|
|
|
public function isReady(): bool |
29
|
|
|
{ |
30
|
|
|
return $this->status === self::TASK_STATUS_READY; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function setTaskId(int $taskId): void |
34
|
|
|
{ |
35
|
|
|
$this->taskId = $taskId; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getTaskId(): ?int |
39
|
|
|
{ |
40
|
|
|
return $this->taskId; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function setClient(Client $client): void |
44
|
|
|
{ |
45
|
|
|
$this->client = $client; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getSolution(): ?array |
49
|
|
|
{ |
50
|
|
|
return $this->solution; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param int $pollingInterval |
55
|
|
|
* @param int $timeout |
56
|
|
|
* @return void |
57
|
|
|
* |
58
|
|
|
* @throws JsonException |
59
|
|
|
* @throws ClientExceptionInterface |
60
|
|
|
*/ |
61
|
|
|
public function wait(int $pollingInterval = 5, int $timeout = 60): void |
62
|
|
|
{ |
63
|
|
|
if ( |
64
|
|
|
$pollingInterval < 1 |
65
|
|
|
|| $timeout < 1 |
66
|
|
|
) { |
67
|
|
|
throw new InvalidArgumentException('Timeout must be positive integer.'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ( |
71
|
|
|
$this->client === null |
72
|
|
|
|| $this->taskId === null |
73
|
|
|
) { |
74
|
|
|
throw new BadMethodCallException('Can\'t wait due to inconsistency.'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$startTime = time(); |
78
|
|
|
$result = $this->client->getTaskResult($this->taskId); |
79
|
|
|
|
80
|
|
|
while (!$result->isReady()) { |
81
|
|
|
sleep($pollingInterval); |
82
|
|
|
if (time() - $startTime >= $timeout) { |
83
|
|
|
throw new RuntimeException('Waiting for ready timeout.'); |
84
|
|
|
} |
85
|
|
|
$result = $this->client->getTaskResult($this->taskId); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Self update |
89
|
|
|
$this->status = $result->status; |
90
|
|
|
$this->solution = $result->solution; |
91
|
|
|
$this->cost = $result->cost; |
92
|
|
|
$this->ip = $result->ip; |
93
|
|
|
$this->createTime = $result->createTime; |
94
|
|
|
$this->endTime = $result->endTime; |
95
|
|
|
$this->solveCount = $result->solveCount; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|