Test Failed
Push — master ( 45b155...96789e )
by Brent
04:23 queued 02:15
created

SizesScaler::setSizes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Brendt\Image\Scaler;
4
5
use Intervention\Image\Image;
6
use Symfony\Component\Finder\SplFileInfo;
7
8
class SizesScaler extends AbstractScaler
9
{
10
11
    /**
12
     * @var array
13
     */
14
    private $sizes = [];
15
16
    /**
17
     * @param array $sizes
18
     *
19
     * @return SizesScaler
20
     */
21
    public function setSizes(array $sizes) : SizesScaler {
22
        $this->sizes = $sizes;
23
24
        return $this;
25
    }
26
27
    /**
28
     * @param SplFileInfo $sourceFile
29
     * @param Image       $imageObject
30
     *
31
     * @return array
32
     */
33
    public function scale(SplFileInfo $sourceFile, Image $imageObject) : array {
34
        $imageWidth = $imageObject->getWidth();
35
        $imageHeight = $imageObject->getHeight();
36
        $ratio = $imageHeight / $imageWidth;
37
38
        $sizes = [];
39
        foreach ($this->sizes as $width) {
40
            if ($width > $imageWidth) {
41
                continue;
42
            }
43
44
            $sizes[$width] = round($width * $ratio);
45
        }
46
47
        return $sizes;
48
    }
49
}
50