Completed
Push — master ( 5f5fca...2fa1e0 )
by Bjørn
12:28 queued 02:24
created

isQualityDetectionRequiredButFailing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WebPConvert\Convert\BaseConverters\BaseTraits;
4
5
use WebPConvert\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 3
    public function isQualityDetectionRequiredButFailing()
26
    {
27 3
        $this->processQualityOptionIfNotAlready();
28 3
        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 5
    public function getCalculatedQuality()
44
    {
45 5
        $this->processQualityOptionIfNotAlready();
46 5
        return $this->calculatedQuality;
47
    }
48
49
    /**
50
     * @return void
51
     */
52 5
    private function processQualityOptionIfNotAlready()
53
    {
54 5
        if (!$this->processed) {
55 5
            $this->processed = true;
56 5
            $this->processQualityOption();
57 5
        }
58 5
    }
59
60
    /**
61
     * @return void
62
     */
63 5
    private function processQualityOption()
64
    {
65 5
        $options = $this->options;
66 5
        $logger = $this->logger;
67 5
        $source = $this->source;
68
69 5
        $q = $options['quality'];
70 5
        if ($q == 'auto') {
71 5
            if (($this->getMimeTypeOfSource() == 'image/jpeg')) {
72 4
                $q = JpegQualityDetector::detectQualityOfJpg($source);
73 4
                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 4
                    if ($q > $options['max-quality']) {
83 4
                        $logger->logLn(
84 4
                            'Quality of source is ' . $q . '. ' .
85 4
                            'This is higher than max-quality, so using max-quality instead (' .
86 4
                                $options['max-quality'] . ')'
87 4
                        );
88 4
                    } else {
89
                        $logger->logLn('Quality set to same as source: ' . $q);
90
                    }
91
                }
92 4
                $q = min($q, $options['max-quality']);
93 4
            } else {
94 1
                $q = $options['default-quality'];
95 1
                $logger->logLn('Quality: ' . $q . '. ');
96
            }
97 5
        } else {
98
            $logger->logLn(
99
                'Quality: ' . $q . '. ' .
100
                'Consider setting quality to "auto" instead. It is generally a better idea'
101
            );
102
        }
103 5
        $this->calculatedQuality = $q;
104 5
    }
105
}
106