RetrieveAttachment::url()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Mostafaznv\Larupload\Concerns\Storage\Attachment;
4
5
6
use Mostafaznv\Larupload\Larupload;
7
use stdClass;
8
use Illuminate\Http\RedirectResponse;
9
use Symfony\Component\HttpFoundation\StreamedResponse;
10
11
trait RetrieveAttachment
12
{
13
    /**
14
     * Get metadata as an array or object
15
     *
16
     * @param string|null $key
17
     * @return object|string|integer|null
18
     */
19
    public function meta(string $key = null): object|int|string|null
20
    {
21
        if ($key) {
22
            $meta = $this->output;
23
24
            if (array_key_exists($key, $meta)) {
25
                return $meta[$key];
26
            }
27
28
            return null;
29
        }
30
31
        return $this->outputToObject();
0 ignored issues
show
Bug introduced by
It seems like outputToObject() 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

31
        return $this->/** @scrutinizer ignore-call */ outputToObject();
Loading history...
32
    }
33
34
    /**
35
     * Get url for all styles (original, cover and ...) of current entity
36
     *
37
     * @return object
38
     */
39
    public function urls(): object
40
    {
41
        $styles = new stdClass();
42
        $staticStyles = [Larupload::ORIGINAL_FOLDER, Larupload::COVER_FOLDER, Larupload::STREAM_FOLDER];
43
        $allStyles = array_merge(
44
            $staticStyles,
45
            array_keys($this->imageStyles),
46
            array_keys($this->videoStyles),
47
            array_keys($this->audioStyles)
48
        );
49
50
        foreach ($allStyles as $style) {
51
            if ($style == Larupload::COVER_FOLDER and !$this->generateCover) {
52
                $styles->{$style} = null;
53
                continue;
54
            }
55
            else if ($style == Larupload::STREAM_FOLDER and empty($this->streams)) {
56
                unset($styles->{$style});
57
                continue;
58
            }
59
60
            $styles->{$this->nameStyle($style)} = $this->url($style);
0 ignored issues
show
Bug introduced by
It seems like nameStyle() 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

60
            $styles->{$this->/** @scrutinizer ignore-call */ nameStyle($style)} = $this->url($style);
Loading history...
61
        }
62
63
        if ($this->withMeta) {
64
            $styles->meta = $this->meta();
65
        }
66
67
        return $styles;
68
    }
69
70
    /**
71
     * Generate URL for attached file
72
     * Remember, if you are using the local driver, all files that should be publicly accessible should be placed in the storage/app/public directory. Furthermore, you should create a symbolic link at public/storage which points to the  storage/app/public directory
73
     *
74
     * @param string $style
75
     * @return null|string
76
     */
77
    public function url(string $style = Larupload::ORIGINAL_FOLDER): ?string
78
    {
79
        $path = $this->prepareStylePath($style);
0 ignored issues
show
Bug introduced by
It seems like prepareStylePath() 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

79
        /** @scrutinizer ignore-call */ 
80
        $path = $this->prepareStylePath($style);
Loading history...
80
81
        if ($path) {
82
            return $this->storageUrl($path);
0 ignored issues
show
Bug introduced by
It seems like storageUrl() 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

82
            return $this->/** @scrutinizer ignore-call */ storageUrl($path);
Loading history...
83
        }
84
85
        return null;
86
    }
87
88
    /**
89
     * Download attached file
90
     *
91
     * @param string $style
92
     * @return RedirectResponse|StreamedResponse|null
93
     */
94
    public function download(string $style = Larupload::ORIGINAL_FOLDER): StreamedResponse|RedirectResponse|null
95
    {
96
        $path = $this->prepareStylePath($style);
97
98
        if ($path) {
99
            return $this->storageDownload($path);
0 ignored issues
show
Bug introduced by
It seems like storageDownload() 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

99
            return $this->/** @scrutinizer ignore-call */ storageDownload($path);
Loading history...
100
        }
101
102
        return null;
103
    }
104
}
105