Passed
Pull Request — master (#1)
by Artem
02:54 queued 01:12
created

ImageOptimizer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ImageOptimizer;
6
7
use Imagine\Image\{Box, ImageInterface};
8
use ImageOptimizer\Exception\ImageNotFoundException;
9
use ImageOptimizer\Exception\DirectoryNotFoundException;
10
11
class ImageOptimizer
12
{
13
    /**
14
     * @var ImagineDriver $imagine
15
     */
16
    protected $driver;
17
18
    /**
19
     * @var string $appPath
20
     */
21
    protected $appPath;
22
23
    public function __construct(ImagineDriver $driver, string $resizedFolder, string $appPath)
24
    {
25
        $this->driver = $driver;
26
        $this->resizedFolder = $resizedFolder;
0 ignored issues
show
Bug Best Practice introduced by
The property resizedFolder does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
27
        $this->appPath = $appPath;
28
    }
29
30
    /**
31
     * Optimizes image
32
     *
33
     * @access	public
34
     * @param	string	$path  	
35
     * @param	int   	$height	
36
     * @param	int   	$width 	
37
     * @return	ImageOptimized
38
     */
39
    public function getOptimizedImage(string $path, int $height, int $width): ImageOptimized
40
    {
41
        $relativePath = parse_url($path, PHP_URL_PATH);
42
43
        $pathinfo = pathinfo($relativePath);
44
45
        $absolutePath = $this->appPath . $path;
46
47
        if (! $this->fileExists($absolutePath)) {
48
            throw new ImageNotFoundException($absolutePath);
0 ignored issues
show
Unused Code introduced by
The call to ImageOptimizer\Exception...xception::__construct() has too many arguments starting with $absolutePath. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
            throw /** @scrutinizer ignore-call */ new ImageNotFoundException($absolutePath);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
49
        }
50
51
        $resizedUrl = $pathinfo['dirname'] . $pathinfo['filename'] . '_' . $height . 'x' . $width . '.' . $pathinfo['extension'];
52
53
        $resizedFile = $this->getBasePath() . $resizedUrl;
54
55
        $optimizedImage = new ImageOptimized([
56
            'original_url' => $path,
57
            'resized_url' => $this->resizedFolder . $resizedUrl,
58
            'resized_webp_url' => $this->resizedFolder . $resizedUrl . '.webp',
59
        ]);
60
61
        if (! $this->fileExists($resizedFile)) {
62
            $this->ensureDirectoryExists($resizedFile);
63
            $this->driver->process($absolutePath, $width, $height, $resizedFile);
64
        }
65
66
        return $optimizedImage;
67
    }
68
69
    /**
70
     * @param	string	$path	
71
     * @return	void
72
     */
73
    protected function ensureDirectoryExists(string $path): void
74
    {
75
        $directory = dirname($path);
76
77
        if (! $this->fileExists($directory)) {
78
            if (! $this->mkdir($directory)) {
79
                throw new DirectoryNotFoundException();
80
            }
81
        }
82
    }
83
84
    /**
85
     * file_exists wrapper
86
     *
87
     * @access	protected
88
     * @param	string	$path	
89
     * @return	bool
90
     */
91
    protected function fileExists(string $path): bool
92
    {
93
        return file_exists($path);
94
    }
95
96
    /**
97
     * mkdir wraper
98
     *
99
     * @access	protected
100
     * @param	string	$directory	
101
     * @return	bool
102
     */
103
    protected function mkdir(string $directory): bool
104
    {
105
        return mkdir($directory, 0755, true);
106
    }
107
108
    /**
109
     * @return	string
110
     */
111
    protected function getBasePath(): string
112
    {
113
        return $this->appPath . $this->resizedFolder;
114
    }
115
}
116