1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | /* |
||
5 | * The MIT License |
||
6 | * |
||
7 | * Copyright 2016 BCL Technologies. |
||
8 | * |
||
9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
||
10 | * of this software and associated documentation files (the "Software"), to deal |
||
11 | * in the Software without restriction, including without limitation the rights |
||
12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||
13 | * copies of the Software, and to permit persons to whom the Software is |
||
14 | * furnished to do so, subject to the following conditions: |
||
15 | * |
||
16 | * The above copyright notice and this permission notice shall be included in |
||
17 | * all copies or substantial portions of the Software. |
||
18 | * |
||
19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||
20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||
21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||
22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||
23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||
24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
||
25 | * THE SOFTWARE. |
||
26 | */ |
||
27 | |||
28 | namespace Bcl\EasyPdfCloud; |
||
29 | |||
30 | use Exception; |
||
31 | |||
32 | class Job |
||
33 | { |
||
34 | /** |
||
35 | * @var RestApi |
||
36 | */ |
||
37 | private $restApi; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $jobId; |
||
43 | |||
44 | /** |
||
45 | * Job constructor. |
||
46 | * |
||
47 | * @param RestApi $restApi |
||
48 | * @param string $jobId |
||
49 | */ |
||
50 | public function __construct(RestApi $restApi, string $jobId) |
||
51 | { |
||
52 | $this->restApi = $restApi; |
||
53 | $this->jobId = $jobId; |
||
54 | } |
||
55 | |||
56 | public function __destruct() |
||
57 | { |
||
58 | $restApi = $this->restApi; |
||
59 | $jobId = $this->jobId; |
||
60 | |||
61 | $this->restApi = null; |
||
62 | $this->jobId = null; |
||
63 | |||
64 | if (null !== $restApi && null !== $jobId) { |
||
65 | try { |
||
66 | $restApi->deleteJob($jobId); |
||
67 | } catch (Exception $e) { |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
![]() |
|||
68 | } |
||
69 | } |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @return string |
||
74 | */ |
||
75 | public function getJobId(): string |
||
76 | { |
||
77 | return $this->jobId; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @return JobExecutionResult |
||
82 | * |
||
83 | * @throws JobExecutionException |
||
84 | */ |
||
85 | public function waitForJobExecutionCompletion(): JobExecutionResult |
||
86 | { |
||
87 | $restApi = $this->restApi; |
||
88 | $jobId = $this->jobId; |
||
89 | |||
90 | $jobInfo = null; |
||
91 | |||
92 | while (true) { |
||
93 | $jobInfo = $restApi->waitForJobEvent($jobId); |
||
94 | if ($jobInfo->getFinished()) { |
||
95 | break; |
||
96 | } |
||
97 | } |
||
98 | |||
99 | $status = $jobInfo->getStatus(); |
||
100 | if (JobInfo::STATUS_COMPLETED !== $status) { |
||
101 | throw new JobExecutionException($jobInfo); |
||
102 | } |
||
103 | |||
104 | $fileData = $restApi->downloadOutput($jobId); |
||
105 | |||
106 | return new JobExecutionResult($jobInfo, $fileData); |
||
107 | } |
||
108 | |||
109 | public function cancelJobExecution(): void |
||
110 | { |
||
111 | $restApi = $this->restApi; |
||
112 | $jobId = $this->jobId; |
||
113 | |||
114 | $restApi->stopJob($jobId); |
||
115 | } |
||
116 | } |
||
117 |