Passed
Push — master ( 7ab3c5...ce19ef )
by Kanstantsin
04:18
created

PsImageOptimizer::exec()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace tkanstantsin\fileupload\formatter\adapter;
4
5
use ImageOptimizer\Optimizer;
6
use ImageOptimizer\OptimizerFactory;
7
use tkanstantsin\fileupload\model\IFile;
8
9
/**
10
 * Class ImageOptimizer
11
 * Optimize images using `ps/image-optimizer`
12
 * @see https://github.com/psliwa/image-optimizer
13
 */
14
class PsImageOptimizer implements IFormatAdapter
15
{
16
    public const DEFAULT_CONFIG = [
17
        'ignore_errors'                     => true,
18
        'execute_only_first_jpeg_optimizer' => false,
19
        'execute_only_first_png_optimizer'  => false,
20
        'jpegoptim_options'                 => ['-m90', '--strip-all', '--all-progressive'],
21
        'jpegtran_options'                  => ['-optimize', '-progressive', '-perfect'],
22
    ];
23
24
    /**
25
     * @var string|null
26
     */
27
    public $tempDir;
28
29
    /**
30
     * @var array
31
     */
32
    public $optimizerConfig = self::DEFAULT_CONFIG;
33
34
    /**
35
     * ImageOptimizer constructor.
36
     *
37
     * @param array $config
38
     */
39
    public function __construct(array $config = [])
40
    {
41
        foreach ($config as $key => $value) {
42
            $this->$key = $value;
43
        }
44
    }
45
46
    /**
47
     * Applies filters or something to content and return it
48
     *
49
     * @param IFile $file
50
     * @param       $content
51
     *
52
     * @return mixed
53
     * @throws \ImageOptimizer\Exception\Exception
54
     */
55
    public function exec(IFile $file, $content)
56
    {
57
        $tmpPath = tempnam($this->getTempDir(), '');
58
        file_put_contents($tmpPath, $content);
59
        $this->getOptimizer()->optimize($tmpPath);
60
        $content = file_get_contents($tmpPath);
61
62
        @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

62
        /** @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...
63
64
        return $content;
65
    }
66
67
    /**
68
     * @return string
69
     */
70
    protected function getTempDir(): string
71
    {
72
        return (string) ($this->tempDir ?? sys_get_temp_dir());
73
    }
74
75
    /**
76
     * @param string $name
77
     *
78
     * @return Optimizer
79
     */
80
    protected function getOptimizer($name = OptimizerFactory::OPTIMIZER_SMART): Optimizer
81
    {
82
        return (new OptimizerFactory($this->optimizerConfig))->get($name);
83
    }
84
}