Completed
Push — master ( 2c4f4a...51d604 )
by Joseph
01:42
created

UploadsFiles::preparePaths()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
rs 9.7333
cc 4
nc 3
nop 2
1
<?php
2
namespace STS\StorageConnect\Models\Concerns;
3
4
use Carbon\Carbon;
5
use Illuminate\Database\Eloquent\Model;
6
use STS\StorageConnect\Contracts\UploadTarget;
7
use STS\StorageConnect\Events\UploadFailed;
8
use STS\StorageConnect\Events\UploadRetrying;
9
use STS\StorageConnect\Events\UploadSucceeded;
10
use STS\StorageConnect\Exceptions\UploadException;
11
use STS\StorageConnect\Jobs\UploadFile;
12
13
trait UploadsFiles
14
{
15
    /**
16
     * @param      $source
17
     * @param      $destinationPath
18
     * @param bool $shouldQueue
19
     * @param null $queueJob
20
     *
21
     * @return bool
22
     */
23
    public function upload($source, $destinationPath = null, $shouldQueue = true, $queueJob = null)
24
    {
25
        $this->verify();
0 ignored issues
show
Bug introduced by
It seems like verify() 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...
26
27
        if ($shouldQueue) {
28
            return dispatch(new UploadFile($source, $destinationPath, $this));
29
        }
30
31
        list($sourcePath, $destinationPath, $targetModel) = $this->preparePaths($source, $destinationPath);
32
33
        try {
34
            return $this->handleUpload($sourcePath, $destinationPath, $targetModel);
35
        } catch (UploadException $exception) {
36
            $this->handleUploadError($exception, $queueJob, $targetModel);
37
        }
38
39
        $this->ping();
0 ignored issues
show
Bug introduced by
It seems like ping() 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...
40
41
        return false;
42
    }
43
44
    /**
45
     * @param $source
46
     * @param null $destinationPath
47
     *
48
     * @return array
49
     */
50
    protected function preparePaths($source, $destinationPath = null)
51
    {
52
        if($source instanceof Model && $source instanceof UploadTarget) {
53
            return [
54
                $source->upload_source_path,
55
                $destinationPath ?: $source->upload_destination_path,
56
                $source
57
            ];
58
        }
59
60
        return [
61
            (string) $source,
62
            $destinationPath,
63
            null
64
        ];
65
    }
66
67
    /**
68
     * @param $sourcePath
69
     * @param $destinationPath
70
     * @param null $targetModel
71
     *
72
     * @return bool
73
     */
74
    protected function handleUpload($sourcePath, $destinationPath, $targetModel = null)
75
    {
76
        if (starts_with($sourcePath, "s3://")) {
77
            app('aws')->createClient('s3')->registerStreamWrapper();
78
        }
79
80
        $this->adapter()->upload($sourcePath, $destinationPath);
0 ignored issues
show
Bug introduced by
It seems like adapter() 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...
81
82
        $this->uploaded_at = Carbon::now();
0 ignored issues
show
Bug introduced by
The property uploaded_at does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
83
        $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() 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...
84
85
        event(new UploadSucceeded($this, $sourcePath, $destinationPath, $targetModel));
86
87
        return true;
88
    }
89
90
    /**
91
     * @param UploadException $exception
92
     * @param null $job
93
     * @param null $targetModel
94
     *
95
     * @return mixed
96
     */
97
    protected function handleUploadError(UploadException $exception, $job = null, $targetModel = null)
98
    {
99
        $exception->setStorage($this);
100
101
        if ($exception->shouldRetry() && $job) {
102
            event(new UploadRetrying($this, $exception, $targetModel));
103
104
            $job->release();
105
106
            return;
107
        }
108
109
        if ($exception->shouldDisable()) {
110
            $this->disable($exception->getReason());
0 ignored issues
show
Bug introduced by
It seems like disable() 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...
111
        }
112
113
        if ($job) {
114
            $job->fail($exception);
115
        }
116
117
        event(new UploadFailed($this, $exception, $targetModel));
118
    }
119
}