Completed
Push — master ( 8aa398...a4bd52 )
by Andreas
02:27
created

Validator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 90.32%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 69
ccs 28
cts 31
cp 0.9032
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A checkExecutable() 0 10 2
A is_pdf() 0 5 1
B check() 0 24 4
1
<?php
2
3
namespace Ottosmops\Pdfvalidate;
4
5
use Ottosmops\Pdfvalidate\Exceptions\PdfNotValide;
6
use Ottosmops\Pdfvalidate\Exceptions\FileNotFound;
7
use Ottosmops\Pdfvalidate\Exceptions\BinaryNotFound;
8
use Ottosmops\Pdfvalidate\Exceptions\MimeTypeIsNotPdf;
9
use Symfony\Component\Process\Process;
10
11
class Validator {
12
13
    public $file;
14
15
    public $error = '';
16
17
    public $process = '';
18
19
    public $output;
20
21
    public $executable;
22
23 3
    public function __construct($file, $executable = 'pdftocairo')
24
    {
25 3
        $this->executable = $executable;
26 3
        $this->checkExecutable();
27 3
        if (!is_file($file)) {
28
            throw new FileNotFound($file);
29
        }
30 3
        if (!self::is_pdf($file)) {
31
            throw new MimeTypeIsNotPdf($file);
32
        }
33 3
        $this->file = $file;
34 3
    }
35
36 3
    public function checkExecutable()
37
    {
38 3
        $process = new Process('which ' . $this->executable);
39 3
        $process->run();
40 3
        if (!$process->isSuccessful()) {
41
            throw new BinaryNotFound($process);
42
        }
43
44 3
        return true;
45
    }
46
47
48 3
    public static function is_pdf($file)
49
    {
50 3
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
51 3
        return finfo_file($finfo, $file) == 'application/pdf';
52
    }
53
54 3
    public function check()
55
    {
56 3
        $command = $this->executable . ' -pdf '. $this->file .' - 2>&1 >/dev/null';
57
58 3
        $process = new Process($command);
59 3
        $process->run();
60
61 3
        $output = $process->getOutput();
62
63 3
        if (!$output) {
64 1
            return true;
65
        };
66
67 2
        if (mb_strpos($output, 'Error') !== false) {
68 2
            $this->error = "Could open the PDF, but the PDF seems to be corrupted.";
69
        }
70
71 2
        if (mb_strpos($output, 'Error opening') !== false) {
72 1
            $this->error = "Could not open the PDF.";
73
        }
74
75 2
        $this->output = $output;
76 2
        return false;
77
    }
78
79
}
80