Pages::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * PDFtk wrapper
4
 *
5
 * @copyright 2014-2019 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
/**
16
 * Read PDF page information.
17
 */
18
class Pages
19
{
20
    /**
21
     * Pages.
22
     *
23
     * @var Page[]
24
     */
25
    private $pages = [];
26
27
    /**
28
     * @var PdftkWrapper
29
     */
30
    private $wrapper;
31
32
    /**
33
     * Constructor.
34
     *
35
     * @param PdftkWrapper $wrapper
36
     */
37 9
    public function __construct(PdftkWrapper $wrapper = null)
38
    {
39 9
        $this->wrapper = $wrapper ?: new PdftkWrapper();
40 9
    }
41
42
    /**
43
     * Returns all pages.
44
     *
45
     * @return Page[]
46
     */
47 5
    public function all()
48
    {
49 5
        return $this->pages;
50
    }
51
52
    /**
53
     * Remove all pages.
54
     *
55
     * @return self
56
     */
57 2
    public function clear()
58
    {
59 2
        $this->pages = [];
60
61 2
        return $this;
62
    }
63
64
    /**
65
     * Imports pages from a PDF file.
66
     *
67
     * @param string $infile
68
     *
69
     * @return self
70
     */
71 4
    public function import($infile)
72
    {
73 4
        $dump = $this->wrapper->getPdfDataDump($infile);
74 3
        $this->importFromDump($dump);
75
76 3
        return $this;
77
    }
78
79
    /**
80
     * Imports page meta data from a pdftk dump.
81
     *
82
     * @param string $dump
83
     *
84
     * @return $this
85
     */
86 5
    public function importFromDump($dump)
87
    {
88 5
        $matches = [];
89
        $regex = '/PageMediaBegin\nPageMediaNumber: (?<page>.+)\nPageMediaRotation: (?<rotation>[0-9]+)\n' .
90
                 'PageMediaRect: .*\n' .
91 5
                 'PageMediaDimensions: (?<dim>(([0-9]\,)?[0-9]+(\.[0-9]+)?) (([0-9]\,)?[0-9]+(\.[0-9]+)?))/';
92 5
        preg_match_all($regex, $dump, $matches, PREG_SET_ORDER);
93
94 5
        $this->pages = [];
95 5
        foreach ($matches as $p) {
96 5
            $page = new Page();
97
98 5
            $dimensions = explode(' ', $p['dim']);
99
100
            $page
101 5
                ->setPageNumber((int) $p['page'])
102 5
                ->setRotation((int) $p['rotation'])
103 5
                ->setWidth((float) str_replace(',', '', $dimensions[0]))
104 5
                ->setHeight((float) str_replace(',', '', $dimensions[1]))
105
            ;
106
107 5
            $this->pages[] = $page;
108
        }
109
110 5
        return $this;
111
    }
112
}
113