1 | <?php |
||||||
2 | |||||||
3 | namespace Itstructure\MFU\Services; |
||||||
4 | |||||||
5 | use Exception; |
||||||
6 | use Illuminate\Http\UploadedFile; |
||||||
7 | use Illuminate\Support\MessageBag; |
||||||
8 | use Itstructure\MFU\Processors\{ |
||||||
9 | BaseProcessor, SaveProcessor, UploadProcessor, UpdateProcessor, DeleteProcessor |
||||||
10 | }; |
||||||
11 | use Itstructure\MFU\Models\Mediafile; |
||||||
12 | |||||||
13 | /** |
||||||
14 | * Class Uploader |
||||||
15 | * @package Itstructure\MFU\Services |
||||||
16 | * @author Andrey Girnik <[email protected]> |
||||||
17 | */ |
||||||
18 | class Uploader |
||||||
19 | { |
||||||
20 | /** |
||||||
21 | * @var array |
||||||
22 | */ |
||||||
23 | private $config; |
||||||
24 | |||||||
25 | /** |
||||||
26 | * @var SaveProcessor|BaseProcessor |
||||||
27 | */ |
||||||
28 | private $processor; |
||||||
29 | |||||||
30 | /** |
||||||
31 | * @param array $config |
||||||
32 | * @return Uploader |
||||||
33 | */ |
||||||
34 | public static function getInstance(array $config = []): self |
||||||
35 | { |
||||||
36 | return new static($config); |
||||||
37 | } |
||||||
38 | |||||||
39 | /** |
||||||
40 | * @param array $data |
||||||
41 | * @param UploadedFile $file |
||||||
42 | * @throws Exception |
||||||
43 | * @return bool |
||||||
44 | */ |
||||||
45 | public function upload(array $data, UploadedFile $file = null): bool |
||||||
46 | { |
||||||
47 | $this->processor = UploadProcessor::getInstance($this->config) |
||||||
48 | ->setMediafileModel(new Mediafile()) |
||||||
49 | ->setData($data) |
||||||
50 | ->setFile($file); |
||||||
51 | |||||||
52 | return $this->save(); |
||||||
53 | } |
||||||
54 | |||||||
55 | /** |
||||||
56 | * @param int $id |
||||||
57 | * @param array $data |
||||||
58 | * @param UploadedFile|null $file |
||||||
59 | * @throws Exception |
||||||
60 | * @return bool |
||||||
61 | */ |
||||||
62 | public function update(int $id, array $data, UploadedFile $file = null): bool |
||||||
63 | { |
||||||
64 | $this->processor = UpdateProcessor::getInstance($this->config) |
||||||
65 | ->setMediafileModel(Mediafile::find($id)) |
||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
66 | ->setData($data) |
||||||
67 | ->setFile($file); |
||||||
68 | |||||||
69 | return $this->save(); |
||||||
70 | } |
||||||
71 | |||||||
72 | /** |
||||||
73 | * @param int $id |
||||||
74 | * @throws Exception |
||||||
75 | * @return bool |
||||||
76 | */ |
||||||
77 | public function delete(int $id): bool |
||||||
78 | { |
||||||
79 | $this->processor = DeleteProcessor::getInstance() |
||||||
80 | ->setMediafileModel(Mediafile::find($id)); |
||||||
0 ignored issues
–
show
It seems like
Itstructure\MFU\Models\Mediafile::find($id) can also be of type null ; however, parameter $model of Itstructure\MFU\Processo...or::setMediafileModel() does only seem to accept Itstructure\MFU\Models\Mediafile , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
81 | |||||||
82 | return $this->processor->run(); |
||||||
83 | } |
||||||
84 | |||||||
85 | /** |
||||||
86 | * @return bool |
||||||
87 | */ |
||||||
88 | public function hasErrors(): bool |
||||||
89 | { |
||||||
90 | return !is_null($this->processor->getErrors()); |
||||||
91 | } |
||||||
92 | |||||||
93 | /** |
||||||
94 | * @return MessageBag|null |
||||||
95 | */ |
||||||
96 | public function getErrors(): ?MessageBag |
||||||
97 | { |
||||||
98 | return $this->processor->getErrors(); |
||||||
99 | } |
||||||
100 | |||||||
101 | /** |
||||||
102 | * @return int |
||||||
103 | */ |
||||||
104 | public function getId(): int |
||||||
105 | { |
||||||
106 | return $this->processor->getId(); |
||||||
107 | } |
||||||
108 | |||||||
109 | /** |
||||||
110 | * @param string|null $key |
||||||
111 | * @return array |
||||||
112 | */ |
||||||
113 | public function getConfig(string $key = null) |
||||||
114 | { |
||||||
115 | return !empty($key) ? $this->config[$key] : $this->config; |
||||||
116 | } |
||||||
117 | |||||||
118 | /** |
||||||
119 | * UploadService constructor. |
||||||
120 | * @param array $config |
||||||
121 | */ |
||||||
122 | private function __construct(array $config = []) |
||||||
123 | { |
||||||
124 | $this->config = $config; |
||||||
125 | } |
||||||
126 | |||||||
127 | /** |
||||||
128 | * @return bool |
||||||
129 | */ |
||||||
130 | private function save(): bool |
||||||
131 | { |
||||||
132 | if (!$this->processor->run()) { |
||||||
133 | return false; |
||||||
134 | } |
||||||
135 | |||||||
136 | if ($this->processor->getMediafileModel()->isImage()) { |
||||||
137 | $this->processor->createThumbs(); |
||||||
0 ignored issues
–
show
The method
createThumbs() does not exist on Itstructure\MFU\Processors\BaseProcessor . It seems like you code against a sub-type of Itstructure\MFU\Processors\BaseProcessor such as Itstructure\MFU\Processors\SaveProcessor .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
138 | } |
||||||
139 | |||||||
140 | return true; |
||||||
141 | } |
||||||
142 | } |
||||||
143 |