Completed
Push — master ( 4dc0e8...439184 )
by Hannes
22s queued 18s
created

TcpdiDriver::merge()   B

Complexity

Conditions 6
Paths 15

Size

Total Lines 28

Duplication

Lines 28
Ratio 100 %

Importance

Changes 0
Metric Value
cc 6
nc 15
nop 1
dl 28
loc 28
rs 8.8497
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
        // TODO A stupid hack to hide deprecation notice
20
        @each($arr = []);
0 ignored issues
show
Bug introduced by
$arr = array() cannot be passed to each() as the parameter $array expects a reference.
Loading history...
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
21
22
        $this->tcpdi = $tcpdi ?: new \TCPDI;
23
    }
24
25 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...
26
    {
27
        $sourceName = '';
28
29
        try {
30
            $tcpdi = clone $this->tcpdi;
31
32
            foreach ($sources as $source) {
33
                $sourceName = $source->getName();
34
                $pageCount = $tcpdi->setSourceData($source->getContents());
35
                $pageNumbers = $source->getPages()->getPageNumbers() ?: range(1, $pageCount);
36
37
                foreach ($pageNumbers as $pageNr) {
38
                    $template = $tcpdi->importPage($pageNr);
39
                    $size = $tcpdi->getTemplateSize($template);
40
                    $tcpdi->AddPage(
41
                        $size['w'] > $size['h'] ? 'L' : 'P',
42
                        [$size['w'], $size['h']]
43
                    );
44
                    $tcpdi->useTemplate($template);
45
                }
46
            }
47
48
            return $tcpdi->Output('', 'S');
49
        } catch (\Exception $e) {
50
            throw new Exception("'{$e->getMessage()}' in '$sourceName'", 0, $e);
51
        }
52
    }
53
}
54