GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#11)
by ignace nyamagana
01:17
created

Pdf   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 63
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setPdf() 0 10 2
A setOptions() 0 12 4
A text() 0 16 2
A getText() 0 8 1
1
<?php
2
3
namespace Spatie\PdfToText;
4
5
use Spatie\PdfToText\Exceptions\CouldNotExtractText;
6
use Spatie\PdfToText\Exceptions\InvalidOption;
7
use Spatie\PdfToText\Exceptions\PdfNotFound;
8
use Symfony\Component\Process\Process;
9
10
class Pdf
11
{
12
    protected $pdf;
13
14
    protected $binPath;
15
16
    protected $options = [];
17
18
    public function __construct(string $binPath = null)
19
    {
20
        $this->binPath = $binPath ?? '/usr/bin/pdftotext';
21
    }
22
23
    public function setPdf(string $pdf) : self
24
    {
25
        if (!\is_readable($pdf)) {
26
            throw new PdfNotFound(sprintf('could not find pdf `%s` or is not readable', $pdf));
27
        }
28
29
        $this->pdf = $pdf;
30
31
        return $this;
32
    }
33
34
    public function setOptions(array $options) : self
35
    {
36
        foreach ($options as $value) {
37
            if (!\is_string($value) || '-' !== $value[0] ?? '') {
38
                throw new InvalidOption('The options array contains invalid value');
39
            }
40
        }
41
42
        $this->options = \array_unique($options);
43
44
        return $this;
45
    }
46
47
    public function text() : string
48
    {
49
        $arguments = $this->options;
50
        $arguments[] = $this->pdf;
51
        $arguments[] = '-';
52
        \array_unshift($arguments, $this->binPath);
53
54
        $commandline = implode(' ', array_map('escapeshellarg', $arguments));
55
        $process = new Process($commandline);
56
        $process->run();
57
        if (!$process->isSuccessful()) {
58
            throw new CouldNotExtractText($process);
59
        }
60
61
        return trim($process->getOutput(), " \t\n\r\0\x0B\x0C");
62
    }
63
64
    public static function getText(string $pdf, string $binPath = null, array $options = []) : string
65
    {
66
        return (new static($binPath))
67
            ->setOptions($options)
68
            ->setPdf($pdf)
69
            ->text()
70
        ;
71
    }
72
}
73