Completed
Push — master ( 4434b2...6b8e46 )
by Yaro
02:04
created

Storage::storeFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
1
<?php
2
3
namespace Yaro\Jarboe\Table\Fields\Traits;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\UploadedFile;
7
use Illuminate\Support\Facades\Storage as IlluminateStorage;
8
9
trait Storage
10
{
11
    protected $disk;
12
    protected $path = '';
13
    protected $multiple = false;
14
15
    protected function storeFile(UploadedFile $file, $filename, Request $request)
16
    {
17
        $path = $file->storeAs($this->getPath(), $filename, [
18
            'disk' => $this->getDisk(),
19
        ]);
20
21
        return $path;
22
    }
23
24 1
    public function multiple(bool $multiple = true)
25
    {
26 1
        $this->multiple = $multiple;
27
28 1
        return $this;
29
    }
30
31 2
    public function isMultiple()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
32
    {
33 2
        return $this->multiple;
34
    }
35
36
    public function disk(string $disk)
37
    {
38
        $this->disk = $disk;
39
40
        return $this;
41
    }
42
43
    public function getDisK()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
44
    {
45
        return $this->disk;
46
    }
47
48
    public function path(string $path)
49
    {
50
        $this->path = $path;
51
52
        return $this;
53
    }
54
55
    public function getPath()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
56
    {
57
        return $this->path;
58
    }
59
60
    public function getUrl($model)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
61
    {
62
        if (is_null($model)) {
63
            return null;
64
        }
65
66
        $filepath = $model->{$this->name()};
0 ignored issues
show
Bug introduced by
It seems like name() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
67
        if (!$filepath) {
68
            return null;
69
        }
70
71
        $disk = IlluminateStorage::disk($this->getDisk());
72
73
        $paths = [];
74
        if (is_array($filepath)) {
75
            foreach ($filepath as $path) {
76
                $paths[] = $this->isEncode() ? $path : $disk->url($path);
0 ignored issues
show
Bug introduced by
It seems like isEncode() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
77
            }
78
            return $paths;
79
        }
80
81
        return $this->isEncode() ? $filepath : $disk->url($filepath);
0 ignored issues
show
Bug introduced by
It seems like isEncode() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
82
    }
83
}
84