1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace STS\StorageConnect\Models\Concerns; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use STS\StorageConnect\Contracts\UploadTarget; |
8
|
|
|
use STS\StorageConnect\Events\UploadFailed; |
9
|
|
|
use STS\StorageConnect\Events\UploadInProgress; |
10
|
|
|
use STS\StorageConnect\Events\UploadRetrying; |
11
|
|
|
use STS\StorageConnect\Events\UploadStarted; |
12
|
|
|
use STS\StorageConnect\Events\UploadSucceeded; |
13
|
|
|
use STS\StorageConnect\Exceptions\UploadException; |
14
|
|
|
use STS\StorageConnect\Jobs\CheckUploadStatus; |
15
|
|
|
use STS\StorageConnect\Jobs\UploadFile; |
16
|
|
|
use STS\StorageConnect\UploadRequest; |
17
|
|
|
use STS\StorageConnect\UploadResponse; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @property Carbon uploaded_at |
21
|
|
|
*/ |
22
|
|
|
trait UploadsFiles |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @param $source |
26
|
|
|
* @param $destinationPath |
27
|
|
|
* @param bool $shouldQueue |
28
|
|
|
* @param null $queueJob |
29
|
|
|
* |
30
|
|
|
* @return UploadResponse|bool |
31
|
|
|
*/ |
32
|
|
|
public function upload( $source, $destinationPath = null, $shouldQueue = true, $queueJob = null ) |
33
|
|
|
{ |
34
|
|
|
$this->verify(); |
|
|
|
|
35
|
|
|
|
36
|
|
|
if ($shouldQueue) { |
37
|
|
|
return dispatch(new UploadFile($source, $destinationPath, $this)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$request = new UploadRequest($source, $destinationPath); |
41
|
|
|
|
42
|
|
|
try { |
43
|
|
|
return $this->handleUpload($request); |
44
|
|
|
} catch (UploadException $exception) { |
45
|
|
|
$this->handleUploadError($exception, $queueJob); |
46
|
|
|
|
47
|
|
|
return false; |
48
|
|
|
} finally { |
49
|
|
|
$this->ping(); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param UploadRequest $request |
55
|
|
|
* |
56
|
|
|
* @return UploadResponse |
57
|
|
|
*/ |
58
|
|
|
protected function handleUpload( UploadRequest $request ) |
59
|
|
|
{ |
60
|
|
|
return $this->processResponse($this->adapter()->upload($request)); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param UploadResponse $response |
65
|
|
|
*/ |
66
|
|
|
public function checkUploadStatus( UploadResponse $response ) |
67
|
|
|
{ |
68
|
|
|
$this->processResponse($this->adapter()->checkUploadStatus($response)); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param UploadResponse $response |
73
|
|
|
* |
74
|
|
|
* @return UploadResponse |
75
|
|
|
*/ |
76
|
|
|
protected function processResponse( UploadResponse $response ) |
77
|
|
|
{ |
78
|
|
|
if ($response->isAsync()) { |
79
|
|
|
dispatch(new CheckUploadStatus($this, $response)); |
80
|
|
|
|
81
|
|
|
if ($response->getStatusChecks() == 0) { |
82
|
|
|
event(new UploadStarted($this, $response)); |
83
|
|
|
} else { |
84
|
|
|
event(new UploadInProgress($this, $response)); |
85
|
|
|
} |
86
|
|
|
} else { |
87
|
|
|
$this->uploaded_at = Carbon::now(); |
88
|
|
|
$this->save(); |
|
|
|
|
89
|
|
|
|
90
|
|
|
event(new UploadSucceeded($this, $response)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $response; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param UploadException $exception |
98
|
|
|
* @param null $job |
99
|
|
|
* |
100
|
|
|
* @return mixed |
101
|
|
|
*/ |
102
|
|
|
protected function handleUploadError( UploadException $exception, $job = null ) |
103
|
|
|
{ |
104
|
|
|
$exception->setStorage($this); |
105
|
|
|
|
106
|
|
|
if ($exception->shouldRetry() && $job) { |
107
|
|
|
event(new UploadRetrying($this, $exception)); |
108
|
|
|
|
109
|
|
|
$job->release(); |
110
|
|
|
|
111
|
|
|
return; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if ($exception->shouldDisable()) { |
115
|
|
|
$this->disable($exception->getReason()); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if ($job) { |
119
|
|
|
$job->fail($exception); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
event(new UploadFailed($this, $exception)); |
123
|
|
|
} |
124
|
|
|
} |
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
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. 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.