-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJob.php
More file actions
120 lines (100 loc) · 2.83 KB
/
Job.php
File metadata and controls
120 lines (100 loc) · 2.83 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
namespace Mindee\Parsing\V2;
use DateTime;
use Exception;
/**
* Job information for a V2 polling attempt.
*/
class Job
{
/**
* @var string Job ID.
*/
public string $id;
/**
* @var ErrorResponse|null Error response if any.
*/
public ?ErrorResponse $error;
/**
* @var DateTime Date and time of the Job creation.
*/
public DateTime $createdAt;
/**
* @var DateTime|null Date and time of the Job completion. Filled once processing is finished.
*/
public ?DateTime $completedAt;
/**
* @var string ID of the model.
*/
public string $modelId;
/**
* @var string Name for the file.
*/
public string $filename;
/**
* @var string|null Optional alias for the file.
*/
public ?string $alias;
/**
* @var string Status of the job.
*/
public string $status;
/**
* @var string URL to poll for the job status.
*/
public string $pollingUrl;
/**
* @var string|null URL to poll for the job result, redirects to the result if available.
*/
public ?string $resultUrl;
/**
* @var JobWebhook[] ID of webhooks associated with the job.
*/
public array $webhooks;
/**
* @param array $serverResponse Raw server response array.
*/
public function __construct(array $serverResponse)
{
$this->id = $serverResponse['id'];
$this->status = $serverResponse['status'];
$this->error = null;
if (
!empty($serverResponse['error'])
) {
$this->error = new ErrorResponse($serverResponse['error']);
}
$this->createdAt = $this->parseDate($serverResponse['created_at']);
$this->completedAt = isset($serverResponse['completed_at'])
? $this->parseDate($serverResponse['completed_at'])
: null;
$this->modelId = $serverResponse['model_id'];
$this->pollingUrl = $serverResponse['polling_url'];
$this->filename = $serverResponse['filename'];
$this->resultUrl = $serverResponse['result_url'] ?? null;
$this->alias = $serverResponse['alias'];
$this->webhooks = [];
if (array_key_exists("webhooks", $serverResponse)) {
foreach ($serverResponse['webhooks'] as $webhook) {
$this->webhooks[] = new JobWebhook($webhook);
}
}
}
/**
* Parse a date string into a DateTime object.
*
* @param string|null $dateString Date string to parse.
* @return DateTime|null
*/
private function parseDate(?string $dateString): ?DateTime
{
if ($dateString === null || $dateString === '') {
return null;
}
try {
return new DateTime($dateString);
} catch (Exception $e) {
return null;
}
}
}