Passed
Push — master ( 74ce7c...3af96c )
by Bjørn
03:41
created

LosslessAutoTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 60
ccs 0
cts 35
cp 0
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A runActualConvert() 0 41 5
A passOnLosslessAuto() 0 3 1
A supportsLossless() 0 3 1
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
    public function supportsLossless()
17
    {
18
        return true;
19
    }
20
21
    /** Default is to not pass "lossless:auto" on, but implement it.
22
     *
23
     *  The Stack converter passes it on (it does not even use this trait)
24
     *  WPC currently implements it, but this might be configurable in the future.
25
     *
26
     */
27
    public function passOnLosslessAuto()
28
    {
29
        return false;
30
    }
31
32
    protected function runActualConvert()
33
    {
34
        if (!$this->passOnLosslessAuto() && ($this->options['lossless'] === 'auto') && $this->supportsLossless()) {
35
            $destination = $this->destination;
36
            $destinationLossless =  $this->destination . '.lossless.webp';
37
            $destinationLossy =  $this->destination . '.lossy.webp';
38
39
            $this->logLn(
0 ignored issues
show
Bug introduced by
It seems like logLn() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

39
            $this->/** @scrutinizer ignore-call */ 
40
                   logLn(
Loading history...
40
                'Lossless is set to auto. Converting to both lossless and lossy and selecting the smallest file'
41
            );
42
43
44
            $this->ln();
0 ignored issues
show
Bug introduced by
It seems like ln() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

44
            $this->/** @scrutinizer ignore-call */ 
45
                   ln();
Loading history...
45
            $this->logLn('Converting to lossy');
46
            $this->destination = $destinationLossy;
0 ignored issues
show
Bug Best Practice introduced by
The property destination does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
47
            $this->options['lossless'] = false;
0 ignored issues
show
Bug Best Practice introduced by
The property options does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
48
            $this->doActualConvert();
0 ignored issues
show
Bug introduced by
It seems like doActualConvert() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

48
            $this->/** @scrutinizer ignore-call */ 
49
                   doActualConvert();
Loading history...
49
            $this->logLn('Reduction: ' .
50
                round((filesize($this->source) - filesize($this->destination))/filesize($this->source) * 100) . '% ');
51
52
            $this->ln();
53
            $this->logLn('Converting to lossless');
54
            $this->destination = $destinationLossless;
55
            $this->options['lossless'] = true;
56
            $this->doActualConvert();
57
            $this->logLn('Reduction: ' .
58
                round((filesize($this->source) - filesize($this->destination))/filesize($this->source) * 100) . '% ');
59
60
            $this->ln();
61
            if (filesize($destinationLossless) > filesize($destinationLossy)) {
62
                $this->logLn('Picking lossy');
63
                unlink($destinationLossless);
64
                rename($destinationLossy, $destination);
65
            } else {
66
                $this->logLn('Picking lossless');
67
                unlink($destinationLossy);
68
                rename($destinationLossless, $destination);
69
            }
70
            $this->destination = $destination;
71
        } else {
72
            $this->doActualConvert();
73
        }
74
    }
75
}
76