| Total Complexity | 60 |
| Total Lines | 471 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like PdfcpuWrapper 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 PdfcpuWrapper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class PdfcpuWrapper implements WrapperInterface, BinaryPathAwareInterface |
||
| 31 | { |
||
| 32 | use BinaryPathAwareTrait; |
||
| 33 | |||
| 34 | private const SUPPORTED_METADATA_ATTRIBUTES = [ |
||
| 35 | 'Title', 'Keywords', 'Subject', 'Author', 'Creator', 'Producer', 'CreationDate', 'ModificationDate', |
||
| 36 | ]; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var ProcessFactory |
||
| 40 | */ |
||
| 41 | private $processFactory; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var Escaper |
||
| 45 | */ |
||
| 46 | private $escaper; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Constructor. |
||
| 50 | * |
||
| 51 | * @throws FileNotFoundException |
||
| 52 | */ |
||
| 53 | public function __construct(string $pdftkBinary = null, ProcessFactory $processFactory = null) |
||
| 54 | { |
||
| 55 | $this->setBinary($pdftkBinary ?: $this->guessBinary(PHP_OS)); |
||
| 56 | $this->processFactory = $processFactory ?: new ProcessFactory(); |
||
| 57 | $this->escaper = new Escaper(); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Guesses the pdfcpu binary path based on the operating system. |
||
| 62 | */ |
||
| 63 | public function guessBinary(string $operatingSystemString): string |
||
| 64 | { |
||
| 65 | if (strtoupper(substr($operatingSystemString, 0, 3)) === 'WIN') { |
||
| 66 | $binary = 'C:\\Program Files\\pdfcpu\\pdfcpu.exe'; |
||
| 67 | } else { |
||
| 68 | $binary = '/usr/bin/pdfcpu'; |
||
| 69 | } |
||
| 70 | |||
| 71 | return $binary; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritDoc} |
||
| 76 | */ |
||
| 77 | public function join(array $filePaths, string $outfile): void |
||
| 78 | { |
||
| 79 | $esc = $this->escaper; |
||
| 80 | |||
| 81 | $filePathsEscaped = array_map(function (string $filePath) use ($esc) { |
||
| 82 | return $esc->shellArg($filePath); |
||
| 83 | }, $filePaths); |
||
| 84 | |||
| 85 | $fileList = implode(' ', $filePathsEscaped); |
||
| 86 | |||
| 87 | $commandLine = sprintf('%s merge %s %s', $this->getBinary(), $esc->shellArg($outfile), $fileList); |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var Process |
||
| 91 | */ |
||
| 92 | $process = $this->processFactory->createProcess($commandLine); |
||
| 93 | |||
| 94 | try { |
||
| 95 | $process->mustRun(); |
||
| 96 | } catch (Exception $e) { |
||
| 97 | throw new PdfException($e->getMessage(), 0, $e, $process->getErrorOutput(), $process->getOutput()); |
||
| 98 | } |
||
| 99 | |||
| 100 | $process->getOutput(); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * {@inheritDoc} |
||
| 105 | */ |
||
| 106 | public function split(string $infile, array $mapping, string $outputFolder = null): void |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * {@inheritDoc} |
||
| 137 | */ |
||
| 138 | public function reorder(string $infile, array $order, string $outfile = null): void |
||
| 139 | { |
||
| 140 | $temporaryOutFile = false; |
||
| 141 | |||
| 142 | if ($outfile === null || $infile === $outfile) { |
||
| 143 | $temporaryOutFile = true; |
||
| 144 | $outfile = tempnam(sys_get_temp_dir(), 'pdf') . '.pdf'; |
||
| 145 | } |
||
| 146 | |||
| 147 | $esc = $this->escaper; |
||
| 148 | |||
| 149 | $commandLine = sprintf( |
||
| 150 | '%s collect -pages %s %s %s', |
||
| 151 | $this->getBinary(), |
||
| 152 | implode(',', $order), |
||
| 153 | $esc->shellArg($infile), |
||
| 154 | $esc->shellArg($outfile) |
||
| 155 | ); |
||
| 156 | |||
| 157 | $process = $this->processFactory->createProcess($commandLine); |
||
| 158 | |||
| 159 | try { |
||
| 160 | $process->mustRun(); |
||
| 161 | } catch (Exception $e) { |
||
| 162 | throw new PdfException( |
||
| 163 | sprintf('Failed to reorder PDF "%s"! Error: %s', $infile, $e->getMessage()), |
||
| 164 | 0, |
||
| 165 | $e, |
||
| 166 | $process->getErrorOutput(), |
||
| 167 | $process->getOutput() |
||
| 168 | ); |
||
| 169 | } |
||
| 170 | |||
| 171 | if ($temporaryOutFile) { |
||
| 172 | unlink($infile); |
||
| 173 | rename($outfile, $infile); |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * {@inheritDoc} |
||
| 179 | */ |
||
| 180 | public function applyBookmarks(Bookmarks $bookmarks, string $infile, string $outfile = null): self |
||
| 181 | { |
||
| 182 | $temporaryOutFile = false; |
||
| 183 | |||
| 184 | $this->checkPdfFileExists($infile); |
||
| 185 | $bookmarksJson = $this->exportBookmarksToJson($bookmarks); |
||
| 186 | $tempfile = tempnam(sys_get_temp_dir(), 'bookmarks') . '.json'; |
||
| 187 | file_put_contents($tempfile, $bookmarksJson); |
||
| 188 | |||
| 189 | if ($outfile === null || $infile === $outfile) { |
||
| 190 | $temporaryOutFile = true; |
||
| 191 | $outfile = tempnam(sys_get_temp_dir(), 'pdf') . '.pdf'; |
||
| 192 | } |
||
| 193 | |||
| 194 | $cmd = sprintf( |
||
| 195 | '%s bookmarks import %s %s %s', |
||
| 196 | $this->getBinary(), |
||
| 197 | $this->escaper->shellArg($infile), |
||
| 198 | $this->escaper->shellArg($tempfile), |
||
| 199 | $this->escaper->shellArg($outfile) |
||
| 200 | ); |
||
| 201 | |||
| 202 | $process = $this->processFactory->createProcess($cmd); |
||
| 203 | |||
| 204 | try { |
||
| 205 | $process->mustRun(); |
||
| 206 | } catch (Exception $e) { |
||
| 207 | $exception = new PdfException( |
||
| 208 | sprintf('Failed to write PDF bookmarks to "%s"! Error: %s', $outfile, $e->getMessage()), |
||
| 209 | 0, |
||
| 210 | $e, |
||
| 211 | $process->getErrorOutput(), |
||
| 212 | $process->getOutput() |
||
| 213 | ); |
||
| 214 | } |
||
| 215 | |||
| 216 | unlink($tempfile); |
||
| 217 | |||
| 218 | if ($temporaryOutFile && !isset($exception)) { |
||
| 219 | unlink($infile); |
||
| 220 | rename($outfile, $infile); |
||
| 221 | } |
||
| 222 | |||
| 223 | if (isset($exception)) { |
||
| 224 | throw $exception; |
||
| 225 | } |
||
| 226 | |||
| 227 | return $this; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * {@inheritDoc} |
||
| 232 | */ |
||
| 233 | public function importBookmarks(Bookmarks $bookmarks, string $infile): self |
||
| 234 | { |
||
| 235 | $tempBookmarksFile = tempnam(sys_get_temp_dir(), 'bookmarks') . '.json'; |
||
| 236 | |||
| 237 | $this->checkPdfFileExists($infile); |
||
| 238 | |||
| 239 | $cmd = sprintf( |
||
| 240 | '%s bookmarks export %s %s', |
||
| 241 | $this->getBinary(), |
||
| 242 | $this->escaper->shellArg($infile), |
||
| 243 | $this->escaper->shellArg($tempBookmarksFile) |
||
| 244 | ); |
||
| 245 | |||
| 246 | $process = $this->processFactory->createProcess($cmd); |
||
| 247 | |||
| 248 | try { |
||
| 249 | $process->mustRun(); |
||
| 250 | } catch (Exception $e) { |
||
| 251 | $exception = new PdfException( |
||
| 252 | sprintf('Failed to read bookmarks data from "%s"! Error: %s', $infile, $e->getMessage()), |
||
| 253 | 0, |
||
| 254 | $e, |
||
| 255 | $process->getErrorOutput(), |
||
| 256 | $process->getOutput() |
||
| 257 | ); |
||
| 258 | } |
||
| 259 | |||
| 260 | if (isset($exception) && false === strpos($process->getErrorOutput(), 'no outlines available')) { |
||
| 261 | @unlink($tempBookmarksFile); |
||
|
1 ignored issue
–
show
|
|||
| 262 | throw $exception; |
||
| 263 | } |
||
| 264 | |||
| 265 | $this->importBookmarksFromJson($bookmarks, @file_get_contents($tempBookmarksFile) ?? ''); |
||
| 266 | |||
| 267 | @unlink($tempBookmarksFile); |
||
| 268 | |||
| 269 | return $this; |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * {@inheritDoc} |
||
| 274 | */ |
||
| 275 | public function importPages(Pages $pages, string $infile): self |
||
| 276 | { |
||
| 277 | $this->checkPdfFileExists($infile); |
||
| 278 | |||
| 279 | $cmd = sprintf('%s info -pages 1- -j %s', $this->getBinary(), $this->escaper->shellArg($infile)); |
||
| 280 | |||
| 281 | $process = $this->processFactory->createProcess($cmd); |
||
| 282 | |||
| 283 | try { |
||
| 284 | $process->mustRun(); |
||
| 285 | } catch (Exception $e) { |
||
| 286 | $exception = new PdfException( |
||
| 287 | sprintf('Failed to read pages data from "%s"! Error: %s', $infile, $e->getMessage()), |
||
| 288 | 0, |
||
| 289 | $e, |
||
| 290 | $process->getErrorOutput(), |
||
| 291 | $process->getOutput() |
||
| 292 | ); |
||
| 293 | |||
| 294 | throw $exception; |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Remove invalid JSON (useless line with the page numbers at the beginning) |
||
| 299 | * @todo Remove when pdfcpu does not emit the extra pages line before JSON anymore |
||
| 300 | */ |
||
| 301 | $outputCleaned = preg_replace('/^pages: (\d,?)+$/mu', '', $process->getOutput()); |
||
| 302 | $infoRaw = json_decode($outputCleaned, true); |
||
| 303 | |||
| 304 | $pageBoundaries = $infoRaw['infos'][0]['pageBoundaries']; |
||
| 305 | |||
| 306 | // the page numbers in the JSON are strings, not numbers and sorted as strings, ensure natural sort |
||
| 307 | ksort($pageBoundaries, SORT_NATURAL); |
||
| 308 | |||
| 309 | foreach ($pageBoundaries as $pageNumber => $pageInfo) { |
||
| 310 | $page = new Page(); |
||
| 311 | |||
| 312 | $page |
||
| 313 | ->setPageNumber((int) $pageNumber) |
||
| 314 | ->setRotation((int) $pageInfo['rot']) |
||
| 315 | ->setWidth((float) $pageInfo['mediaBox']['rect']['ur']['x']) |
||
| 316 | ->setHeight((float) $pageInfo['mediaBox']['rect']['ur']['y']) |
||
| 317 | ; |
||
| 318 | |||
| 319 | $pages->add($page); |
||
| 320 | } |
||
| 321 | |||
| 322 | return $this; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * {@inheritDoc} |
||
| 327 | */ |
||
| 328 | public function applyMetadata(Metadata $metadata, string $infile, string $outfile = null): self |
||
| 329 | { |
||
| 330 | throw new NotImplementedException('The current pdfcpu version does not support to set metadata!'); |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * {@inheritDoc} |
||
| 335 | */ |
||
| 336 | public function importMetadata(Metadata $metadata, string $infile): self |
||
| 337 | { |
||
| 338 | $cmd = sprintf('%s info -j %s', $this->getBinary(), $this->escaper->shellArg($infile)); |
||
| 339 | |||
| 340 | $process = $this->processFactory->createProcess($cmd); |
||
| 341 | |||
| 342 | try { |
||
| 343 | $process->mustRun(); |
||
| 344 | } catch (Exception $e) { |
||
| 345 | throw new PdfException( |
||
| 346 | sprintf('Failed to read metadata data from "%s"! Error: %s', $infile, $e->getMessage()), |
||
| 347 | 0, |
||
| 348 | $e, |
||
| 349 | $process->getErrorOutput(), |
||
| 350 | $process->getOutput() |
||
| 351 | ); |
||
| 352 | } |
||
| 353 | |||
| 354 | $raw = json_decode($process->getOutput(), true); |
||
| 355 | $metadataArray = $raw['infos'][0]; |
||
| 356 | |||
| 357 | foreach (self::SUPPORTED_METADATA_ATTRIBUTES as $attribute) { |
||
| 358 | $attributeNormalized = lcfirst($attribute); |
||
| 359 | |||
| 360 | if ($attributeNormalized === 'keywords' && isset($metadataArray['keywords'])) { |
||
| 361 | $metadataArray['keywords'] = implode(', ', $metadataArray['keywords']); |
||
| 362 | } |
||
| 363 | |||
| 364 | if (isset($metadataArray[$attributeNormalized]) && '' !== trim($metadataArray[$attributeNormalized])) { |
||
| 365 | $metadata->set($attribute, $metadataArray[$attributeNormalized]); |
||
| 366 | } |
||
| 367 | } |
||
| 368 | |||
| 369 | return $this; |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Checks whether a PDF file exists. |
||
| 374 | */ |
||
| 375 | private function checkPdfFileExists($file) |
||
| 376 | { |
||
| 377 | if (!file_exists($file)) { |
||
| 378 | throw new FileNotFoundException(sprintf('PDF "%s" not found', $file)); |
||
| 379 | } |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Imports bookmarks from a pdfcpu bookmark JSON file. |
||
| 384 | */ |
||
| 385 | private function importBookmarksFromJson(Bookmarks $bookmarks, string $json): self |
||
| 386 | { |
||
| 387 | $raw = json_decode($json, true); |
||
| 388 | $bookmarksArray = $raw['bookmarks'] ?? []; |
||
| 389 | |||
| 390 | $this->parseBookmarksTree($bookmarks, $bookmarksArray); |
||
| 391 | |||
| 392 | return $this; |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Recursively traverse the bookmarks array and add the bookmarks appropriately. |
||
| 397 | */ |
||
| 398 | private function parseBookmarksTree(Bookmarks $bookmarks, array $arr, int $level = 1) |
||
| 399 | { |
||
| 400 | foreach ($arr as $current) { |
||
| 401 | $bookmark = new Bookmark(); |
||
| 402 | |||
| 403 | $bookmark |
||
| 404 | ->setTitle($current['title']) |
||
| 405 | ->setPageNumber($current['page']) |
||
| 406 | ->setLevel($level) |
||
| 407 | ; |
||
| 408 | |||
| 409 | $bookmarks->add($bookmark); |
||
| 410 | |||
| 411 | if (isset($current['kids'])) { |
||
| 412 | $this->parseBookmarksTree($bookmarks, $current['kids'], $level + 1); |
||
| 413 | } |
||
| 414 | } |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Exports bookmarks to a pdfcpu bookmark JSON file. |
||
| 419 | */ |
||
| 420 | private function exportBookmarksToJson(Bookmarks $bookmarks): string |
||
| 421 | { |
||
| 422 | $bookmarksRecursiveArray = $this->buildBookmarksTree($this->buildBookmarksArrayForTree($bookmarks)); |
||
| 423 | |||
| 424 | return json_encode(['bookmarks' => $bookmarksRecursiveArray], JSON_PRETTY_PRINT); |
||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Recursively build the JSON tree based on the normalized bookmarks array. |
||
| 429 | */ |
||
| 430 | private function buildBookmarksTree(array $bookmarksArray, $parentId = null): array |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Builds an array with additional entries prefixed with "__" for level, id and parent id. |
||
| 456 | */ |
||
| 457 | private function buildBookmarksArrayForTree(Bookmarks $bookmarks): array |
||
| 487 | } |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Returns the id of the last bookmark with a lower level than the provided current level. |
||
| 491 | */ |
||
| 492 | private function getLastParentId(array $bookmarksArray, int $currentLevel): ?int |
||
| 493 | { |
||
| 494 | for ($j = count($bookmarksArray) - 1; $j >= 0; $j--) { |
||
| 495 | if ($bookmarksArray[$j]['__level'] < $currentLevel) { |
||
| 501 | } |
||
| 502 | } |
||
| 503 |
If you suppress an error, we recommend checking for the error condition explicitly: