Image::getValue()   B
last analyzed

Complexity

Conditions 10
Paths 10

Size

Total Lines 39
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 13.2218

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 25
c 1
b 0
f 0
nc 10
nop 1
dl 0
loc 39
ccs 15
cts 22
cp 0.6818
crap 13.2218
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of the Divergence package.
4
 *
5
 * (c) Henry Paradiz <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Divergence\Models\Media;
12
13
use Exception;
14
15
/**
16
 * Image Media Model
17
 *
18
 * @author Henry Paradiz <[email protected]>
19
 * @author Chris Alfano <[email protected]>
20
 *
21
 * {@inheritDoc}
22
 */
23
class Image extends Media
0 ignored issues
show
Bug introduced by
The type Divergence\Models\Media\Media was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
{
25
    // configurables
26
    public static $jpegCompression = 90;
27
28 10
    public function getValue($name)
29
    {
30
        switch ($name) {
31 10
            case 'ThumbnailMIMEType':
32 3
                switch ($this->getValue('MIMEType')) {
33 3
                    case 'application/psd':
34
                        return 'image/png';
35 3
                    case 'image/tiff':
36
                        return 'image/jpeg';
37
                    default:
38 3
                        return $this->getValue('MIMEType');
39
                }
40
41
                // no break
42 10
            case 'Extension':
43
44 8
                switch ($this->getValue('MIMEType')) {
45 8
                    case 'application/psd':
46
                        return 'psd';
47
48 8
                    case 'image/tiff':
49
                        return 'tif';
50
51 8
                    case 'image/gif':
52
                        return 'gif';
53
54 8
                    case 'image/jpeg':
55
                        return 'jpg';
56
57 8
                    case 'image/png':
58 8
                        return 'png';
59
60
                    default:
61
                        throw new Exception('Unable to find photo extension for mime-type: '.$this->getValue('MIMEType'));
62
                }
63
64
                // no break
65
            default:
66 10
                return parent::getValue($name);
67
        }
68
    }
69
70
    /**
71
     * Runs getimagesize on the file and returns an array with the output
72
     *
73
     * @param string $filename
74
     * @param array $mediaInfo
75
     * @return array $mediaInfo
76
     */
77 2
    public static function analyzeFile($filename, $mediaInfo = [])
78
    {
79 2
        if (!$mediaInfo['imageInfo'] = getimagesize($filename)) {
80
            throw new Exception('Failed to read image file information');
81
        }
82
83
        // store image data
84 2
        $mediaInfo['width'] = $mediaInfo['imageInfo'][0];
85 2
        $mediaInfo['height'] = $mediaInfo['imageInfo'][1];
86 2
        $mediaInfo['duration'] = 0;
87
88 2
        return $mediaInfo;
89
    }
90
}
91