Passed
Push — master ( 3af96c...86c7fe )
by Bjørn
03:07
created

LosslessAutoTrait::convertTwoAndSelectSmallest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 38
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 30
nc 2
nop 0
dl 0
loc 38
ccs 0
cts 30
cp 0
crap 6
rs 9.44
c 0
b 0
f 0
1
<?php
2
3
//namespace WebPConvert\Convert\BaseConverters\BaseTraits;
4
namespace WebPConvert\Convert\Converters\ConverterTraits;
5
6
/**
7
 * Trait for converters that supports lossless encoding and thus the "lossless:auto" option.
8
 *
9
 * @package    WebPConvert
10
 * @author     Bjørn Rosell <[email protected]>
11
 * @since      Class available since Release 2.0.0
12
 */
13
trait LosslessAutoTrait
14
{
15
16
    abstract protected function logLn($msg, $style = '');
17
    abstract protected function ln();
18
    abstract protected function doActualConvert();
19
    abstract public function getDestination();
20
    abstract public function setDestination($destination);
21
    abstract public function getOptions();
22
    abstract protected function setOption($optionName, $optionValue);
23
24
    public function supportsLossless()
25
    {
26
        return true;
27
    }
28
29
    /** Default is to not pass "lossless:auto" on, but implement it.
30
     *
31
     *  The Stack converter passes it on (it does not even use this trait)
32
     *  WPC currently implements it, but this might be configurable in the future.
33
     *
34
     */
35
    public function passOnLosslessAuto()
36
    {
37
        return false;
38
    }
39
40
    private function convertTwoAndSelectSmallest()
41
    {
42
        $destination = $this->getDestination();
43
        $destinationLossless =  $this->destination . '.lossless.webp';
44
        $destinationLossy =  $this->destination . '.lossy.webp';
45
46
        $this->logLn(
47
            'Lossless is set to auto. Converting to both lossless and lossy and selecting the smallest file'
48
        );
49
50
        $this->ln();
51
        $this->logLn('Converting to lossy');
52
        $this->setDestination($destinationLossy);
53
        $this->setOption('lossless', false);
54
        $this->doActualConvert();
55
        $this->logLn('Reduction: ' .
56
            round((filesize($this->source) - filesize($this->destination))/filesize($this->source) * 100) . '% ');
57
58
        $this->ln();
59
        $this->logLn('Converting to lossless');
60
        $this->setDestination($destinationLossless);
61
        $this->setOption('lossless', true);
62
        $this->doActualConvert();
63
        $this->logLn('Reduction: ' .
64
            round((filesize($this->source) - filesize($this->destination))/filesize($this->source) * 100) . '% ');
65
66
        $this->ln();
67
        if (filesize($destinationLossless) > filesize($destinationLossy)) {
68
            $this->logLn('Picking lossy');
69
            unlink($destinationLossless);
70
            rename($destinationLossy, $destination);
71
        } else {
72
            $this->logLn('Picking lossless');
73
            unlink($destinationLossy);
74
            rename($destinationLossless, $destination);
75
        }
76
        $this->setDestination($destination);
77
        $this->setOption('lossless', 'auto');
78
    }
79
80
    protected function runActualConvert()
81
    {
82
        if (!$this->passOnLosslessAuto() && ($this->options['lossless'] === 'auto') && $this->supportsLossless()) {
83
            $this->convertTwoAndSelectSmallest();
84
        } else {
85
            $this->doActualConvert();
86
        }
87
    }
88
}
89