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.

Ghostscript   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 40
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A regeneratePdf() 0 15 1
A __construct() 0 3 1
A tempFile() 0 3 1
1
<?php
2
3
namespace ProtoneMedia\LaravelMixins\Pdf;
4
5
use Symfony\Component\Process\Process;
6
7
class Ghostscript implements CanRegeneratePDF
8
{
9
    /**
10
     * Ghostscript binary path.
11
     */
12
    private string $bin;
13
14
    public function __construct(string $bin = 'ghostscript')
15
    {
16
        $this->bin = $bin;
17
    }
18
19
    /**
20
     * Generates a temporary filename for a PDF file.
21
     *
22
     * @return string
23
     */
24
    private static function tempFile(): string
25
    {
26
        return tempnam(sys_get_temp_dir(), 'ghostscript') . '.pdf';
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function regeneratePdf(string $pdfContents): string
33
    {
34
        file_put_contents($input = static::tempFile(), $pdfContents);
35
36
        (new Process([
37
            $this->bin,
38
            '-sDEVICE=pdfwrite',
39
            '-dPDFSETTINGS=/prepress',
40
            '-sOutputFile=' . $destination = static::tempFile(),
41
            $input,
42
        ]))->run();
43
44
        return tap(@file_get_contents($destination), function () use ($input, $destination) {
45
            @unlink($input);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

45
            /** @scrutinizer ignore-unhandled */ @unlink($input);

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...
46
            @unlink($destination);
47
        });
48
    }
49
}
50