1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HustleWorks\Chute; |
4
|
|
|
|
5
|
|
|
use HustleWorks\Chute\DTO\ImageConfiguration; |
6
|
|
|
use HustleWorks\Chute\Contracts\ImageValidatorInterface; |
7
|
|
|
use HustleWorks\Chute\Contracts\ImageRepositoryInterface; |
8
|
|
|
use HustleWorks\Chute\Contracts\StorageInterface; |
9
|
|
|
use HustleWorks\Chute\DTO\ImageFile; |
10
|
|
|
use HustleWorks\Chute\ServiceResponse; |
11
|
|
|
use HustleWorks\Chute\StandardServiceResponse; |
12
|
|
|
|
13
|
|
|
abstract class ImageUploader |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var ImageValidatorInterface |
17
|
|
|
*/ |
18
|
|
|
protected $validator; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var StorageInterface |
22
|
|
|
*/ |
23
|
|
|
protected $storage; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ImageRepositoryInterface |
27
|
|
|
*/ |
28
|
|
|
protected $image_repo; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* ImageUploader constructor. |
32
|
|
|
* |
33
|
|
|
* @param ImageValidatorInterface $validator |
34
|
|
|
* @param StorageInterface $storage |
35
|
|
|
* @param ImageRepositoryInterface $image_repo |
36
|
|
|
*/ |
37
|
|
|
public function __construct( |
38
|
|
|
ImageValidatorInterface $validator, |
39
|
|
|
StorageInterface $storage, |
40
|
|
|
ImageRepositoryInterface $image_repo |
41
|
|
|
) |
42
|
|
|
{ |
43
|
|
|
$this->validator = $validator; |
44
|
|
|
$this->storage = $storage; |
45
|
|
|
$this->image_repo = $image_repo; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Handle Image Upload |
50
|
|
|
* |
51
|
|
|
* @param mixed $framework_file |
52
|
|
|
* @param ImageConfiguration $config |
53
|
|
|
* @param array $optional_data |
54
|
|
|
* @return ServiceResponse |
55
|
|
|
*/ |
56
|
|
|
public function handle($framework_file, ImageConfiguration $config, array $optional_data = []) |
57
|
|
|
{ |
58
|
|
|
/* Create ImageFile object for processing */ |
59
|
|
|
$image_file = $this->prepareImageFile($framework_file); |
60
|
|
|
|
61
|
|
|
/* run validation on upload */ |
62
|
|
|
$response = $this->validator->validate($image_file, $config->rules); |
63
|
|
|
|
64
|
|
|
/* if valid create database entry, store image to disk and generate response */ |
65
|
|
|
if ($response->success()) { |
66
|
|
|
|
67
|
|
|
if ($record = $this->image_repo->findExistingImageable($config->optional_data)) { |
68
|
|
|
$this->image_repo->delete($record); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$record = $this->image_repo->create([ |
72
|
|
|
'name' => $config->image_name, |
73
|
|
|
'disk' => $config->temp_disk, |
74
|
|
|
'filename' => $image_file->filename, |
75
|
|
|
'path' => $config->directory, |
76
|
|
|
'status' => 'pending', |
77
|
|
|
'width' => $image_file->width, |
78
|
|
|
'height' => $image_file->height, |
79
|
|
|
'mime_type' => $image_file->mime_type, |
80
|
|
|
'extension' => $image_file->extension, |
81
|
|
|
'size' => $image_file->size, |
82
|
|
|
'alt' => $optional_data['alt'] ?? null, |
83
|
|
|
'title' => $optional_data['title'] ?? null, |
84
|
|
|
'description' => $optional_data['description'] ?? null, |
85
|
|
|
]); |
86
|
|
|
|
87
|
|
|
$this->storage->put($image_file->stream, $record->disk, "$record->path/$record->uuid", $record->filename); |
|
|
|
|
88
|
|
|
$response = new StandardServiceResponse(true, [ |
89
|
|
|
'image' => $record, |
90
|
|
|
], 'File successfully uploaded'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $response; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Prepare Image File |
99
|
|
|
* |
100
|
|
|
* Take raw upload and convert it to an image file |
101
|
|
|
* |
102
|
|
|
* @param $framework_file |
103
|
|
|
* @return ImageFile |
104
|
|
|
*/ |
105
|
|
|
abstract protected function prepareImageFile($framework_file): ImageFile; |
106
|
|
|
} |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.