Completed
Pull Request — master (#54)
by Jolita
10:19
created

GlideImage::getWatermarkParameters()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 16
rs 9.4285
cc 2
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace Spatie\Glide;
4
5
use League\Glide\ServerFactory;
6
use Spatie\Glide\Exceptions\SourceFileDoesNotExist;
7
use League\Flysystem\Filesystem;
8
use League\Flysystem\Adapter\Local;
9
10
class GlideImage
11
{
12
    /**
13
     * @var string The path to the input image.
14
     */
15
    protected $sourceFile;
16
17
    /**
18
     * @var array The modification the need to be made on the image.
19
     *            Take a look at Glide's image API to see which parameters are possible.
20
     *            http://glide.thephpleague.com/1.0/api/quick-reference/
21
     */
22
    protected $modificationParameters = [];
23
24
    public static function create(string $sourceFile) : GlideImage
25
    {
26
        return (new static())->setSourceFile($sourceFile);
27
    }
28
29
    public function setSourceFile(string $sourceFile) : GlideImage
30
    {
31
        if (!file_exists($sourceFile)) {
32
            throw new SourceFileDoesNotExist();
33
        }
34
35
        $this->sourceFile = $sourceFile;
36
37
        return $this;
38
    }
39
40
    public function modify(array $modificationParameters) : GlideImage
41
    {
42
        $this->modificationParameters = $modificationParameters;
43
44
        return $this;
45
    }
46
47
    public function save(string $outputFile) : string
48
    {
49
        $sourceFileName = pathinfo($this->sourceFile, PATHINFO_BASENAME);
50
51
        $cacheDir = sys_get_temp_dir();
52
53
        if (array_has($this->modificationParameters, 'mark')) {
54
            list($watermarksFolder, $modificationParameters) = $this->getWatermarkParameters();
55
        }
56
57
        $glideServer = ServerFactory::create([
58
            'source' => dirname($this->sourceFile),
59
            'cache' => $cacheDir,
60
            'driver' => config('laravel-glide.driver'),
61
            'watermarks' => !empty($watermarksFolder) ? new Filesystem(new Local($watermarksFolder)) : null,
62
        ]);
63
64
        $conversionResult = $cacheDir.'/'.$glideServer->makeImage($sourceFileName, $modificationParameters ?? $this->modificationParameters);
0 ignored issues
show
Bug introduced by
The variable $modificationParameters does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
65
66
        rename($conversionResult, $outputFile);
67
68
        return $outputFile;
69
    }
70
71
    protected function getWatermarkParameters() : array
72
    {
73
        $pathParts = pathinfo($this->modificationParameters['mark']);
74
75
        $modificationParameters = collect($this->modificationParameters)->map(function ($item, $key) use ($pathParts) {
76
77
            if ($key === 'mark') {
78
                $item = $pathParts['basename'];
79
            }
80
81
            return $item;
82
83
        })->toArray();
84
85
        return [$pathParts['dirname'], $modificationParameters];
86
    }
87
}
88