ImageHandler   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 52
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D handleQueue() 0 34 9
1
<?php
2
3
namespace Dumkaaa\BxOptimize\Handler;
4
5
class ImageHandler extends Handler
6
{
7
    /** {@inheritdoc} */
8
    protected $validExt = [
9
        'png',
10
        'jpeg',
11
        'jpg',
12
    ];
13
    protected $args = [
14
        'png' => ' -o7 %s',
15
        'jpg' => ' -progressive -copy none -optimize -outfile %s %s', // dest, src
16
        'jpeg' => ' -progressive -copy none -optimize -outfile %s %s', // dest, src
17
    ];
18
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function handleQueue()
23
    {
24
        $handlers = [];
25
        foreach ($this->validExt as $ext) {
26
            $handlers[$ext] = HandlerTools::getBinaryHandler($ext);
27
        }
28
29
        foreach ($this->files as $file) {
30
31
            $command = false;
32
33
            $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
34
35
            if ($handlers[$ext]) {
36
37
                switch ($ext) {
38
                    case 'png':
39
                        $command = sprintf($this->args[$ext], escapeshellarg($file));
40
                        break;
41
                    case 'jpg':
42
                    case 'jpeg':
43
                        $command = sprintf($this->args[$ext], escapeshellarg($file), escapeshellarg($file . ".original"));
44
                        break;
45
                }
46
                if (!file_exists($file . ".original") && $command) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $command of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
47
                    copy($file, $file . ".original");
48
                    exec($handlers[$ext] . $command);
49
                }
50
51
            } else {
52
                echo "Не найден бинарный обработчик для файла: $file\n";
53
            }
54
        }
55
    }
56
}
57