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 (#66)
by
unknown
01:15
created

Image::setTemporaryDirectory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Spatie\Image;
4
5
use BadMethodCallException;
6
use Spatie\Image\Exceptions\InvalidImageDriver;
7
use Spatie\ImageOptimizer\OptimizerChainFactory;
8
use Intervention\Image\ImageManagerStatic as InterventionImage;
9
10
/** @mixin \Spatie\Image\Manipulations */
11
class Image
12
{
13
    /** @var string */
14
    protected $pathToImage;
15
16
    /** @var \Spatie\Image\Manipulations */
17
    protected $manipulations;
18
19
    protected $imageDriver = 'gd';
20
21
    protected static $temporaryDirectory = null;
22
23
    /**
24
     * @param string $pathToImage
25
     *
26
     * @return static
27
     */
28
    public static function load(string $pathToImage)
29
    {
30
        return new static($pathToImage);
31
    }
32
33
    public static function setTemporaryDirectory($tempDir)
34
    {
35
        self::$temporaryDirectory = $tempDir;
36
    }
37
38
    public function __construct(string $pathToImage)
39
    {
40
        $this->pathToImage = $pathToImage;
41
        $this->manipulations = new Manipulations();
42
        if (! isset(self::$temporaryDirectory)) {
43
            self::setTemporaryDirectory(sys_get_temp_dir());
44
        }
45
    }
46
47
    /**
48
     * @param string $imageDriver
49
     *
50
     * @return $this
51
     *
52
     * @throws InvalidImageDriver
53
     */
54
    public function useImageDriver(string $imageDriver)
55
    {
56
        if (! in_array($imageDriver, ['gd', 'imagick'])) {
57
            throw InvalidImageDriver::driver($imageDriver);
58
        }
59
60
        $this->imageDriver = $imageDriver;
61
62
        InterventionImage::configure([
63
            'driver' => $this->imageDriver,
64
        ]);
65
66
        return $this;
67
    }
68
69
    /**
70
     * @param callable|$manipulations
71
     *
72
     * @return $this
73
     */
74
    public function manipulate($manipulations)
75
    {
76
        if (is_callable($manipulations)) {
77
            $manipulations($this->manipulations);
78
        }
79
80
        if ($manipulations instanceof Manipulations) {
81
            $this->manipulations->mergeManipulations($manipulations);
0 ignored issues
show
Documentation introduced by
$manipulations is of type object<Spatie\Image\Manipulations>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82
        }
83
84
        return $this;
85
    }
86
87
    public function __call($name, $arguments)
88
    {
89
        if (! method_exists($this->manipulations, $name)) {
90
            throw new BadMethodCallException("Manipulation `{$name}` does not exist");
91
        }
92
93
        $this->manipulations->$name(...$arguments);
94
95
        return $this;
96
    }
97
98
    public function getWidth(): int
99
    {
100
        return InterventionImage::make($this->pathToImage)->width();
101
    }
102
103
    public function getHeight(): int
104
    {
105
        return InterventionImage::make($this->pathToImage)->height();
106
    }
107
108
    public function getManipulationSequence(): ManipulationSequence
109
    {
110
        return $this->manipulations->getManipulationSequence();
111
    }
112
113
    public function save($outputPath = '')
114
    {
115
        if ($outputPath == '') {
116
            $outputPath = $this->pathToImage;
117
        }
118
119
        $this->addFormatManipulation($outputPath);
120
121
        GlideConversion::create($this->pathToImage)
122
            ->setTemporaryDirectory(self::$temporaryDirectory)
123
            ->useImageDriver($this->imageDriver)
124
            ->performManipulations($this->manipulations)
125
            ->save($outputPath);
126
127
        if ($this->shouldOptimize()) {
128
            $optimizerChainConfiguration = $this->manipulations->getFirstManipulationArgument('optimize');
129
130
            $optimizerChainConfiguration = json_decode($optimizerChainConfiguration, true);
131
132
            $this->performOptimization($outputPath, $optimizerChainConfiguration);
133
        }
134
    }
135
136
    protected function shouldOptimize(): bool
137
    {
138
        return ! is_null($this->manipulations->getFirstManipulationArgument('optimize'));
139
    }
140
141
    protected function performOptimization($path, array $optimizerChainConfiguration)
142
    {
143
        $optimizerChain = OptimizerChainFactory::create();
144
145
        if (count($optimizerChainConfiguration)) {
146
            $optimizers = array_map(function (array $optimizerOptions, string $optimizerClassName) {
147
                return (new $optimizerClassName)->setOptions($optimizerOptions);
148
            }, $optimizerChainConfiguration, array_keys($optimizerChainConfiguration));
149
150
            $optimizerChain->setOptimizers($optimizers);
151
        }
152
153
        $optimizerChain->optimize($path);
154
    }
155
156
    protected function addFormatManipulation($outputPath)
157
    {
158
        if ($this->manipulations->hasManipulation('format')) {
159
            return;
160
        }
161
162
        $inputExtension = strtolower(pathinfo($this->pathToImage, PATHINFO_EXTENSION));
163
        $outputExtension = strtolower(pathinfo($outputPath, PATHINFO_EXTENSION));
164
165
        if ($inputExtension === $outputExtension) {
166
            return;
167
        }
168
169
        $supportedFormats = ['jpg', 'png', 'gif'];
170
171
        if (in_array($outputExtension, $supportedFormats)) {
172
            $this->manipulations->format($outputExtension);
173
        }
174
    }
175
}
176