Completed
Push — master ( 2da831...e940cd )
by Dmitry
02:02
created

ImageHandler   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
D handleQueue() 0 37 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' => ' -o2 %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
                echo get_class()." обрабатывает файл: $file\n";
38
39
40
                switch ($ext) {
41
                    case 'png':
42
                        $command = sprintf($this->args[$ext], escapeshellarg($file));
43
                        break;
44
                    case 'jpg':
45
                    case 'jpeg':
46
                        $command =  sprintf($this->args[$ext], escapeshellarg($file), escapeshellarg($file . ".original"));
47
                        break;
48
                }
49
                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...
50
                    copy($file, $file . ".original");
51
                    exec($handlers[$ext]  . $command);
52
                }
53
54
            } else {
55
                echo "Не найден бинарный обработчик для файла: $file\n";
56
            }
57
        }
58
    }
59
}
60