Completed
Push — master ( 72430f...dafba8 )
by Jonathan
04:56
created

Dimension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1.008

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 5
cp 0.8
rs 10
cc 1
nc 1
nop 2
crap 1.008
1
<?php
2
3
namespace League\Glide\Manipulators\Helpers;
4
5
use Intervention\Image\Image;
6
7
class Dimension
8
{
9
    /**
10
     * The source image.
11
     * @var Image
12
     */
13
    protected $image;
14
15
    /**
16
     * The device pixel ratio.
17
     * @var integer
18
     */
19
    protected $dpr;
20
21
    /**
22
     * Create dimension helper instance.
23
     * @param Image   $image The source image.
24
     * @param integer $dpr   The device pixel ratio.
25
     */
26 26
    public function __construct(Image $image, $dpr = 1)
27
    {
28 26
        $this->image = $image;
29 26
        $this->dpr = $dpr;
30 26
    }
31
32
    /**
33
     * Resolve the dimension.
34
     * @param  string $value The dimension value.
35
     * @return double The resolved dimension.
36
     */
37 26
    public function get($value)
38
    {
39 26
        if (is_numeric($value) and $value > 0) {
40 18
            return (double) $value * $this->dpr;
41
        }
42
43 10
        if (preg_match('/^(\d{1,2}(?!\d)|100)(w|h)$/', $value, $matches)) {
44 6
            if ($matches[2] === 'h') {
45 4
                return (double) $this->image->height() * ($matches[1] / 100);
46
            }
47
48 4
            return (double) $this->image->width() * ($matches[1] / 100);
49
        }
50 6
    }
51
}
52