|
1
|
|
|
<?php namespace Modules\Media\Events\Handlers; |
|
2
|
|
|
|
|
3
|
|
|
use Modules\Media\Contracts\StoringMedia; |
|
4
|
|
|
use Modules\Media\Image\Imagy; |
|
5
|
|
|
use Modules\Media\Repositories\FileRepository; |
|
6
|
|
|
use Modules\Media\Services\FileService; |
|
7
|
|
|
|
|
8
|
|
|
class HandleMediaStorage |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var FileService |
|
12
|
|
|
*/ |
|
13
|
|
|
private $fileService; |
|
14
|
|
|
/** |
|
15
|
|
|
* @var FileRepository |
|
16
|
|
|
*/ |
|
17
|
|
|
private $file; |
|
18
|
|
|
/** |
|
19
|
|
|
* @var Imagy |
|
20
|
|
|
*/ |
|
21
|
|
|
private $imagy; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(FileService $fileService, FileRepository $file, Imagy $imagy) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->fileService = $fileService; |
|
26
|
|
|
$this->file = $file; |
|
27
|
|
|
$this->imagy = $imagy; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function handle(StoringMedia $event) |
|
31
|
|
|
{ |
|
32
|
|
|
$entity = $event->getEntity(); |
|
33
|
|
|
$zone = $this->getZoneFrom($event->getSubmissionData()); |
|
34
|
|
|
$postMedias = $this->getPostedMediasFrom($event->getSubmissionData()); |
|
35
|
|
|
$orders = $this->getOrdersFrom($event->getSubmissionData()); |
|
36
|
|
|
|
|
37
|
|
|
foreach ($postMedias as $key => $fileId) { |
|
38
|
|
|
$order = array_search($fileId, $orders); |
|
39
|
|
|
$entity->files()->attach($fileId, ['imageable_type' => get_class($entity), 'zone' => $zone, 'order' => $order]); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
private function getZoneFrom(array $submissionData) |
|
44
|
|
|
{ |
|
45
|
|
|
return array_get($submissionData, 'zone'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private function getPostedMediasFrom(array $submissionData) |
|
49
|
|
|
{ |
|
50
|
|
|
return array_get($submissionData, 'medias', []); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
private function getOrdersFrom(array $submissionData) |
|
54
|
|
|
{ |
|
55
|
|
|
$orderString = array_get($submissionData, 'orders'); |
|
56
|
|
|
|
|
57
|
|
|
return explode(',', $orderString); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|