| Total Complexity | 50 |
| Total Lines | 430 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like program often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use program, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class program |
||
| 8 | { |
||
| 9 | ////////////////////////////////////////////////////////////////////// |
||
| 10 | // |
||
| 11 | // !!! VERY IMPORTANT !!! |
||
| 12 | // |
||
| 13 | // You must configure these variables before running this sample code. |
||
| 14 | // |
||
| 15 | ////////////////////////////////////////////////////////////////////// |
||
| 16 | |||
| 17 | // Client ID. You can get your client ID from the developer page |
||
| 18 | // (for registered user, you must sign in before visiting this page) |
||
| 19 | // https://www.easypdfcloud.com/developer |
||
| 20 | // |
||
| 21 | // To test the sample code quickly for demo purpose, you can use |
||
| 22 | // the following demo client ID: |
||
| 23 | // |
||
| 24 | // 05ee808265f24c66b2b8e31d90c31ab1 |
||
| 25 | // |
||
| 26 | private static $clientId = '05ee808265f24c66b2b8e31d90c31ab1'; |
||
| 27 | |||
| 28 | // Client secret. You can get your client secret from the developer page |
||
| 29 | // (for registered user, you must sign in before visiting this page) |
||
| 30 | // https://www.easypdfcloud.com/developer |
||
| 31 | // |
||
| 32 | // To test the sample code quickly for demo purpose, you can use |
||
| 33 | // the following demo client secret: |
||
| 34 | // |
||
| 35 | // 16ABE87E052059F147BB2A491944BF7EA7876D0F843DED105CBA094C887CBC99 |
||
| 36 | // |
||
| 37 | private static $clientSecret = '16ABE87E052059F147BB2A491944BF7EA7876D0F843DED105CBA094C887CBC99'; |
||
| 38 | |||
| 39 | // Workflow ID. You can get your workflow ID from the developer page |
||
| 40 | // (for registered user, you must sign in before visiting this page) |
||
| 41 | // https://www.easypdfcloud.com/developer |
||
| 42 | // |
||
| 43 | // To test the sample code quickly for demo purpose, you can use |
||
| 44 | // one of the following demo workflow IDs: |
||
| 45 | // |
||
| 46 | // 00000000048585C1 : "Convert Files to PDF" workflow |
||
| 47 | // 00000000048585C2 : "Convert PDF to Word" workflow |
||
| 48 | // 00000000048585C3 : "Combine Files to PDF" workflow |
||
| 49 | // |
||
| 50 | private static $workflowId = '00000000048585C1'; |
||
| 51 | |||
| 52 | // A flag indicating if the specified workflow contains the "Combine PDFs" task |
||
| 53 | // |
||
| 54 | // true: The specified workflow contains the "Combine PDFs" task |
||
| 55 | // false: The specified workflow does not contain the "Combine PDFs" task |
||
| 56 | // |
||
| 57 | private static $workflowIncludesCombinePdfsTask = false; |
||
| 58 | |||
| 59 | // A flag to specify whether to enable test mode for job execution. |
||
| 60 | // |
||
| 61 | // true: Enable test mode (do not use API credits) |
||
| 62 | // false: Do not enable test mode (use API credits) |
||
| 63 | // |
||
| 64 | // If true is specified, then the job is executed as a test mode and |
||
| 65 | // your API credit will not be used. |
||
| 66 | // |
||
| 67 | private static $enableTestMode = true; |
||
| 68 | |||
| 69 | // Path to the input file which you want to upload and process |
||
| 70 | // This value is used only if the $workflowIncludesCombinePdfsTask == false |
||
| 71 | // |
||
| 72 | // To test the sample code quickly for demo purpose, you can use |
||
| 73 | // one of the following files bundled with this sample: |
||
| 74 | // |
||
| 75 | // samples/input/sample.docx : For converting Word to PDF |
||
| 76 | // samples/input/sample.pdf : For converting PDF to Word |
||
| 77 | // |
||
| 78 | private static $inputFilePath = 'samples/input/sample.docx'; |
||
| 79 | |||
| 80 | // List of path to the input file which you want to upload and process |
||
| 81 | // This value is used only if the $workflowIncludesCombinePdfsTask == true |
||
| 82 | // |
||
| 83 | // To test the sample code quickly for demo purpose, you can use |
||
| 84 | // the following files bundled with this sample: |
||
| 85 | // |
||
| 86 | // samples/input/sample.docx : A sample Word file |
||
| 87 | // samples/input/sample.pdf : A sample PDF file |
||
| 88 | // |
||
| 89 | private static $inputFilePaths = array( |
||
| 90 | 'samples/input/sample.docx', |
||
| 91 | 'samples/input/sample.pdf', |
||
| 92 | ); |
||
| 93 | |||
| 94 | // Output directory used for saving output file. Make sure that you |
||
| 95 | // have sufficient permission to create and write to this directory |
||
| 96 | private static $outputDir = 'samples/output'; |
||
| 97 | |||
| 98 | // A flag indicating whether to push output file to the client when |
||
| 99 | // this sample code is running from a web server |
||
| 100 | // |
||
| 101 | // true: Push output to the client when running from a web server |
||
| 102 | // false: Always save output to the local location specified by $outputDir |
||
| 103 | // |
||
| 104 | // For debugging purpose, setting this value to false is recommended |
||
| 105 | // when running this sample code for the first time |
||
| 106 | private static $pushOutputToClientIfRunningFromWebServer = false; |
||
| 107 | |||
| 108 | ////////////////////////////////////////////////////////////////////// |
||
| 109 | |||
| 110 | public static function main() |
||
| 111 | { |
||
| 112 | // Set current working directory to the same location as this file |
||
| 113 | // so that the default sample input file and output directory |
||
| 114 | // (both using relative path) can be found |
||
| 115 | \chdir(__DIR__); |
||
| 116 | |||
| 117 | static::debugPrintLine('Working directory: ' . \getcwd()); |
||
| 118 | static::debugPrintLine(); |
||
| 119 | |||
| 120 | try { |
||
| 121 | // Check user parameters |
||
| 122 | static::checkParameters(); |
||
| 123 | } catch (\Exception $e) { |
||
| 124 | static::printException('Parameter check failed!', $e); |
||
| 125 | |||
| 126 | return; |
||
| 127 | } |
||
| 128 | |||
| 129 | try { |
||
| 130 | static::debugPrintLine('Executing new job...'); |
||
| 131 | |||
| 132 | if (false === static::$workflowIncludesCombinePdfsTask) { |
||
| 133 | // The workflow does not include the "Combine PDFs" task |
||
| 134 | |||
| 135 | // Start job execution |
||
| 136 | static::executeNewJob( |
||
| 137 | static::$clientId, |
||
| 138 | static::$clientSecret, |
||
| 139 | static::$workflowId, |
||
| 140 | static::$enableTestMode, |
||
| 141 | static::$inputFilePath, |
||
| 142 | static::$outputDir |
||
| 143 | ); |
||
| 144 | } else { |
||
| 145 | // The workflow includes the "Combine PDFs" task |
||
| 146 | |||
| 147 | // Start job execution |
||
| 148 | static::executeNewJobForMergeTask( |
||
| 149 | static::$clientId, |
||
| 150 | static::$clientSecret, |
||
| 151 | static::$workflowId, |
||
| 152 | static::$enableTestMode, |
||
| 153 | static::$inputFilePaths, |
||
| 154 | static::$outputDir |
||
| 155 | ); |
||
| 156 | } |
||
| 157 | } catch (\Bcl\EasyPdfCloud\EasyPdfCloudApiException $e) { |
||
| 158 | static::printException('API execution failed!', $e); |
||
| 159 | } catch (\Bcl\EasyPdfCloud\JobExecutionException $e) { |
||
| 160 | static::printException('Job execution failed!', $e); |
||
| 161 | // Check API/OCR credits info |
||
| 162 | static::checkCreditsInfo($e->getJobInfo()); |
||
| 163 | } catch (\Bcl\EasyPdfCloud\ApiAuthorizationException $e) { |
||
| 164 | static::printException('API Authorization failed!', $e); |
||
| 165 | } catch (\Exception $e) { |
||
| 166 | static::printException('Uncaught exception!', $e); |
||
| 167 | } |
||
| 168 | |||
| 169 | static::debugPrintLine('Done'); |
||
| 170 | } |
||
| 171 | |||
| 172 | ////////////////////////////////////////////////////////////////////// |
||
| 173 | |||
| 174 | private static function isRunningFromConsole() |
||
| 175 | { |
||
| 176 | return \PHP_SAPI === 'cli'; |
||
| 177 | } |
||
| 178 | |||
| 179 | ////////////////////////////////////////////////////////////////////// |
||
| 180 | |||
| 181 | private static function eol() |
||
| 182 | { |
||
| 183 | return static::isRunningFromConsole() ? PHP_EOL : '<br />'; |
||
| 184 | } |
||
| 185 | |||
| 186 | ////////////////////////////////////////////////////////////////////// |
||
| 187 | |||
| 188 | private static function shouldSaveOutputToLocalStorage() |
||
| 189 | { |
||
| 190 | if (static::isRunningFromConsole()) { |
||
| 191 | return true; |
||
| 192 | } |
||
| 193 | |||
| 194 | if (!static::$pushOutputToClientIfRunningFromWebServer) { |
||
| 195 | return true; |
||
| 196 | } |
||
| 197 | |||
| 198 | return false; |
||
| 199 | } |
||
| 200 | |||
| 201 | ////////////////////////////////////////////////////////////////////// |
||
| 202 | |||
| 203 | private static function debugPrintLine($string = null) |
||
| 204 | { |
||
| 205 | if (static::shouldSaveOutputToLocalStorage()) { |
||
| 206 | static::printLine($string); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | ////////////////////////////////////////////////////////////////////// |
||
| 211 | |||
| 212 | private static function printLine($string = null) |
||
| 213 | { |
||
| 214 | echo(\mb_strlen($string, 'utf-8') > 0 ? $string : '') . static::eol(); |
||
| 215 | } |
||
| 216 | |||
| 217 | ////////////////////////////////////////////////////////////////////// |
||
| 218 | |||
| 219 | private static function getStackTrace($exception) |
||
| 220 | { |
||
| 221 | $prefix = ''; |
||
| 222 | $postfix = ''; |
||
| 223 | |||
| 224 | if (!static::isRunningFromConsole()) { |
||
| 225 | $prefix = '<pre>'; |
||
| 226 | $postfix = '</pre>'; |
||
| 227 | } |
||
| 228 | |||
| 229 | $trace = $prefix . $exception->getTraceAsString() . $postfix; |
||
| 230 | |||
| 231 | return $trace; |
||
| 232 | } |
||
| 233 | |||
| 234 | ////////////////////////////////////////////////////////////////////// |
||
| 235 | |||
| 236 | private static function printException($message, $exception) |
||
| 237 | { |
||
| 238 | static::printLine(); |
||
| 239 | static::printLine($message); |
||
| 240 | static::printLine('Message: ' . $exception->getMessage()); |
||
| 241 | static::printLine(); |
||
| 242 | static::printLine('Stack trace: '); |
||
| 243 | static::printLine(static::getStackTrace($exception)); |
||
| 244 | static::printLine(); |
||
| 245 | } |
||
| 246 | |||
| 247 | ////////////////////////////////////////////////////////////////////// |
||
| 248 | |||
| 249 | private static function checkParameters() |
||
| 250 | { |
||
| 251 | static::debugPrintLine('--------------------------------------------------------'); |
||
| 252 | static::debugPrintLine('App parameters:'); |
||
| 253 | static::debugPrintLine(); |
||
| 254 | static::debugPrintLine('clientId: ' . static::$clientId); |
||
| 255 | static::debugPrintLine('clientSecret: ' . (0 === \mb_strlen(static::$clientSecret, 'utf-8') ? '' : '********')); |
||
| 256 | static::debugPrintLine('workflowId: ' . static::$workflowId); |
||
| 257 | static::debugPrintLine('enableTestMode: ' . (static::$enableTestMode ? 'true' : 'false')); |
||
| 258 | |||
| 259 | if (false === static::$workflowIncludesCombinePdfsTask) { |
||
| 260 | static::debugPrintLine('inputFilePath: ' . static::$inputFilePath); |
||
| 261 | } else { |
||
| 262 | static::debugPrintLine('inputFilePaths:'); |
||
| 263 | foreach (static::$inputFilePaths as $filePath) { |
||
| 264 | static::debugPrintLine(' ' . $filePath); |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | static::debugPrintLine('outputDir: ' . static::$outputDir); |
||
| 269 | static::debugPrintLine('--------------------------------------------------------'); |
||
| 270 | static::debugPrintLine(); |
||
| 271 | |||
| 272 | if (0 === \mb_strlen(static::$clientId, 'utf-8')) { |
||
| 273 | throw new \InvalidArgumentException('clientId is not specified in the code!'); |
||
| 274 | } |
||
| 275 | |||
| 276 | if (0 === \mb_strlen(static::$clientSecret, 'utf-8')) { |
||
| 277 | throw new \InvalidArgumentException('clientSecret is not specified in the code!'); |
||
| 278 | } |
||
| 279 | |||
| 280 | if (false === static::$workflowIncludesCombinePdfsTask) { |
||
| 281 | if (0 === \mb_strlen(static::$inputFilePath, 'utf-8')) { |
||
| 282 | throw new \InvalidArgumentException('inputFilePath is not specified in the code!'); |
||
| 283 | } |
||
| 284 | |||
| 285 | if (false === \is_file(static::$inputFilePath)) { |
||
| 286 | throw new \InvalidArgumentException(static::$inputFilePath . ' does not exist'); |
||
| 287 | } |
||
| 288 | } else { |
||
| 289 | if (0 === \count(static::$inputFilePaths)) { |
||
| 290 | throw new \InvalidArgumentException('inputFilePaths is not specified in the code!'); |
||
| 291 | } |
||
| 292 | |||
| 293 | foreach (static::$inputFilePaths as $filePath) { |
||
| 294 | if (false === \is_file($filePath)) { |
||
| 295 | throw new \InvalidArgumentException($filePath . ' does not exist'); |
||
| 296 | } |
||
| 297 | } |
||
| 298 | } |
||
| 299 | |||
| 300 | if (0 === \mb_strlen(static::$outputDir, 'utf-8')) { |
||
| 301 | throw new \InvalidArgumentException('outputDir is not specified in the code!'); |
||
| 302 | } |
||
| 303 | |||
| 304 | if (static::shouldSaveOutputToLocalStorage()) { |
||
| 305 | if (false === \is_dir(static::$outputDir)) { |
||
| 306 | if (false === \mkdir(static::$outputDir, 0755, true)) { |
||
| 307 | static::debugPrintLine(); |
||
| 308 | static::debugPrintLine('Unable to create output directory'); |
||
| 309 | static::debugPrintLine('Make sure you have sufficient permission to create the output directory.'); |
||
| 310 | static::debugPrintLine(); |
||
| 311 | } |
||
| 312 | } |
||
| 313 | } |
||
| 314 | } |
||
| 315 | |||
| 316 | ////////////////////////////////////////////////////////////////////// |
||
| 317 | |||
| 318 | private static function checkCreditsInfo($jobInfo) |
||
| 319 | { |
||
| 320 | $jobInfoDetail = $jobInfo->getDetail(); |
||
| 321 | |||
| 322 | if (null !== $jobInfoDetail) { |
||
| 323 | $apiCredits = $jobInfoDetail->getApiCredits(); |
||
| 324 | $ocrCredits = $jobInfoDetail->getOcrCredits(); |
||
| 325 | |||
| 326 | if ((null !== $apiCredits) || (null !== $ocrCredits)) { |
||
| 327 | static::debugPrintLine(); |
||
| 328 | |||
| 329 | if (null !== $apiCredits) { |
||
| 330 | // Check API credits |
||
| 331 | |||
| 332 | static::debugPrintLine('API credits remaining: ' . $apiCredits->getCreditsRemaining()); |
||
| 333 | |||
| 334 | if ($apiCredits->getNotEnoughCredits()) { |
||
| 335 | static::debugPrintLine('Not enough API credits!'); |
||
| 336 | } |
||
| 337 | |||
| 338 | static::debugPrintLine(); |
||
| 339 | } |
||
| 340 | |||
| 341 | if (null !== $ocrCredits) { |
||
| 342 | // Check OCR credits |
||
| 343 | |||
| 344 | static::debugPrintLine('OCR credits remaining: ' . $ocrCredits->getCreditsRemaining()); |
||
| 345 | |||
| 346 | if ($ocrCredits->getNotEnoughCredits()) { |
||
| 347 | static::debugPrintLine('Not enough OCR credits!'); |
||
| 348 | } |
||
| 349 | |||
| 350 | static::debugPrintLine(); |
||
| 351 | } |
||
| 352 | } |
||
| 353 | } |
||
| 354 | } |
||
| 355 | |||
| 356 | ////////////////////////////////////////////////////////////////////// |
||
| 357 | |||
| 358 | private static function saveOutput(\Bcl\EasyPdfCloud\FileData $fileData, $outputDir) |
||
| 359 | { |
||
| 360 | $fileName = $fileData->getName(); |
||
| 361 | $contents = $fileData->getContents(); |
||
| 362 | $fileSize = $fileData->getBytes(); |
||
| 363 | |||
| 364 | static::debugPrintLine('Job execution completed'); |
||
| 365 | static::debugPrintLine('Output file name: ' . $fileName); |
||
| 366 | static::debugPrintLine('Output file size: ' . \number_format($fileSize) . ' bytes'); |
||
| 367 | |||
| 368 | if (static::shouldSaveOutputToLocalStorage()) { |
||
| 369 | static::debugPrintLine('Saving to output directory...'); |
||
| 370 | |||
| 371 | $outputFilePath = \rtrim($outputDir, '/\\') . DIRECTORY_SEPARATOR . $fileName; |
||
| 372 | |||
| 373 | // Save output to local directory |
||
| 374 | if (false === \file_put_contents($outputFilePath, $contents)) { |
||
| 375 | static::debugPrintLine(); |
||
| 376 | static::debugPrintLine('Unable to save output file to output directory'); |
||
| 377 | static::debugPrintLine('Make sure you have sufficient permission to write to the output directory.'); |
||
| 378 | static::debugPrintLine(); |
||
| 379 | |||
| 380 | return; |
||
| 381 | } |
||
| 382 | |||
| 383 | static::debugPrintLine('Output saved to: ' . $outputFilePath); |
||
| 384 | |||
| 385 | return; |
||
| 386 | } |
||
| 387 | |||
| 388 | // Push output to the client |
||
| 389 | \header('Content-Type: ' . 'application/octet-stream'); |
||
| 390 | \header('Content-Length: ' . $fileSize); |
||
| 391 | \header('Content-Disposition: attachment; filename="' . urlencode($fileName) . '"'); |
||
| 392 | echo $contents; |
||
| 393 | } |
||
| 394 | |||
| 395 | ////////////////////////////////////////////////////////////////////// |
||
| 396 | |||
| 397 | private static function executeNewJob($clientId, $clientSecret, $workflowId, $enableTestMode, $inputFilePath, $outputDir) |
||
| 398 | { |
||
| 399 | // Create easyPDF Cloud client object |
||
| 400 | $client = new \Bcl\EasyPdfCloud\Client($clientId, $clientSecret); |
||
| 401 | |||
| 402 | // Upload input file and start new job |
||
| 403 | $job = $client->startNewJobWithFilePath($workflowId, $inputFilePath, $enableTestMode); |
||
| 404 | |||
| 405 | static::debugPrintLine('New job started (job ID: ' . $job->getJobId() . ')'); |
||
| 406 | static::debugPrintLine('Waiting for job execution completion...'); |
||
| 407 | |||
| 408 | // Wait until job execution is completed |
||
| 409 | $jobExecutionResult = $job->waitForJobExecutionCompletion(); |
||
| 410 | |||
| 411 | // Check API/OCR credits info |
||
| 412 | static::checkCreditsInfo($jobExecutionResult->getJobInfo()); |
||
| 413 | |||
| 414 | static::saveOutput($jobExecutionResult->getFileData(), $outputDir); |
||
| 415 | } |
||
| 416 | |||
| 417 | ////////////////////////////////////////////////////////////////////// |
||
| 418 | |||
| 419 | private static function executeNewJobForMergeTask($clientId, $clientSecret, $workflowId, $enableTestMode, array $inputFilePaths, $outputDir) |
||
| 420 | { |
||
| 421 | // Create easyPDF Cloud client object |
||
| 422 | $client = new \Bcl\EasyPdfCloud\Client($clientId, $clientSecret); |
||
| 423 | |||
| 424 | // Upload input file and start new job |
||
| 425 | $job = $client->startNewJobForMergeTask($workflowId, $inputFilePaths, $enableTestMode); |
||
| 426 | |||
| 427 | static::debugPrintLine('New job started (job ID: ' . $job->getJobId() . ')'); |
||
| 428 | static::debugPrintLine('Waiting for job execution completion...'); |
||
| 429 | |||
| 430 | // Wait until job execution is completed |
||
| 431 | $jobExecutionResult = $job->waitForJobExecutionCompletion(); |
||
| 432 | |||
| 433 | // Check API/OCR credits info |
||
| 434 | static::checkCreditsInfo($jobExecutionResult->getJobInfo()); |
||
| 435 | |||
| 436 | static::saveOutput($jobExecutionResult->getFileData(), $outputDir); |
||
| 437 | } |
||
| 444 |