Completed
Push — master ( 0235df...db4a8b )
by Bjørn
12:10 queued 01:07
created

AutoQualityTrait::getCalculatedQuality()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WebPConvert\Convert\BaseConverters\BaseTraits;
4
5
use WebPConvert\Convert\Helpers\JpegQualityDetector;
6
7
trait AutoQualityTrait
8
{
9
10
    private $processed = false;
11
    private $qualityCouldNotBeDetected = false;
12
    private $calculatedQuality;
13
14
    abstract public function getMimeTypeOfSource();
15
16
    /**
17
     *  Determine if quality detection is required but failing.
18
     *
19
     *  It is considered "required" when:
20
     *  - Mime type is "image/jpeg"
21
     *  - Quality is set to "auto"
22
     *
23
     *  @return  boolean
24
     */
25
    public function isQualityDetectionRequiredButFailing()
26
    {
27
        $this->processQualityOptionIfNotAlready();
28
        return $this->qualityCouldNotBeDetected;
29
    }
30
31
    /**
32
     *  Get calculated quality.
33
     *
34
     *  If mime type is something else than "image/jpeg", the "default-quality" option is returned
35
     *  Same thing for jpeg, when the "quality" option is set to a number (rather than "auto").
36
     *
37
     *  Otherwise:
38
     *  If quality cannot be detetected, the "default-quality" option is returned.
39
     *  If quality can be detetected, the lowest value of this and the "max-quality" option is returned
40
     *
41
     *  @return  int
42
     */
43
    public function getCalculatedQuality()
44
    {
45
        $this->processQualityOptionIfNotAlready();
46
        return $this->calculatedQuality;
47
    }
48
49
    /**
50
     * @return void
51
     */
52
    private function processQualityOptionIfNotAlready()
53
    {
54
        if (!$this->processed) {
55
            $this->processed = true;
56
            $this->processQualityOption();
57
        }
58
    }
59
60
    /**
61
     * @return void
62
     */
63
    private function processQualityOption()
64
    {
65
        $options = $this->options;
66
        $logger = $this->logger;
67
        $source = $this->source;
68
69
        $q = $options['quality'];
70
        if ($q == 'auto') {
71
            if (($this->getMimeTypeOfSource() == 'image/jpeg')) {
72
                $q = JpegQualityDetector::detectQualityOfJpg($source);
73
                if (is_null($q)) {
74
                    $q = $options['default-quality'];
75
                    $logger->logLn(
76
                        'Quality of source could not be established (Imagick or GraphicsMagick is required)' .
77
                        ' - Using default instead (' . $options['default-quality'] . ').'
78
                    );
79
80
                    $this->qualityCouldNotBeDetected = true;
81
                } else {
82
                    if ($q > $options['max-quality']) {
83
                        $logger->logLn(
84
                            'Quality of source is ' . $q . '. ' .
85
                            'This is higher than max-quality, so using max-quality instead (' .
86
                                $options['max-quality'] . ')'
87
                        );
88
                    } else {
89
                        $logger->logLn('Quality set to same as source: ' . $q);
90
                    }
91
                }
92
                $q = min($q, $options['max-quality']);
93
            } else {
94
                //$q = $options['default-quality'];
95
                $q = min($options['default-quality'], $options['max-quality']);
96
                $logger->logLn('Quality: ' . $q . '. ');
97
            }
98
        } else {
99
            $logger->logLn(
100
                'Quality: ' . $q . '. ' .
101
                'Consider setting quality to "auto" instead. It is generally a better idea'
102
            );
103
        }
104
        $this->calculatedQuality = $q;
105
    }
106
}
107