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.

Pdf   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 79
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setPdf() 0 10 2
A setOptions() 0 6 1
A addOptions() 0 9 1
A parseOptions() 0 17 2
A text() 0 10 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\PdfNotFound;
7
use Symfony\Component\Process\Process;
8
9
class Pdf
10
{
11
    protected $pdf;
12
13
    protected $binPath;
14
15
    protected $options = [];
16
17
    public function __construct(string $binPath = null)
18
    {
19
        $this->binPath = $binPath ?? '/usr/bin/pdftotext';
20
    }
21
22
    public function setPdf(string $pdf): self
23
    {
24
        if (!is_readable($pdf)) {
25
            throw new PdfNotFound("Could not read `{$pdf}`");
26
        }
27
28
        $this->pdf = $pdf;
29
30
        return $this;
31
    }
32
33
    public function setOptions(array $options): self
34
    {
35
        $this->options = $this->parseOptions($options);
36
37
        return $this;
38
    }
39
40
    public function addOptions(array $options): self
41
    {
42
        $this->options = array_merge(
43
            $this->options,
44
            $this->parseOptions($options)
45
        );
46
47
        return $this;
48
    }
49
50
    protected function parseOptions(array $options): array
51
    {
52
        $mapper = function (string $content) : array {
53
            $content = trim($content);
54
            if ('-' !== ($content[0] ?? '')) {
55
                $content = '-'.$content;
56
            }
57
58
            return explode(' ', $content, 2);
59
        };
60
61
        $reducer = function (array $carry, array $option) : array {
62
            return array_merge($carry, $option);
63
        };
64
65
        return array_reduce(array_map($mapper, $options), $reducer, []);
66
    }
67
68
    public function text() : string
69
    {
70
        $process = new Process(array_merge([$this->binPath], $this->options, [$this->pdf, '-']));
71
        $process->run();
72
        if (!$process->isSuccessful()) {
73
            throw new CouldNotExtractText($process);
74
        }
75
76
        return trim($process->getOutput(), " \t\n\r\0\x0B\x0C");
77
    }
78
79
    public static function getText(string $pdf, string $binPath = null, array $options = []) : string
80
    {
81
        return (new static($binPath))
82
            ->setOptions($options)
83
            ->setPdf($pdf)
84
            ->text()
85
        ;
86
    }
87
}
88