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 | use InvalidArgumentException; |
||
32 | use function count; |
||
33 | use function basename; |
||
34 | use function pathinfo; |
||
35 | |||
36 | class Client |
||
37 | { |
||
38 | /** |
||
39 | * @var RestApi |
||
40 | */ |
||
41 | private $restApi; |
||
42 | |||
43 | /** |
||
44 | * Client constructor. |
||
45 | * |
||
46 | * @param string $clientId |
||
47 | * @param string $clientSecret |
||
48 | * @param IOAuth2TokenManager|null $tokenManager |
||
49 | * @param UrlInfo|null $urlInfo |
||
50 | */ |
||
51 | public function __construct(string $clientId, string $clientSecret, IOAuth2TokenManager $tokenManager = null, UrlInfo $urlInfo = null) |
||
52 | { |
||
53 | $this->restApi = new RestApi($clientId, $clientSecret, $tokenManager, $urlInfo); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Starts new job from file located in given path |
||
58 | * |
||
59 | * @param string $workflowId |
||
60 | * @param string $filePath |
||
61 | * @param bool $enableTestMode |
||
62 | * |
||
63 | * @return Job |
||
64 | */ |
||
65 | public function startNewJobWithFilePath(string $workflowId, string $filePath, bool $enableTestMode = false): Job |
||
66 | { |
||
67 | $restApi = $this->restApi; |
||
68 | |||
69 | $jobId = $restApi->createNewJobWithFilePath($workflowId, $filePath, true, $enableTestMode); |
||
70 | |||
71 | return new Job($restApi, $jobId); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Starts new job from file located in given path |
||
76 | * also specifies name for result file |
||
77 | * |
||
78 | * @param string $workflowId |
||
79 | * @param string $filePath |
||
80 | * @param string $fileName |
||
81 | * @param bool $enableTestMode |
||
82 | * |
||
83 | * @return Job |
||
84 | */ |
||
85 | public function startNewJobWithFilePathAndName(string $workflowId, string $filePath, string $fileName, bool $enableTestMode = false): Job |
||
86 | { |
||
87 | $restApi = $this->restApi; |
||
88 | |||
89 | $jobId = $restApi->createNewJobWithFilePathAndName($workflowId, $filePath, $fileName, true, $enableTestMode); |
||
90 | |||
91 | return new Job($restApi, $jobId); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Starts new job with given contents |
||
96 | * and custom result file name |
||
97 | * |
||
98 | * @param string $workflowId |
||
99 | * @param string $fileContents |
||
100 | * @param string $fileName |
||
101 | * @param bool $enableTestMode |
||
102 | * |
||
103 | * @return Job |
||
104 | */ |
||
105 | public function startNewJobWithFileContents(string $workflowId, string $fileContents, string $fileName, bool $enableTestMode = false): Job |
||
106 | { |
||
107 | $restApi = $this->restApi; |
||
108 | |||
109 | $jobId = $restApi->createNewJobWithFileContents($workflowId, $fileContents, $fileName, true, $enableTestMode); |
||
110 | |||
111 | return new Job($restApi, $jobId); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Starts new job that merges multiple file |
||
116 | * into result file |
||
117 | * |
||
118 | * @param string $workflowId |
||
119 | * @param array $filePaths |
||
120 | * @param bool $enableTestMode |
||
121 | * @return Job |
||
122 | * |
||
123 | * @throws Exception |
||
124 | */ |
||
125 | public function startNewJobForMergeTask(string $workflowId, array $filePaths, bool $enableTestMode = false): Job |
||
126 | { |
||
127 | $restApi = $this->restApi; |
||
128 | |||
129 | $filesCount = count($filePaths); |
||
130 | |||
131 | if (0 === $filesCount) { |
||
132 | throw new InvalidArgumentException('No input files specified'); |
||
133 | } |
||
134 | |||
135 | $filePath = $filePaths[0]; |
||
136 | |||
137 | if (1 === $filesCount) { |
||
138 | return $this->startNewJobWithFilePath($workflowId, $filePath); |
||
139 | } |
||
140 | |||
141 | $fileName = basename($filePath); |
||
142 | |||
143 | $fileNameMap = array(); |
||
144 | $fileNameMap[$fileName] = true; |
||
145 | |||
146 | $jobId = $restApi->createNewJobWithFilePathAndName($workflowId, $filePath, $fileName, false, $enableTestMode); |
||
147 | |||
148 | try { |
||
149 | for ($i = 1; $i < $filesCount; ++$i) { |
||
150 | $filePath = $filePaths[$i]; |
||
151 | $fileName = basename($filePath); |
||
152 | |||
153 | $pathInfo = pathinfo($filePath); |
||
154 | $fName = (isset($pathInfo['filename']) ? $pathInfo['filename'] : ''); |
||
155 | $fExt = (isset($pathInfo['extension']) ? $pathInfo['extension'] : ''); |
||
156 | |||
157 | $fNameIndex = 0; |
||
158 | while (isset($fileNameMap[$fileName])) { |
||
159 | ++$fNameIndex; |
||
160 | $fileName = $fName . ' (' . $fNameIndex . ').' . $fExt; |
||
161 | } |
||
162 | |||
163 | $fileNameMap[$fileName] = true; |
||
164 | |||
165 | $restApi->uploadInputWithFilePathAndName($jobId, $filePath, $fileName); |
||
166 | } |
||
167 | |||
168 | $restApi->startJob($jobId); |
||
169 | } catch (Exception $e) { |
||
170 | try { |
||
171 | $restApi->deleteJob($jobId); |
||
172 | } catch (Exception $eInner) { |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
![]() |
|||
173 | } |
||
174 | |||
175 | throw $e; |
||
176 | } |
||
177 | |||
178 | return new Job($restApi, $jobId); |
||
179 | } |
||
180 | } |
||
181 |