Completed
Push — master ( f25143...c4b850 )
by Freek
02:26
created

GlideImage   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 16
Bugs 3 Features 2
Metric Value
wmc 6
c 16
b 3
f 2
lcom 1
cbo 3
dl 0
loc 64
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A setSourceFile() 0 10 2
A modify() 0 6 1
B save() 0 26 2
1
<?php
2
3
namespace Spatie\Glide;
4
5
use League\Glide\ServerFactory;
6
use Spatie\Glide\Exceptions\SourceFileDoesNotExist;
7
8
class GlideImage
9
{
10
    /**
11
     * @var string The path to the input image.
12
     */
13
    protected $sourceFile;
14
15
    /**
16
     * @var array The modification the need to be made on the image.
17
     *            Take a look at Glide's image API to see which parameters are possible.
18
     *            http://glide.thephpleague.com/1.0/api/quick-reference/
19
     */
20
    protected $modificationParameters = [];
21
22
    public static function create(string $sourceFile) : GlideImage
23
    {
24
        return (new static())->setSourceFile($sourceFile);
25
    }
26
27
    public function setSourceFile(string $sourceFile) : GlideImage
28
    {
29
        if (!file_exists($sourceFile)) {
30
            throw new SourceFileDoesNotExist();
31
        }
32
33
        $this->sourceFile = $sourceFile;
34
35
        return $this;
36
    }
37
38
    public function modify(array $modificationParameters) : GlideImage
39
    {
40
        $this->modificationParameters = $modificationParameters;
41
42
        return $this;
43
    }
44
45
    public function save(string $outputFile) : string
46
    {
47
        $sourceFileName = pathinfo($this->sourceFile, PATHINFO_BASENAME);
48
49
        $cacheDir = sys_get_temp_dir();
50
51
        $glideServerParameters = [
52
            'source' => dirname($this->sourceFile),
53
            'cache' => $cacheDir,
54
            'driver' => config('laravel-glide.driver'),
55
        ];
56
57
        if (isset($this->modificationParameters['mark'])) {
58
            $watermarkPathInfo = pathinfo($this->modificationParameters['mark']);
59
            $glideServerParameters['watermarks'] = $watermarkPathInfo['dirname'];
60
            $this->modificationParameters['mark'] = $watermarkPathInfo['basename'];
61
        }
62
63
        $glideServer = ServerFactory::create($glideServerParameters);
64
65
        $conversionResult = $cacheDir.'/'.$glideServer->makeImage($sourceFileName, $modificationParameters ?? $this->modificationParameters);
0 ignored issues
show
Bug introduced by
The variable $modificationParameters does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
66
67
        rename($conversionResult, $outputFile);
68
69
        return $outputFile;
70
    }
71
}
72