SizesScaler   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 47
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setSizes() 0 5 1
A scale() 0 21 4
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
40
        if ($this->includeSource) {
41
            $sizes[$imageWidth] = $imageHeight;
42
        }
43
44
        foreach ($this->sizes as $width) {
45
            if ($width > $imageWidth) {
46
                continue;
47
            }
48
49
            $sizes[$width] = round($width * $ratio);
50
        }
51
52
        return $sizes;
53
    }
54
}
55