Crop::parseOptionsString()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 19
ccs 0
cts 17
cp 0
rs 8.8571
cc 6
eloc 13
nc 6
nop 1
crap 42
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 Crop extends AbstractTransformer
13
{
14
    /**
15
     * @var ImageManager
16
     */
17
    private $imageManager;
18
19
    /**
20
     * {@inheritdoc}
21
     */
22
    protected $defaults = [
23
        'x' => 0,
24
        'y' => 0,
25
        'width' => null,
26
        'height' => null,
27
    ];
28
29
    public function __construct(ImageManager $imageManager)
30
    {
31
        $this->imageManager = $imageManager;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 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...
38
    {
39
        $options = $this->parseOptions($options);
40
41
        $image = $this->imageManager->make($source);
42
43
        $image->crop(
44
            $options['width'],
45
            $options['height'],
46
            $options['x'],
47
            $options['y']
48
        );
49
50
        return $image->encode($image->mime())->getEncoded();
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 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...
57
    {
58
        $options = parent::parseOptions($options);
59
60
        if (empty($options['width']) || empty($options['height'])) {
61
            throw new InvalidArgumentException; //todo
62
        }
63
64
        return $options;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    protected function parseOptionsString($options)
71
    {
72
        $parsed_options = [];
73
        $options_list = explode(',', $options, 4);
74
75
        foreach ($options_list as $option) {
76
            if (preg_match('/^w(?<value>[1-9]\d*)$/', $option, $match)) {
77
                $parsed_options['width'] = $match['value'];
78
            } elseif (preg_match('/^h(?<value>[1-9]\d*)$/', $option, $match)) {
79
                $parsed_options['height'] = $match['value'];
80
            } elseif (preg_match('/^x(?<value>[1-9]\d*)$/', $option, $match)) {
81
                $parsed_options['x'] = $match['value'];
82
            } elseif (preg_match('/^y(?<value>[1-9]\d*)$/', $option, $match)) {
83
                $parsed_options['y'] = $match['value'];
84
            }
85
        }
86
87
        return $parsed_options;
88
    }
89
}
90