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 (#10)
by
unknown
02:36
created

GlideConversion::extractWatermarkPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Spatie\Image;
4
5
use League\Glide\Server;
6
use League\Glide\ServerFactory;
7
use Spatie\Image\Exceptions\CouldNotConvert;
8
9
/** @private */
10
final class GlideConversion
11
{
12
    /** @var string */
13
    private $inputImage;
14
15
    /** @var string */
16
    private $imageDriver = 'gd';
17
18
    /** @var string */
19
    private $conversionResult = null;
20
21
    public static function create(string $inputImage): self
22
    {
23
        return new self($inputImage);
24
    }
25
26
    public function __construct(string $inputImage)
27
    {
28
        $this->inputImage = $inputImage;
29
    }
30
31
    public function useImageDriver(string $imageDriver): self
32
    {
33
        $this->imageDriver = $imageDriver;
34
35
        return $this;
36
    }
37
38
    public function performManipulations(Manipulations $manipulations)
39
    {
40
        foreach ($manipulations->getManipulationSequence() as $manipulationGroup) {
41
            $inputFile = $this->conversionResult ?? $this->inputImage;
42
43
            $watermarkPath = $this->extractWatermarkPath($manipulationGroup);
44
45
            $glideServer = $this->createGlideServer($inputFile, $watermarkPath);
46
47
            $this->conversionResult = sys_get_temp_dir().DIRECTORY_SEPARATOR.$glideServer->makeImage(
48
                    pathinfo($inputFile, PATHINFO_BASENAME),
49
                    $this->prepareManipulations($manipulationGroup)
50
                );
51
        }
52
53
        return $this;
54
    }
55
56
    /**
57
     * Removes the watermark path from the manipulationGroup and returns it. This way it can be injected into the Glide
58
     * server as the `watermarks` path.
59
     *
60
     * @param $manipulationGroup
61
     *
62
     * @return null|string
63
     */
64
    private function extractWatermarkPath(&$manipulationGroup)
65
    {
66
        if (array_key_exists('watermark', $manipulationGroup)) {
67
            $watermarkPath = dirname($manipulationGroup['watermark']);
68
69
            $manipulationGroup['watermark'] = basename($manipulationGroup['watermark']);
70
71
            return $watermarkPath;
72
        }
73
    }
74
75
    private function createGlideServer($inputFile, string $watermarkPath = null): Server
76
    {
77
        $config = [
78
            'source' => dirname($inputFile),
79
            'cache' => sys_get_temp_dir(),
80
            'driver' => $this->imageDriver,
81
        ];
82
83
        if ($watermarkPath) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $watermarkPath of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
84
            $config['watermarks'] = $watermarkPath;
85
        }
86
87
        return ServerFactory::create($config);
88
    }
89
90
    public function save(string $outputFile)
91
    {
92
        if ($this->conversionResult == '') {
93
            copy($this->inputImage, $outputFile);
94
95
            return;
96
        }
97
98
        rename($this->conversionResult, $outputFile);
99
    }
100
101
    private function prepareManipulations(array $manipulationGroup): array
102
    {
103
        $glideManipulations = [];
104
105
        foreach ($manipulationGroup as $name => $argument) {
106
            $glideManipulations[$this->convertToGlideParameter($name)] = $argument;
107
        }
108
109
        return $glideManipulations;
110
    }
111
112
    private function convertToGlideParameter(string $manipulationName): string
113
    {
114
        $conversions = [
115
            'width' => 'w',
116
            'height' => 'h',
117
            'blur' => 'blur',
118
            'pixelate' => 'pixel',
119
            'crop' => 'fit',
120
            'manualCrop' => 'crop',
121
            'orientation' => 'or',
122
            'fit' => 'fit',
123
            'devicePixelRatio' => 'dpr',
124
            'brightness' => 'bri',
125
            'contrast' => 'con',
126
            'gamma' => 'gam',
127
            'sharpen' => 'sharp',
128
            'filter' => 'filt',
129
            'background' => 'bg',
130
            'border' => 'border',
131
            'quality' => 'q',
132
            'format' => 'fm',
133
            'watermark' => 'mark',
134
            'watermarkWidth' => 'markw',
135
            'watermarkHeight' => 'markh',
136
            'watermarkFit' => 'markfit',
137
            'watermarkPaddingX' => 'markx',
138
            'watermarkPaddingY' => 'marky',
139
            'watermarkPosition' => 'markpos',
140
            'watermarkOpacity' => 'markalpha',
141
        ];
142
143
        if (! isset($conversions[$manipulationName])) {
144
            throw CouldNotConvert::unknownManipulation($manipulationName);
145
        }
146
147
        return $conversions[$manipulationName];
148
    }
149
}
150