Fit::parseOptionsString()   C
last analyzed

Complexity

Conditions 23
Paths 23

Size

Total Lines 54
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 552

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 54
ccs 0
cts 52
cp 0
rs 6.5279
cc 23
eloc 47
nc 23
nop 1
crap 552

How to fix   Long Method    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
namespace SvImages\Transformer;
3
4
use Intervention\Image;
5
use Intervention\Image\ImageManager;
6
use SvImages\Exception\InvalidArgumentException;
7
8
/**
9
 * @author Vytautas Stankus <[email protected]>
10
 * @license MIT
11
 */
12
class Fit extends AbstractTransformer
13
{
14
    protected $positions = [
15
        'top-left',
16
        'top',
17
        'top-right',
18
        'left',
19
        'center',
20
        'right',
21
        'bottom-left',
22
        'bottom',
23
        'bottom-right',
24
        'tl',
25
        't',
26
        'tr',
27
        'l',
28
        'c',
29
        'r',
30
        'bl',
31
        'b',
32
        'br'
33
    ];
34
35
    protected $defaults = [
36
        'position' => 'center',
37
        'width' => null,
38
        'height' => null,
39
    ];
40
41
    /**
42
     * @var ImageManager
43
     */
44
    private $imageManager;
45
46
    public function __construct(ImageManager $imageManager)
47
    {
48
        $this->imageManager = $imageManager;
49
    }
50
51 View Code Duplication
    public function apply($source, $options)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        $options = $this->parseOptions($options);
54
55
        $image = $this->imageManager->make($source);
56
57
        $image->fit(
58
            $options['width'],
59
            $options['height'],
60
            function ($constraint) {
61
                $constraint->upsize();
62
            },
63
            $options['position']
64
        );
65
66
        return $image->encode($image->mime())->getEncoded();
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 View Code Duplication
    protected function parseOptions($options)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    {
74
        $options = parent::parseOptions($options);
75
76
        if (empty($options['width']) && empty($options['height'])) {
77
            throw new InvalidArgumentException; //todo
78
        }
79
80
        return $options;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    protected function parseOptionsString($options)
87
    {
88
        $parsed_options = [];
89
        $options_list = explode(',', $options, 3);
90
91
        foreach ($options_list as $option) {
92
            if (preg_match('/^w(?<value>[1-9]\d*)$/', $option, $match)) {
93
                $parsed_options['width'] = $match['value'];
94
            } elseif (preg_match('/^h(?<value>[1-9]\d*)$/', $option, $match)) {
95
                $parsed_options['height'] = $match['value'];
96
            } elseif (in_array($option, $this->positions)) {
97
                switch ($option) {
98
                    case 'top-left':
99
                    case 'tl':
100
                        $parsed_options['position'] = 'top-left';
101
                        break;
102
                    case 'top':
103
                    case 't':
104
                        $parsed_options['position'] = 'top';
105
                        break;
106
                    case 'top-right':
107
                    case 'tr':
108
                        $parsed_options['position'] = 'top-right';
109
                        break;
110
                    case 'left':
111
                    case 'l':
112
                        $parsed_options['position'] = 'left';
113
                        break;
114
                    case 'center':
115
                    case 'c':
116
                        $parsed_options['position'] = 'center';
117
                        break;
118
                    case 'right':
119
                    case 'r':
120
                        $parsed_options['position'] = 'right';
121
                        break;
122
                    case 'bottom-left':
123
                    case 'bl':
124
                        $parsed_options['position'] = 'bottom-left';
125
                        break;
126
                    case 'bottom':
127
                    case 'b':
128
                        $parsed_options['position'] = 'bottom';
129
                        break;
130
                    case 'bottom-right':
131
                    case 'br':
132
                        $parsed_options['position'] = 'bottom-right';
133
                        break;
134
                }
135
            }
136
        }
137
138
        return $parsed_options;
139
    }
140
}
141