Crop::apply()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 15
loc 15
ccs 0
cts 12
cp 0
rs 9.4285
cc 1
eloc 9
nc 1
nop 2
crap 2
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