1
|
|
|
<?php namespace Modules\Media\Repositories\Eloquent; |
2
|
|
|
|
3
|
|
|
use Illuminate\Database\Eloquent\Collection; |
4
|
|
|
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository; |
5
|
|
|
use Modules\Media\Entities\File; |
6
|
|
|
use Modules\Media\Helpers\FileHelper; |
7
|
|
|
use Modules\Media\Repositories\FileRepository; |
8
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
9
|
|
|
|
10
|
|
|
class EloquentFileRepository extends EloquentBaseRepository implements FileRepository |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Update a resource |
14
|
|
|
* @param File $file |
15
|
|
|
* @param $data |
16
|
|
|
* @return mixed |
17
|
|
|
*/ |
18
|
|
|
public function update($file, $data) |
19
|
|
|
{ |
20
|
|
|
$file->update($data); |
21
|
|
|
|
22
|
|
|
return $file; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Create a file row from the given file |
27
|
|
|
* @param UploadedFile $file |
28
|
|
|
* @return mixed |
29
|
|
|
*/ |
30
|
|
|
public function createFromFile(UploadedFile $file) |
31
|
|
|
{ |
32
|
|
|
$fileName = FileHelper::slug($file->getClientOriginalName()); |
33
|
|
|
|
34
|
|
|
$exists = $this->model->whereFilename($fileName)->first(); |
35
|
|
|
|
36
|
|
|
if ($exists) { |
37
|
|
|
throw new \InvalidArgumentException('File slug already exists'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $this->model->create([ |
41
|
|
|
'filename' => $fileName, |
42
|
|
|
'path' => config('asgard.media.config.files-path') . "{$fileName}", |
43
|
|
|
'extension' => $file->guessClientExtension(), |
44
|
|
|
'mimetype' => $file->getClientMimeType(), |
45
|
|
|
'filesize' => $file->getFileInfo()->getSize(), |
46
|
|
|
'folder_id' => 0, |
47
|
|
|
]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function destroy($file) |
51
|
|
|
{ |
52
|
|
|
$file->delete(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Find a file for the entity by zone |
57
|
|
|
* @param $zone |
58
|
|
|
* @param object $entity |
59
|
|
|
* @return object |
60
|
|
|
*/ |
61
|
|
|
public function findFileByZoneForEntity($zone, $entity) |
62
|
|
|
{ |
63
|
|
|
foreach ($entity->files as $file) { |
64
|
|
|
if ($file->pivot->zone == $zone) { |
65
|
|
|
return $file; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return ''; |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Find multiple files for the given zone and entity |
74
|
|
|
* @param zone $zone |
75
|
|
|
* @param object $entity |
76
|
|
|
* @return object |
77
|
|
|
*/ |
78
|
|
|
public function findMultipleFilesByZoneForEntity($zone, $entity) |
79
|
|
|
{ |
80
|
|
|
$files = []; |
81
|
|
|
foreach ($entity->files as $file) { |
82
|
|
|
if ($file->pivot->zone == $zone) { |
83
|
|
|
$files[] = $file; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return new Collection($files); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.