JpgCompress::execute()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 1
dl 0
loc 11
ccs 8
cts 8
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SaeDigital\JpgCompress;
4
5
use Intervention\Image\ImageManager;
6
use Phulp\PipeInterface;
7
use Phulp\Source;
8
9
/**
10
 * Class JpgCompress
11
 *
12
 * @package SaeDigital\JpgCompress
13
 */
14
class JpgCompress implements PipeInterface
15
{
16
17
    /**
18
     * @var array
19
     */
20
    private $options = [
21
        'driver'  => 'imagick',
22
        'quality' => 60,
23
        'format'  => null
24
    ];
25
26
27
    /**
28
     * @var ImageManager;
29
     */
30
    private $manager;
31
32
33
    /**
34
     * JpgCompress constructor.
35
     *
36
     * @param array $options
37
     * @param ImageManager $manager
38
     *
39
     */
40 1
    public function __construct(array $options = [], ImageManager $manager = null)
41
    {
42 1
        $this->options = array_merge($this->options, $options);
43 1
        $this->manager = $manager ?: new ImageManager($this->options);
44 1
    }
45
46
47
    /**
48
     * @param Source $source
49
     *
50
     * @return void
51
     */
52 1
    public function execute(Source $source)
53
    {
54 1
        foreach ($source->getDistFiles() as $key => $file) {
55 1
            $pathFile = $file->getFullpath().$file->getDistpathname();
56 1
            $ext = $this->options['format'] ?: pathinfo($pathFile, PATHINFO_EXTENSION);
57
58 1
            $content = (string) $this->manager
59 1
                ->make($pathFile)
60 1
                ->encode($ext, $this->options['quality']);
61
62 1
            $file->setContent($content);
63
        }
64 1
    }
65
}
66