Passed
Branch v2 (aaaa8e)
by Brent
03:38
created

ImageFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A make() 0 8 1
A create() 0 17 2
A createScaledImage() 0 15 1
A createScaledFileName() 0 7 1
A copySourceImageToDestination() 0 5 1
1
<?php
2
3
namespace Pageon\Html\Image;
4
5
use Intervention\Image\ImageManager;
6
use Intervention\Image\Image as ScaleableImage;
7
use Symfony\Component\Filesystem\Filesystem;
8
9
class ImageFactory
10
{
11
    private $scaler;
12
    private $sourceDirectory;
13
    private $publicDirectory;
14
    private $imageManager;
15
16
    public function __construct(
17
        string $sourceDirectory,
18
        string $publicDirectory,
19
        Scaler $scaler
20
    ) {
21
        $this->sourceDirectory = rtrim($sourceDirectory, '/');
22
        $this->publicDirectory = rtrim($publicDirectory, '/');
23
        $this->scaler = $scaler;
24
        $this->imageManager = new ImageManager([
25
            'driver' => 'gd',
26
        ]);
27
    }
28
29
    public static function make(
30
        string $sourceDirectory,
31
        string $publicDirectory,
32
        Scaler $scaler
33
    ): ImageFactory
34
    {
35
        return new self($sourceDirectory, $publicDirectory, $scaler);
36
    }
37
38
    public function create($src): Image
39
    {
40
        $srcPath = ltrim($src, '/');
41
        $image = Image::make($srcPath);
42
43
        $this->copySourceImageToDestination($srcPath);
44
        $scaleableImage = $this->imageManager->make("{$this->publicDirectory}/{$srcPath}");
45
46
        $variations = $this->scaler->getVariations($scaleableImage);
47
        $image->addSrcset($image->src(), $scaleableImage->getWidth());
48
49
        foreach ($variations as $width => $height) {
50
            $this->createScaledImage($image, $width, $height, $scaleableImage);
51
        }
52
53
        return $image;
54
    }
55
56
    private function createScaledImage(
57
        Image $image,
58
        int $width,
59
        int $height,
60
        ScaleableImage $scaleableImage
61
    ) {
62
        $scaleableImageClone = clone $scaleableImage;
63
        $scaledFileName = $this->createScaledFileName($image, $width, $height);
64
65
        $scaleableImageClone
66
            ->resize($width, $height)
67
            ->save("{$this->publicDirectory}/{$scaledFileName}");
68
69
        $image->addSrcset($scaledFileName, $width);
70
    }
71
72
    private function createScaledFileName(Image $image, int $width, int $height): string
73
    {
74
        $srcPath = ltrim($image->src(), '/');
75
        $extension = pathinfo($srcPath, PATHINFO_EXTENSION);
76
77
        return str_replace(".{$extension}", "-{$width}x{$height}.{$extension}", $srcPath);
78
    }
79
80
    private function copySourceImageToDestination(string $srcPath): void
81
    {
82
        $fs = new Filesystem();
83
        $fs->copy("{$this->sourceDirectory}/{$srcPath}", "{$this->publicDirectory}/{$srcPath}");
84
    }
85
}
86