Completed
Push — master ( c2f76d...03586b )
by Freek
11:35
created

ResponsiveImage   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 110
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 10 1
A registerTinySvg() 0 10 1
A __construct() 0 6 1
A url() 0 6 1
A generatedFor() 0 10 1
A width() 0 8 1
A height() 0 6 1
A getPropertyParts() 0 6 1
A stringBetween() 0 10 1
A delete() 0 20 1
1
<?php
2
3
namespace Spatie\MediaLibrary\ResponsiveImages;
4
5
use Spatie\MediaLibrary\Models\Media;
6
use \Spatie\MediaLibrary\Filesystem\Filesystem;
7
use Spatie\MediaLibrary\UrlGenerator\UrlGeneratorFactory;
8
use Spatie\MediaLibrary\PathGenerator\PathGeneratorFactory;
9
10
class ResponsiveImage
11
{
12
    /** @var string */
13
    public $fileName = '';
14
15
    /** @var \Spatie\MediaLibrary\Models\Media */
16
    public $media;
17
18
    public static function register(Media $media, $fileName, $conversionName)
19
    {
20
        $responsiveImages = $media->responsive_images;
21
22
        $responsiveImages[$conversionName]['urls'][] = $fileName;
23
24
        $media->responsive_images = $responsiveImages;
25
26
        $media->save();
27
    }
28
29
    public static function registerTinySvg(Media $media, string $base64Svg, string $conversionName)
30
    {
31
        $responsiveImages = $media->responsive_images;
32
33
        $responsiveImages[$conversionName]['base64svg'] = $base64Svg;
34
35
        $media->responsive_images = $responsiveImages;
36
37
        $media->save();
38
    }
39
40
    public function __construct(string $fileName, Media $media)
41
    {
42
        $this->fileName = $fileName;
43
44
        $this->media = $media;
45
    }
46
47
    public function url(): string
48
    {
49
        $urlGenerator = UrlGeneratorFactory::createForMedia($this->media);
50
51
        return $urlGenerator->getResponsiveImagesDirectoryUrl() . $this->fileName;
52
    }
53
54
    public function generatedFor(): string
55
    {
56
        $propertyParts = $this->getPropertyParts();
57
58
        array_pop($propertyParts);
59
60
        array_pop($propertyParts);
61
62
        return implode('_', $propertyParts);
63
    }
64
65
    public function width(): int
66
    {
67
        $propertyParts = $this->getPropertyParts();
68
69
        array_pop($propertyParts);
70
71
        return (int) last($propertyParts);
72
    }
73
74
    public function height(): int
75
    {
76
        $propertyParts = $this->getPropertyParts();
77
78
        return (int) last($propertyParts);
79
    }
80
81
    protected function getPropertyParts(): array
82
    {
83
        $propertyString =  $this->stringBetween($this->fileName, '___', '.');
84
85
        return explode('_', $propertyString);
86
    }
87
88
    protected function stringBetween(string $subject, string $startCharacter, string $endCharacter): string
89
    {
90
        $between = strstr($subject, $startCharacter);
91
92
        $between = str_replace('___', '', $between);
93
94
        $between = strstr($between, $endCharacter, true);
95
96
        return $between;
97
    }
98
99
    public function delete()
100
    {
101
        $pathGenerator = PathGeneratorFactory::create();
102
103
        $path = $pathGenerator->getPathForResponsiveImages($this->media);
104
105
        $fullPath = $path . $this->fileName;
106
107
        app(Filesystem::class)->removeFile($this->media, $fullPath);
108
109
        $responsiveImages = $this->media->responsive_images;
110
111
        unset($responsiveImages[$this->generatedFor()]);
112
113
        $this->media->responsive_images = $responsiveImages;
114
115
        $this->media->save();
116
117
        return $this;
118
    }
119
}
120