Merge   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 2
Metric Value
wmc 6
eloc 25
c 3
b 1
f 2
dl 0
loc 71
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addWatermark() 0 9 1
A render() 0 18 4
A __construct() 0 5 1
1
<?php
2
3
namespace TarfinLabs\EasyPdf;
4
5
use setasign\Fpdi\PdfParser\StreamReader;
6
7
class Merge extends BasePdf
8
{
9
    /**
10
     * @var array
11
     */
12
    protected $files;
13
14
    protected $watermark;
15
    protected $w_x;
16
    protected $w_y;
17
    protected $w_width;
18
    protected $w_height;
19
20
    /**
21
     * Merge constructor.
22
     *
23
     * @param  array  $files
24
     */
25
    public function __construct(array $files)
26
    {
27
        parent::__construct();
28
29
        $this->files = $files;
30
    }
31
32
    /**
33
     * Add watermark to each page.
34
     *
35
     * @param  $watermark
36
     * @param  $x
37
     * @param  $y
38
     * @param  $width
39
     * @param  $height
40
     * @return $this
41
     */
42
    public function addWatermark($watermark, $x = null, $y = null, $width = 0, $height = 0)
43
    {
44
        $this->watermark = $watermark;
45
        $this->w_x = $x;
46
        $this->w_y = $y;
47
        $this->w_width = $width;
48
        $this->w_height = $height;
49
50
        return $this;
51
    }
52
53
    /**
54
     * Merge pdf files into one pdf.
55
     *
56
     * @return $this
57
     *
58
     * @throws Exceptions\UnableToOpen
59
     */
60
    public function render()
61
    {
62
        foreach ($this->files as $file) {
63
            $this->setFileContent($file);
64
65
            $count = $this->pdf->setSourceFile(StreamReader::createByString($this->content));
66
67
            for ($page = 1; $page <= $count; $page++) {
68
                $this->pdf->AddPage();
69
                $importedPage = $this->pdf->ImportPage($page);
70
                $this->pdf->useTemplate($importedPage);
71
                if ($this->watermark) {
72
                    $this->pdf->Image($this->watermark, $this->w_x, $this->w_y, $this->w_width, $this->w_height);
73
                }
74
            }
75
        }
76
77
        return $this;
78
    }
79
}
80