ThinkImage::__call()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
namespace SimpleImageManager\Eloquent;
4
5
use Illuminate\Http\UploadedFile;
6
use Illuminate\Support\Facades\View;
7
use Illuminate\Support\Str;
8
use SimpleImageManager\Contracts\ImageManagerInterface;
9
use SimpleImageManager\Facades\SimpleImageManager;
10
11
class ThinkImage
12
{
13
    protected ImageManagerInterface $manager;
14
15
    protected ?string $value;
16
17
    /**
18
     * Default fallback for path.
19
     *
20
     * @var string|null
21
     */
22
    protected ?string $defaultPath = null;
23
24
    /**
25
     * Default fallback for url.
26
     *
27
     * @var string|null
28
     */
29
    protected ?string $defaultUrl = null;
30
31 6
    public function __construct(string $driver, ?string $value = null)
32
    {
33 6
        $this->manager = SimpleImageManager::driver($driver);
0 ignored issues
show
Bug introduced by
The method driver() does not exist on SimpleImageManager\Facades\SimpleImageManager. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
        /** @scrutinizer ignore-call */ 
34
        $this->manager = SimpleImageManager::driver($driver);
Loading history...
34 6
        $this->value   = $value;
35
    }
36
37
    /**
38
     * @param string|null $value
39
     *
40
     * @return static
41
     */
42 6
    public function setValue(?string $value): static
43
    {
44 6
        $this->value = $value;
45
46 6
        return $this;
47
    }
48
49
    /**
50
     * @param string|null $defaultPath
51
     *
52
     * @return static
53
     */
54 6
    public function setDefaultPath(?string $defaultPath): static
55
    {
56 6
        $this->defaultPath = $defaultPath;
57
58 6
        return $this;
59
    }
60
61
    /**
62
     * @param string|null $defaultUrl
63
     *
64
     * @return static
65
     */
66 6
    public function setDefaultUrl(?string $defaultUrl): static
67
    {
68 6
        $this->defaultUrl = $defaultUrl;
69
70 6
        return $this;
71
    }
72
73
    /**
74
     * Check is file exists.
75
     *
76
     * @param string|null $format
77
     *
78
     * @return bool
79
     */
80 1
    public function exists(?string $format = null): bool
81
    {
82 1
        return (($path = $this->path($format))) && file_exists($path);
83
    }
84
85
    /**
86
     * Upload file to storage.
87
     *
88
     * @param UploadedFile $image
89
     * @param string|null $filename
90
     * @param string|null $oldFile
91
     *
92
     * @return string|null Storage file name.
93
     */
94 3
    public function upload(UploadedFile $image, ?string $filename = null, ?string $oldFile = null): ?string
95
    {
96 3
        return $this->manager->upload($image, $filename, $oldFile);
97
    }
98
99
    /**
100
     * Delete file from storage.
101
     *
102
     * @return bool Storage file name.
103
     */
104 1
    public function delete(): bool
105
    {
106 1
        if (!$this->value) {
107 1
            return false;
108
        }
109
110 1
        return $this->manager->delete($this->value);
111
    }
112
113
    /**
114
     * Full path to file.
115
     *
116
     * @param string|null $format
117
     * @param string|null $default
118
     *
119
     * @return string|null
120
     */
121 3
    public function path(?string $format = null, ?string $default = null): ?string
122
    {
123 3
        if (!$this->value) {
124 1
            return $default ?? $this->defaultPath;
125
        }
126
127 3
        return $this->manager->path($this->value, $format) ?? ($default ?? $this->defaultPath);
128
    }
129
130
    /**
131
     * File url.
132
     *
133
     * @param string|null $format
134
     * @param string|null $default
135
     *
136
     * @return string|null
137
     */
138 2
    public function url(?string $format = null, ?string $default = null): ?string
139
    {
140 2
        if (!$this->value) {
141 1
            return $default ?? $this->defaultUrl;
142
        }
143
144 2
        return $this->manager->url((string) $this->value, $format) ?? ($default ?? $this->defaultUrl);
145
    }
146
147
    /**
148
     * Image tag.
149
     *
150
     * @param array $attrs
151
     * @param string|null $defaultUrl
152
     * @param bool $lazy
153
     *
154
     * @return string
155
     */
156 1
    public function img(array $attrs = [], ?string $defaultUrl = null, bool $lazy = true): string
157
    {
158 1
        $attrs = collect($attrs);
0 ignored issues
show
Bug introduced by
$attrs of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

158
        $attrs = collect(/** @scrutinizer ignore-type */ $attrs);
Loading history...
159
160 1
        if (!($src = $attrs->pull('src', $this->url($attrs->pull('format'), $defaultUrl)))) {
161 1
            return '';
162
        }
163
164 1
        $defaultSrc = $attrs->pull('defaultSrc', $src);
165 1
        $srcset     = $attrs->pull('srcset', collect($this->manager->srcsetMap())->map(fn ($i, $key) => trim($this->url($key, $defaultUrl) . " $i"))
166 1
                                                                                    ->filter()
167 1
                                                                                    ->implode(', '));
168 1
        $class      = $attrs->pull('class', '');
169 1
        $lazyClass  = trim($attrs->pull('lazyClass', 'lazy'));
170 1
        if ($lazy && !Str::contains($class, $lazyClass)) {
171 1
            $class = "{$lazyClass} {$class}";
172
        }
173
174 1
        $attributes = $attrs->map(fn ($i, $key) => "$key='{$i}'")->implode(' ');
175
176 1
        return View::make('simple-image-manager::blocks.think-image', compact('lazy', 'attributes', 'defaultSrc', 'src', 'srcset', 'class'))->render();
177
    }
178
179
    /**
180
     * Propagate call.
181
     *
182
     * @param string $method
183
     * @param array $attributes
184
     */
185 3
    public function __call($method, $attributes)
186
    {
187 3
        if (method_exists($this->manager, $method)) {
188 3
            return call_user_func_array([ $this->manager, $method ], $attributes);
189
        }
190
191 1
        throw new \BadMethodCallException("Method [{$method} not exists]");
192
    }
193
}
194