Test Failed
Branch master (df1c42)
by Mostafa
15:23
created

RetrieveAttachment::download()   A

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
        );
48
49
        foreach ($allStyles as $style) {
50
            if ($style == Larupload::COVER_FOLDER and !$this->generateCover) {
51
                $styles->{$style} = null;
52
                continue;
53
            }
54
            else if ($style == Larupload::STREAM_FOLDER and empty($this->streams)) {
55
                unset($styles->{$style});
56
                continue;
57
            }
58
59
            $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

59
            $styles->{$this->/** @scrutinizer ignore-call */ nameStyle($style)} = $this->url($style);
Loading history...
60
        }
61
62
        if ($this->withMeta) {
63
            $styles->meta = $this->meta();
64
        }
65
66
        return $styles;
67
    }
68
69
    /**
70
     * Generate URL for attached file
71
     * 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
72
     *
73
     * @param string $style
74
     * @return null|string
75
     */
76
    public function url(string $style = Larupload::ORIGINAL_FOLDER): ?string
77
    {
78
        $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

78
        /** @scrutinizer ignore-call */ 
79
        $path = $this->prepareStylePath($style);
Loading history...
79
80
        if ($path) {
81
            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

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

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