TcpdiDriver::merge()   B
last analyzed

Complexity

Conditions 6
Paths 19

Size

Total Lines 30

Duplication

Lines 30
Ratio 100 %

Importance

Changes 0
Metric Value
cc 6
nc 19
nop 1
dl 30
loc 30
rs 8.8177
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace iio\libmergepdf\Driver;
6
7
use iio\libmergepdf\Exception;
8
use iio\libmergepdf\Source\SourceInterface;
9
10
final class TcpdiDriver implements DriverInterface
11
{
12
    /**
13
     * @var \TCPDI
14
     */
15
    private $tcpdi;
16
17
    public function __construct(\TCPDI $tcpdi = null)
18
    {
19
        $this->tcpdi = $tcpdi ?: new \TCPDI;
20
    }
21
22 View Code Duplication
    public function merge(SourceInterface ...$sources): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
    {
24
        $sourceName = '';
25
26
        try {
27
            $tcpdi = clone $this->tcpdi;
28
29
            foreach ($sources as $source) {
30
                $sourceName = $source->getName();
31
                $pageCount = $tcpdi->setSourceData($source->getContents());
32
                $pageNumbers = $source->getPages()->getPageNumbers() ?: range(1, $pageCount);
33
34
                foreach ($pageNumbers as $pageNr) {
35
                    $template = $tcpdi->importPage($pageNr);
36
                    $size = $tcpdi->getTemplateSize($template);
37
                    $tcpdi->SetPrintHeader(false);
38
                    $tcpdi->SetPrintFooter(false);
39
                    $tcpdi->AddPage(
40
                        $size['w'] > $size['h'] ? 'L' : 'P',
41
                        [$size['w'], $size['h']]
42
                    );
43
                    $tcpdi->useTemplate($template);
44
                }
45
            }
46
47
            return $tcpdi->Output('', 'S');
48
        } catch (\Exception $e) {
49
            throw new Exception("'{$e->getMessage()}' in '$sourceName'", 0, $e);
50
        }
51
    }
52
}
53