Completed
Push — master ( fd9d07...e23a03 )
by Hannes
01:57 queued 01:07
created

Merger::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace iio\libmergepdf;
4
5
use setasign\Fpdi\Fpdi;
6
use Symfony\Component\Finder\Finder;
7
8
/**
9
 * Merge existing pdfs into one
10
 */
11
class Merger
12
{
13
    /**
14
     * List of pdf sources to merge
15
     *
16
     * @var SourceInterface[]
17
     */
18
    private $sources = [];
19
20
    /**
21
     * @var Fpdi Fpdi object
22
     */
23
    private $fpdi;
24
25
    /**
26
     * @var string Directory path used for temporary files
27
     */
28
    private $tempDir;
29
30
    /**
31
     * Constructor
32
     *
33
     * @param Fpdi $fpdi
34
     */
35
    public function __construct(Fpdi $fpdi = null)
36
    {
37
        $this->fpdi = $fpdi ?: new Fpdi;
38
    }
39
40
    /**
41
     * Add raw PDF from string
42
     *
43
     * Note that your PDFs are merged in the order that you add them
44
     *
45
     * @param  string $content Raw pdf content
46
     * @param  Pages  $pages   Specification of the pages to add
47
     * @return void
48
     */
49
    public function addRaw($content, Pages $pages = null)
50
    {
51
        $this->sources[] = new RawSource($content, $pages ?: new Pages);
52
    }
53
54
    /**
55
     * Add PDF from file
56
     *
57
     * Note that your PDFs are merged in the order that you add them
58
     *
59
     * @param  string    $filename Name of file to add
60
     * @param  Pages     $pages    Pages to add from file
61
     * @return void
62
     */
63
    public function addFile($filename, Pages $pages = null)
64
    {
65
        $this->sources[] = new FileSource($filename, $pages ?: new Pages);
66
    }
67
68
    /**
69
     * Add PDF from file
70
     *
71
     * @deprecated Since version 3.1
72
     */
73
    public function addFromFile($fname, Pages $pages = null, $cleanup = null)
0 ignored issues
show
Unused Code introduced by
The parameter $cleanup is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
74
    {
75
        trigger_error('addFromFile() is deprecated, use addFile() instead', E_USER_DEPRECATED);
76
        $this->addFile($fname, $pages);
77
    }
78
79
    /**
80
     * Add files using iterator
81
     *
82
     * @param  iterable  $iterator Iterator or array with names of files to merge
83
     * @param  Pages     $pages    Optional pages constraint used for every added pdf
84
     * @return void
85
     * @throws Exception If $iterator is not valid
86
     */
87
    public function addIterator($iterator, Pages $pages = null)
88
    {
89
        if (!is_array($iterator) && !$iterator instanceof \Traversable) {
90
            throw new Exception("\$iterator must be traversable");
91
        }
92
93
        foreach ($iterator as $filename) {
94
            $this->addFile($filename, $pages);
95
        }
96
    }
97
98
    /**
99
     * Add files using a symfony finder
100
     *
101
     * @param  Finder $finder
102
     * @param  Pages  $pages  Optional pages constraint used for every added pdf
103
     * @return void
104
     */
105
    public function addFinder(Finder $finder, Pages $pages = null)
106
    {
107
        foreach ($finder as $fileInfo) {
108
            $this->addFile($fileInfo->getRealpath(), $pages);
109
        }
110
    }
111
112
    /**
113
     * Merges your provided PDFs and get raw string
114
     *
115
     * A note on the $resetAfterMerge flag. Prior to version 3.1 the internal
116
     * state was always reset after merge. This behaviour is deprecated. In
117
     * version 4 the internal state will never be automatically reset. the
118
     * $resetAfterMerge flag can be used to mimic the comming behaviour
119
     *
120
     * @param  boolean   $resetAfterMerge Flag if internal state should reset after merge
121
     * @return string
122
     * @throws Exception On failure
123
     */
124
    public function merge($resetAfterMerge = true)
125
    {
126
        /** @var string Name of source being processed */
127
        $name = '';
128
129
        try {
130
            $fpdi = clone $this->fpdi;
131
132
            foreach ($this->sources as $source) {
133
                $name = $source->getName();
134
135
                /** @var int Total number of pages in pdf */
136
                $nrOfPagesInPdf = $fpdi->setSourceFile($source->getStreamReader());
137
138
                /** @var Pages The set of pages to merge, defaults to all pages */
139
                $pagesToMerge = $source->getPages()->hasPages() ? $source->getPages() : new Pages("1-$nrOfPagesInPdf");
140
141
                // Add specified pages
142
                foreach ($pagesToMerge as $pageNr) {
143
                    $template = $fpdi->importPage($pageNr);
144
                    $size = $fpdi->getTemplateSize($template);
145
                    $fpdi->AddPage(
146
                        $size['width'] > $size['height'] ? 'L' : 'P',
147
                        [$size['width'], $size['height']]
0 ignored issues
show
Documentation introduced by
array($size['width'], $size['height']) is of type array<integer,?,{"0":"?","1":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
148
                    );
149
                    $fpdi->useTemplate($template);
150
                }
151
            }
152
153
            if ($resetAfterMerge) {
154
                $this->reset();
155
            }
156
157
            return $fpdi->Output('', 'S');
158
159
        } catch (\Exception $e) {
160
            throw new Exception("'{$e->getMessage()}' in '{$name}'", 0, $e);
161
        }
162
    }
163
164
    /**
165
     * Reset internal state
166
     *
167
     * @return void
168
     */
169
    public function reset()
170
    {
171
        $this->sources = [];
172
    }
173
174
    /**
175
     * Create temporary file and return name
176
     *
177
     * @deprecated Since version 3.1
178
     */
179
    public function getTempFname()
180
    {
181
        trigger_error(
182
            'Use of getTempFname() is deprecated as temporare files are no longer created',
183
            E_USER_DEPRECATED
184
        );
185
186
        return tempnam($this->getTempDir(), "libmergepdf");
0 ignored issues
show
Deprecated Code introduced by
The method iio\libmergepdf\Merger::getTempDir() has been deprecated with message: Since version 3.1

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
187
    }
188
189
    /**
190
     * Get directory path for temporary files
191
     *
192
     * @deprecated Since version 3.1
193
     */
194
    public function getTempDir()
195
    {
196
        trigger_error(
197
            'Use of getTempDir() is deprecated as temporare files are no longer created',
198
            E_USER_DEPRECATED
199
        );
200
201
        return $this->tempDir ?: sys_get_temp_dir();
202
    }
203
204
    /**
205
     * Set directory path for temporary files
206
     *
207
     * @deprecated Since version 3.1
208
     */
209
    public function setTempDir($dirname)
210
    {
211
        trigger_error(
212
            'Use of setTempDir() is deprecated as temporare files are no longer created',
213
            E_USER_DEPRECATED
214
        );
215
216
        $this->tempDir = $dirname;
217
    }
218
}
219