Passed
Push — master ( b37083...55746c )
by Andreas
02:28
created

Converter   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 158
rs 10
c 0
b 0
f 0
wmc 27

17 Methods

Rating   Name   Duplication   Size   Complexity  
A lastPage() 0 4 1
A create() 0 3 1
A scaleTo() 0 4 1
A page() 0 5 1
A __construct() 0 16 4
A executable() 0 4 1
A target() 0 4 1
A firstPage() 0 4 1
A source() 0 4 1
A format() 0 6 1
A command() 0 5 1
A convert() 0 10 2
A setOptions() 0 4 1
A normalizeFormat() 0 12 4
A extension() 0 4 1
A setExtensionFromFormat() 0 5 4
A addOption() 0 4 1
1
<?php
2
3
namespace Ottosmops\Pdftothumb;
4
5
use Symfony\Component\Process\Process;
6
7
use Ottosmops\Pdftothumb\Exceptions\CouldNotConvertPdf;
8
use Ottosmops\Pdftothumb\Exceptions\FileNotFound;
9
use Ottosmops\Pdftothumb\Exceptions\BinaryNotFound;
10
use Ottosmops\Pdftothumb\Exceptions\FileFormatNotAllowed;
11
12
13
class Converter
14
{
15
    public $executable = '';
16
17
    protected $options = [];
18
19
    protected $source = '';
20
21
    protected $target = '';
22
23
    protected $extension = '';
24
25
    // cf https://www.systutorials.com/docs/linux/man/1-pdftoppm/
26
    public $exitCodes = [
27
        0 => 'No error.',
28
        1 => 'Error opening a PDF file.',
29
        2 => 'Error opening an output file.',
30
        3 => 'Error related to PDF permissions.',
31
        99 => 'Other error.'
32
    ];
33
34
35
    public function __construct(string $source, string $target = '', $executable = '')
36
    {
37
        $this->executable = $executable ? $executable : 'pdftoppm';
38
        $this->format('jpeg');
39
        $this->extension('jpg');
40
        $this->page(1);
41
        $this->scaleTo(150);
42
43
        $this->source($source);
44
        if (!is_file($source)) {
45
            throw new FileNotFound("could not find pdf {$source}");
46
        }
47
        if (!$target) {
48
            $this->target = pathinfo($this->source, PATHINFO_DIRNAME).'/'.pathinfo($this->source, PATHINFO_FILENAME);
49
        } else {
50
            $this->target($target);
51
        }
52
53
    }
54
55
    public static function create(string $source, string $target = '', string $executable = '')
56
    {
57
        return (new static($source, $target, $executable));
58
    }
59
60
    public function executable(string $executable)
61
    {
62
        $this->executable = $executable;
63
        return $this;
64
    }
65
66
    protected function source(string $source)
67
    {
68
        $this->source = $source;
69
        return $this;
70
    }
71
72
    public function target(string $target = null)
73
    {
74
        $this->target = pathinfo($target, PATHINFO_DIRNAME) . '/' . pathinfo($target, PATHINFO_FILENAME);
75
        return $this;
76
    }
77
78
    public function scaleTo(int $scaleTo)
79
    {
80
        $this->options['scale-to'] = '-scale-to ' . (string) $scaleTo;
81
        return $this;
82
    }
83
84
    public function firstPage(int $firstPage)
85
    {
86
        $this->options['firstPage'] = '-f ' . (string) $firstPage;
87
        return $this;
88
    }
89
90
    public function lastPage(int $lastPage)
91
    {
92
        $this->options['lastPage'] = '-l ' . (string) $lastPage;
93
        return $this;
94
    }
95
96
    public function page(int $page)
97
    {
98
        $this->firstPage($page);
99
        $this->lastPage($page);
100
        return $this;
101
    }
102
103
    public function format(string $format)
104
    {
105
        $this->options['format'] = '-' . $this->normalizeFormat($format);
106
        $this->setExtensionFromFormat($format);
107
108
        return $this;
109
    }
110
111
    protected function setExtensionFromFormat(string $format)
0 ignored issues
show
Unused Code introduced by
The parameter $format is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

111
    protected function setExtensionFromFormat(/** @scrutinizer ignore-unused */ string $format)

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

Loading history...
112
    {
113
        $this->options['format'] == '-jpeg' && $this->extension('jpg');
114
        $this->options['format'] == '-tiff' && $this->extension('tif');
115
        $this->options['format'] == '-png' && $this->extension('png');
116
    }
117
118
    protected function normalizeFormat(string $format)
119
    {
120
        $format = strtolower($format);
121
        $format = $format == 'jpg' ? 'jpeg' : $format;
122
        $format = $format == 'tif' ? 'tiff' : $format;
123
124
        $formats = ['jpeg', 'png', 'tiff'];
125
126
        if (!in_array($format, $formats)) {
127
            throw new FileFormatNotAllowed("fileformat not allowed {$format}");
128
        }
129
        return $format;
130
    }
131
132
    protected function extension($extension = null)
133
    {
134
        $this->extension = $extension;
135
        return $this;
136
    }
137
138
    public function addOption(string $options)
139
    {
140
        $this->options[] = $options;
141
        return $this;
142
    }
143
144
    public function setOptions(string $options)
145
    {
146
        $this->options = [$options];
147
        return $this;
148
    }
149
150
    public function command()
151
    {
152
        $options = join(' ', $this->options);
153
        $command = "{$this->executable} {$options} '{$this->source}' > '{$this->target}.{$this->extension}'";
154
        return $command;
155
    }
156
157
    /**
158
     * extract text
159
     * @return string
160
     */
161
    public function convert()
162
    {
163
        $process = new Process($this->command());
164
        $process->run();
165
166
        if (!$process->isSuccessful()) {
167
            throw new CouldNotConvertPdf($process);
168
        }
169
170
        return $process->getExitCode();
171
    }
172
}
173