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
|
|
|
public function handle($event = null) |
11
|
|
|
{ |
12
|
|
|
if ($event instanceof StoringMedia) { |
13
|
|
|
$this->handleMultiMedia($event); |
14
|
|
|
|
15
|
|
|
$this->handleSingleMedia($event); |
16
|
|
|
} |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Handle the request for the multi media partial |
21
|
|
|
* @param StoringMedia $event |
22
|
|
|
*/ |
23
|
|
|
private function handleMultiMedia(StoringMedia $event) |
24
|
|
|
{ |
25
|
|
|
$entity = $event->getEntity(); |
26
|
|
|
$postMedias = array_get($event->getSubmissionData(), 'medias_multi', []); |
27
|
|
|
|
28
|
|
|
foreach ($postMedias as $zone => $attributes) { |
29
|
|
|
$orders = $this->getOrdersFrom($attributes); |
30
|
|
|
foreach ($attributes['files'] as $fileId) { |
31
|
|
|
$order = array_search($fileId, $orders); |
32
|
|
|
$entity->files()->attach($fileId, ['imageable_type' => get_class($entity), 'zone' => $zone, 'order' => $order]); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Handle the request to parse single media partials |
39
|
|
|
* @param StoringMedia $event |
40
|
|
|
*/ |
41
|
|
|
private function handleSingleMedia(StoringMedia $event) |
42
|
|
|
{ |
43
|
|
|
$entity = $event->getEntity(); |
44
|
|
|
$postMedia = array_get($event->getSubmissionData(), 'medias_single', []); |
45
|
|
|
|
46
|
|
|
foreach ($postMedia as $zone => $fileId) { |
47
|
|
|
$entity->files()->attach($fileId, ['imageable_type' => get_class($entity), 'zone' => $zone, 'order' => null]); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Parse the orders input and return an array of file ids, in order |
53
|
|
|
* @param array $attributes |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
private function getOrdersFrom(array $attributes) |
57
|
|
|
{ |
58
|
|
|
$orderString = array_get($attributes, 'orders'); |
59
|
|
|
|
60
|
|
|
if ($orderString === null) { |
61
|
|
|
return []; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$orders = explode(',', $orderString); |
65
|
|
|
|
66
|
|
|
return array_filter($orders); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|