Passed
Push — main ( c698a6...2d171d )
by Yaroslav
02:44
created

CanCreate::makeFileName()   A

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 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
namespace SimpleImageManager\Managers;
4
5
use Illuminate\Http\UploadedFile;
6
use Illuminate\Support\Str;
7
use Spatie\Image\Image;
8
9
trait CanCreate
10
{
11
    /**
12
     * @param string|null $fileName
13
     *
14
     * @return string
15
     */
16 9
    protected function makeFileName(?string $fileName = null): string
17
    {
18 9
        if (!$fileName) {
19 2
            return $this->prefix . Str::random(30);
20
        }
21
22 7
        return $this->prefix . $fileName;
23
    }
24
25 9
    protected function ensureDirectoryExists(string $fileName): void
26
    {
27
        // Be sure directories tree created
28 9
        $tmpFile = rtrim(dirname($fileName), '/') . '/.tmp-' . Str::uuid();
29 9
        $this->storage()->put($tmpFile, '');
0 ignored issues
show
Bug introduced by
It seems like storage() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

29
        $this->/** @scrutinizer ignore-call */ 
30
               storage()->put($tmpFile, '');
Loading history...
30 9
        $this->storage()->delete($tmpFile);
31
    }
32
33 9
    protected function createOriginalFile(UploadedFile $image, string $newFileName, string $newFileExt): void
34
    {
35 9
        $path = "{$newFileName}{$newFileExt}";
36 9
        if (!in_array($newFileExt, $this->immutableExtensions)) {
37 8
            $builder = Image::load($image->path());
38 8
            if (is_array($this->original) &&
39 8
                !empty($this->original['methods'])
40
            ) {
41 8
                foreach ($this->original['methods'] as $method => $attrs) {
42 8
                    call_user_func_array([$builder, $method], $attrs);
43
                }
44
            }
45
46 8
            $builder->save($this->storage()->path($path));
47
        } else {
48 1
            $this->storage()->put($path, $image->getContent());
49
        }
50
    }
51
52 9
    protected function createFormats(UploadedFile $image, string $newFileName, string $newFileExt): void
53
    {
54 9
        if (!in_array($newFileExt, $this->immutableExtensions)) {
55 8
            foreach ($this->formats as $format => $configuration) {
56 8
                $path = "{$newFileName}-{$format}{$newFileExt}";
57
58 8
                $builder = Image::load($image->path());
59 8
                if (!empty($configuration['methods']) && is_array($configuration['methods'])) {
60 8
                    foreach ($configuration['methods'] as $method => $attrs) {
61 8
                        call_user_func_array([$builder, $method], $attrs);
62
                    }
63
                }
64 8
                $builder->save($this->storage()->path($path));
65
            }
66
        }
67
    }
68
}
69