Passed
Push — master ( b5ff85...15db63 )
by Artem
02:57 queued 11s
created

ImageOptimizer::getBasePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ImageOptimizer;
6
7
use ImageOptimizer\Exception\ImageNotFoundException;
8
use ImageOptimizer\Exception\DirectoryNotFoundException;
9
10
class ImageOptimizer
11
{
12
    /**
13
     * @var DriverInterface $imagine
14
     */
15
    protected $driver;
16
17
    /**
18
     * @var string $appPath
19
     */
20
    protected $appPath;
21
22 4
    public function __construct(DriverInterface $driver, string $resizedFolder, string $appPath)
23
    {
24 4
        $this->driver = $driver;
25 4
        $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...
26 4
        $this->appPath = $appPath;
27 4
    }
28
29
    /**
30
     * Optimizes image
31
     *
32
     * @access	public
33
     * @param	string	$path  	
34
     * @param	int   	$height	
35
     * @param	int   	$width 	
36
     * @return	ImageOptimized
37
     */
38 4
    public function getOptimizedImage(string $path, int $height, int $width): ImageOptimized
39
    {
40 4
        $relativePath = parse_url($path, PHP_URL_PATH);
41
42 4
        $pathinfo = pathinfo($relativePath);
43
44 4
        $absolutePath = $this->appPath . $path;
45
46 4
        if (! $this->fileExists($absolutePath)) {
47 1
            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

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