Splitter::split()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 16
ccs 12
cts 12
cp 1
rs 9.9
cc 3
nc 3
nop 3
crap 3
1
<?php
2
/**
3
 * PDFtk wrapper
4
 *
5
 * @copyright 2014-2022 Institute of Legal Medicine, Medical University of Innsbruck
6
 * @author Andreas Erhard <[email protected]>
7
 * @license LGPL-3.0-only
8
 * @link http://www.gerichtsmedizin.at/
9
 *
10
 * @package pdftk
11
 */
12
13
namespace Gmi\Toolkit\Pdftk;
14
15
use Gmi\Toolkit\Pdftk\Exception\SplitException;
16
17
use Exception;
18
19
/**
20
 * Splits PDF files.
21
 */
22
class Splitter
23
{
24
    /**
25
     * @var PdftkWrapper
26
     */
27
    private $wrapper;
28
29
    /**
30
     * Constructor.
31
     *
32
     * @param PdftkWrapper $wrapper
33
     */
34 9
    public function __construct(PdftkWrapper $wrapper = null)
35
    {
36 9
        $this->wrapper = $wrapper ?: new PdftkWrapper();
37 9
    }
38
39
    /**
40
     * Splits a PDF according to the provided filename => pages mapping.
41
     *
42
     * @param string $inputFile    Filename of the input file which should be split
43
     * @param array  $mapping      Mapping of output filename to page numbers
44
     * @param string $outputFolder Folder where the output files should be stored (without trailing slash).
45
     *                             If the output folder is null, the filenames of the mapping
46
     *                             (which can contain a path) are used as they are.
47
     *
48
     * @throws SplitException if the PDF split fails
49
     */
50 4
    public function split($inputFile, $mapping, $outputFolder = null)
51
    {
52 4
        $commandLines = $this->buildCommandLines($inputFile, $mapping, $outputFolder);
53
54 4
        foreach ($commandLines as $commandLine) {
55 4
            $process = $this->wrapper->createProcess($commandLine);
56
57
            try {
58 4
                $process->mustRun();
59 2
            } catch (Exception $e) {
60 2
                throw new SplitException(
61 2
                    sprintf('Failed to split PDF "%s"! Error: %s', $inputFile, $e->getMessage()),
62 2
                    0,
63 2
                    $e,
64 2
                    $process->getErrorOutput(),
65 4
                    $process->getOutput()
66
                );
67
            }
68
        }
69 2
    }
70
71
    /**
72
     * Builds the pdftk command lines for splitting.
73
     *
74
     * @param string $inputFile
75
     * @param array  $mapping
76
     * @param string $outputFolder
77
     *
78
     * @return string[]
79
     */
80 4
    private function buildCommandLines($inputFile, $mapping, $outputFolder = null)
81
    {
82 4
        $commandLines = [];
83
84 4
        foreach ($mapping as $filename => $pages) {
85 4
            if ($outputFolder) {
86 3
                $target = sprintf('%s/%s', $outputFolder, $filename);
87
            } else {
88 1
                $target = $filename;
89
            }
90
91 4
            $commandLines[] = sprintf(
92 4
                '%s %s cat %s output %s',
93 4
                $this->wrapper->getBinary(),
94 4
                escapeshellarg($inputFile),
95 4
                implode(' ', $pages),
96 4
                escapeshellarg($target)
97
            );
98
        }
99
100 4
        return $commandLines;
101
    }
102
}
103