Passed
Push — master ( 33679c...4b5f24 )
by Brent
02:04
created

ResponsiveFactory::create()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 49
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 31
nc 10
nop 1
1
<?php
2
3
namespace brendt\image;
4
5
use brendt\image\exception\FileNotFoundException;
6
use Intervention\Image\Exception\NotReadableException;
7
use Intervention\Image\ImageManager;
8
use Symfony\Component\Filesystem\Filesystem;
9
use Symfony\Component\Finder\Finder;
10
use Symfony\Component\Finder\SplFileInfo;
11
12
class ResponsiveFactory {
13
14
    /**
15
     * The compile directory to store images.
16
     *
17
     * @var string
18
     */
19
    protected $compileDir;
20
21
    /**
22
     * @var float
23
     */
24
    protected $stepModified = 0.1;
25
26
    /**
27
     * @var integer
28
     */
29
    protected $minsize = 300;
30
31
    /**
32
     * @var ImageManager
33
     */
34
    protected $engine;
35
36
    /**
37
     * @var bool
38
     */
39
    private $enableCache;
40
41
    /**
42
     * ResponsiveFactory constructor.
43
     *
44
     * @param string $compileDir
45
     * @param string $driver
46
     * @param float  $stepModifier
47
     * @param int    $minsize
48
     * @param bool   $enableCache
49
     */
50
    public function __construct($compileDir, $driver = 'gd', $stepModifier = 0.1, $minsize = 300, $enableCache = false) {
51
        $this->compileDir = './' . trim($compileDir, './');
52
53
        $fs = new Filesystem();
54
        if (!$fs->exists($this->compileDir)) {
55
            $fs->mkdir($this->compileDir);
56
        }
57
58
        $this->engine = new ImageManager([
59
            'driver' => $driver,
60
        ]);
61
62
        $this->stepModifier = $stepModifier;
0 ignored issues
show
Bug introduced by
The property stepModifier does not seem to exist. Did you mean stepModified?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
63
        $this->minsize = $minsize;
64
        $this->enableCache = $enableCache;
65
    }
66
67
    /**
68
     * @param string $path
69
     *
70
     * @return ResponsiveImage
71
     * @throws FileNotFoundException
72
     */
73
    public function create($path) {
74
        $file = $this->getImageFile($path);
75
        $sourcePath = "{$this->compileDir}/{$file->getFilename()}";
76
77
        $fs = new Filesystem();
78
        if (!$fs->exists($sourcePath)) {
79
            $fs->copy($file->getPathname(), $sourcePath);
80
        }
81
82
        $sourceImage = new ResponsiveImage($sourcePath);
83
        $sourceFile = $sourceImage->getFile();
84
85
        try {
86
            $image = $this->engine->make($sourceFile->getPathname());
87
        } catch (NotReadableException $e) {
88
            throw new FileNotFoundException($sourceFile->getPathname());
89
        }
90
91
        $extension = $sourceFile->getExtension();
92
        $name = str_replace(".{$extension}", '', $sourceFile->getFilename());
93
94
        $width = $image->getWidth();
95
        $stepWidth = $width * $this->stepModifier;
0 ignored issues
show
Bug introduced by
The property stepModifier does not seem to exist. Did you mean stepModified?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
96
        $height = $image->getHeight();
97
        $stepHeight = $height * $this->stepModifier;
0 ignored issues
show
Bug introduced by
The property stepModifier does not seem to exist. Did you mean stepModified?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
98
99
        while ($width >= $this->minsize) {
100
            if ($width === $image->getWidth()) {
101
                $width -= $stepWidth;
102
                $height -= $stepHeight;
103
104
                continue;
105
            }
106
107
            $scaledPath = "{$this->compileDir}/{$name}-{$width}.{$extension}";
108
109
            if (!$this->enableCache || !$fs->exists($scaledPath)) {
110
                $image->resize($width, $height)
111
                    ->save($scaledPath);
112
            }
113
114
            $sourceImage->addSource($scaledPath, $width);
115
116
            $width -= $stepWidth;
117
            $height -= $stepHeight;
118
        }
119
120
        return $sourceImage;
121
    }
122
123
    /**
124
     * @param int $minsize
125
     *
126
     * @return ResponsiveFactory
127
     */
128
    public function setMinsize($minsize) {
129
        $this->minsize = $minsize;
130
131
        return $this;
132
    }
133
134
    /**
135
     * @param float $stepModified
136
     *
137
     * @return ResponsiveFactory
138
     */
139
    public function setStepModified($stepModified) {
140
        $this->stepModified = $stepModified;
141
142
        return $this;
143
    }
144
145
    /**
146
     * @param $src
147
     *
148
     * @return SplFileInfo
149
     * @throws FileNotFoundException
150
     */
151 View Code Duplication
    private function getImageFile($src) {
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...
152
        $files = Finder::create()->files()->in('.')->path(trim($src, './'));
153
154
        foreach ($files as $file) {
155
            return $file;
156
        }
157
158
        throw new FileNotFoundException($src);
159
    }
160
161
}
162