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
Push — master ( 44690f...18bf17 )
by Freek
08:38
created

GlideConversion::useImageDriver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Spatie\Image;
4
5
use Exception;
6
use League\Glide\Server;
7
use League\Glide\ServerFactory;
8
9
final class GlideConversion
10
{
11
    /** @var string */
12
    protected $inputImage;
13
14
    /** @var string */
15
    protected $imageDriver = 'gd';
16
17
    /** @var string */
18
    protected $conversionResult = null;
19
20
    public static function create(string $inputImage): self
21
    {
22
        return new static($inputImage);
23
    }
24
25
    public function __construct(string $inputImage)
26
    {
27
        $this->inputImage = $inputImage;
28
    }
29
30
    public function useImageDriver(string $imageDriver): self
31
    {
32
        $this->imageDriver = $imageDriver;
33
34
        return $this;
35
    }
36
37
    public function performManipulations(Manipulations $manipulations)
38
    {
39
        foreach ($manipulations->getManipulationSets() as $manipulations) {
40
            $inputFile = $this->conversionResult ?? $this->inputImage;
41
42
            $glideServer = $this->createGlideServer($inputFile);
43
44
            $this->conversionResult = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $glideServer->makeImage(
45
                    pathinfo($inputFile, PATHINFO_BASENAME),
46
                    $this->prepareManipulations($manipulations)
47
                );
48
        }
49
50
        return $this;
51
    }
52
53
    protected function createGlideServer($inputFile): Server
54
    {
55
        return ServerFactory::create([
56
            'source' => dirname($inputFile),
57
            'cache' => sys_get_temp_dir(),
58
            'driver' => $this->imageDriver,
59
        ]);
60
    }
61
62
    public function save(string $outputFile)
63
    {
64
        if ($this->conversionResult == '') {
65
            copy($this->inputImage, $outputFile);
66
67
            return;
68
        }
69
70
        rename($this->conversionResult, $outputFile);
71
    }
72
73
    protected function prepareManipulations(array $manipulations): array
74
    {
75
        $glideManipulations = [];
76
77
        foreach ($manipulations as $operation => $argument) {
78
            $glideManipulations[$this->convertToGlideParameter($operation)] = $argument;
79
        }
80
81
        return $glideManipulations;
82
    }
83
84
    protected function convertToGlideParameter(string $manipulationFunctionName): string
85
    {
86
        $conversions = [
87
            'width' => 'w',
88
            'height' => 'h',
89
            'blur' => 'blur',
90
            'pixelate' => 'pixel',
91
            'crop' => 'fit',
92
            'orientation' => 'orientation',
93
            'fit' => 'fit',
94
            'devicePixelRatio' => 'dpr',
95
            'brightness' => 'bri',
96
            'contrast' => 'con',
97
            'gamma' => 'gam',
98
            'sharpen' => 'sharp',
99
            'filter' => 'filt',
100
            'background' => 'bg',
101
            'border' => 'border',
102
            'quality' => 'q',
103
            'format' => 'fm',
104
        ];
105
106
        if (!isset($conversions[$manipulationFunctionName])) {
107
            throw new Exception('Unknown');
108
        }
109
110
        return $conversions[$manipulationFunctionName];
111
    }
112
}
113