PsImageOptimizer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 70
ccs 0
cts 23
cp 0
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getOptimizer() 0 3 1
A getTempDir() 0 3 1
A init() 0 5 2
A exec() 0 10 1
1
<?php
2
declare(strict_types=1);
3
4
namespace tkanstantsin\fileupload\formatter\adapter;
5
6
use ImageOptimizer\Optimizer;
7
use ImageOptimizer\OptimizerFactory;
8
use tkanstantsin\fileupload\model\BaseObject;
9
use tkanstantsin\fileupload\model\IFile;
10
11
/**
12
 * Class ImageOptimizer
13
 * Optimize images using `ps/image-optimizer`
14
 * @see https://github.com/psliwa/image-optimizer
15
 */
16
class PsImageOptimizer extends BaseObject implements IFormatAdapter
17
{
18
    public const DEFAULT_CONFIG = [
19
        'ignore_errors'                     => true,
20
        'execute_only_first_jpeg_optimizer' => false,
21
        'execute_only_first_png_optimizer'  => false,
22
        'jpegoptim_options'                 => ['-m90', '--strip-all', '--all-progressive'],
23
        'jpegtran_options'                  => ['-optimize', '-progressive', '-perfect'],
24
    ];
25
26
    /**
27
     * @var string|null
28
     */
29
    public $tempDir;
30
31
    /**
32
     * @var array
33
     */
34
    public $optimizerConfig = self::DEFAULT_CONFIG;
35
36
    /**
37
     * @inheritdoc
38
     */
39
    public function init(): void
40
    {
41
        parent::init();
42
        if (!class_exists(OptimizerFactory::class)) {
43
            trigger_error(sprintf('%s class not found', OptimizerFactory::class));
44
        }
45
    }
46
47
48
    /**
49
     * Applies filters or something to content and return it
50
     *
51
     * @param IFile $file
52
     * @param       $content
53
     *
54
     * @return mixed
55
     * @throws \ImageOptimizer\Exception\Exception
56
     */
57
    public function exec(IFile $file, $content)
58
    {
59
        $tmpPath = tempnam($this->getTempDir(), '');
60
        file_put_contents($tmpPath, $content);
61
        $this->getOptimizer()->optimize($tmpPath);
62
        $content = file_get_contents($tmpPath);
63
64
        @unlink($tmpPath);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

64
        /** @scrutinizer ignore-unhandled */ @unlink($tmpPath);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
65
66
        return $content;
67
    }
68
69
    /**
70
     * @return string
71
     */
72
    protected function getTempDir(): string
73
    {
74
        return (string) ($this->tempDir ?? sys_get_temp_dir());
75
    }
76
77
    /**
78
     * @param string $name
79
     *
80
     * @return Optimizer
81
     * @throws \ImageOptimizer\Exception\Exception
82
     */
83
    protected function getOptimizer($name = OptimizerFactory::OPTIMIZER_SMART): Optimizer
84
    {
85
        return (new OptimizerFactory($this->optimizerConfig))->get($name);
86
    }
87
}