Passed
Push — master ( 1d5a7e...f5ffe9 )
by Brent
02:21
created

WidthScaler::scale()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 26
Code Lines 18

Duplication

Lines 6
Ratio 23.08 %

Importance

Changes 0
Metric Value
dl 6
loc 26
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 18
nc 3
nop 2
1
<?php
2
3
namespace Brendt\Image\Scaler;
4
5
use Brendt\Image\ResponsiveImage;
6
use Intervention\Image\Image;
7
8
class WidthScaler extends AbstractScaler
9
{
10
11
    /**
12
     * @param ResponsiveImage $responsiveImage
13
     * @param Image           $imageObject
14
     *
15
     * @return ResponsiveImage
16
     */
17
    public function scale(ResponsiveImage $responsiveImage, Image $imageObject) {
18
        $width = $imageObject->getWidth();
19
        $height = $imageObject->getHeight();
20
        $fileName = $responsiveImage->getFileName();
21
        $extension = $responsiveImage->getExtension();
22
        $urlPath = $responsiveImage->getUrlPath();
23
24
        while ($width >= $this->minWidth) {
25
            $scaledName = "{$fileName}-{$width}.{$extension}";
26
            $scaledSrc = "{$urlPath}/{$scaledName}";
27
            $responsiveImage->addSource($scaledSrc, $width);
28
29
            $publicScaledPath = "{$this->publicPath}/{$urlPath}/{$scaledName}";
30 View Code Duplication
            if (!$this->enableCache || !$this->fs->exists($publicScaledPath)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
31
                $this->fs->dumpFile(
32
                    $publicScaledPath,
33
                    $imageObject->resize($width, $height)->encode($extension)
34
                );
35
            }
36
37
            $width = floor($width * $this->stepModifier);
38
            $height = floor($height * $this->stepModifier);
39
        }
40
41
        return $responsiveImage;
42
    }
43
}
44