1 | <?php |
||||||
2 | |||||||
3 | namespace ByTIC\MediaLibrary\Media\Traits; |
||||||
4 | |||||||
5 | use ByTIC\MediaLibrary\Loaders\Database; |
||||||
6 | use ByTIC\MediaLibrary\Support\MediaModels; |
||||||
7 | use Exception; |
||||||
8 | use League\Flysystem\File as FileLeague; |
||||||
9 | use Nip\Filesystem\File; |
||||||
10 | |||||||
11 | /** |
||||||
12 | * Trait FileMethodsTrait. |
||||||
13 | */ |
||||||
14 | trait FileMethodsTrait |
||||||
15 | { |
||||||
16 | /** |
||||||
17 | * @var File|FileLeague |
||||||
18 | */ |
||||||
19 | protected $file; |
||||||
20 | |||||||
21 | /** |
||||||
22 | * @return File |
||||||
23 | */ |
||||||
24 | 7 | public function getFile() |
|||||
25 | { |
||||||
26 | 7 | return $this->file; |
|||||
27 | } |
||||||
28 | |||||||
29 | /** |
||||||
30 | * @param File $file |
||||||
31 | */ |
||||||
32 | 7 | public function setFile(File $file) |
|||||
33 | { |
||||||
34 | 7 | $this->file = $file; |
|||||
35 | 7 | } |
|||||
36 | |||||||
37 | /** |
||||||
38 | * @return bool |
||||||
39 | */ |
||||||
40 | 2 | public function hasFile() |
|||||
41 | { |
||||||
42 | 2 | return $this->getFile() instanceof File; |
|||||
43 | } |
||||||
44 | |||||||
45 | /** |
||||||
46 | * @param $path |
||||||
47 | * @param $contents |
||||||
48 | */ |
||||||
49 | 2 | public function generateFileFromContent($path, $contents) |
|||||
50 | { |
||||||
51 | 2 | $this->getCollection()->getFilesystem()->put( |
|||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
52 | 2 | $path, |
|||||
53 | $contents |
||||||
54 | ); |
||||||
55 | |||||||
56 | 2 | $file = new File($this->getCollection()->getFilesystem(), $path); |
|||||
57 | 2 | $this->setFile($file); |
|||||
58 | 2 | } |
|||||
59 | |||||||
60 | /** |
||||||
61 | * @return string |
||||||
62 | */ |
||||||
63 | 7 | public function getName() |
|||||
64 | { |
||||||
65 | 7 | return $this->getFile()->getName(); |
|||||
66 | } |
||||||
67 | |||||||
68 | /** |
||||||
69 | * @return string |
||||||
70 | */ |
||||||
71 | 1 | public function read() |
|||||
72 | { |
||||||
73 | 1 | return $this->getFile()->read(); |
|||||
74 | } |
||||||
75 | |||||||
76 | /** |
||||||
77 | * @return bool |
||||||
78 | */ |
||||||
79 | 2 | public function delete() |
|||||
80 | { |
||||||
81 | 2 | $this->deleteMediaFromFilesystem(); |
|||||
82 | 2 | $this->deleteMediaFromDatabase(); |
|||||
83 | 2 | return true; |
|||||
84 | } |
||||||
85 | |||||||
86 | 2 | protected function deleteMediaFromDatabase() |
|||||
87 | { |
||||||
88 | 2 | $loader = $this->getCollection()->getLoader(); |
|||||
89 | 2 | if ($loader instanceof Database) { |
|||||
90 | MediaModels::records()->deteleMedia($this); |
||||||
91 | } |
||||||
92 | 2 | } |
|||||
93 | |||||||
94 | 2 | protected function deleteMediaFromFilesystem() |
|||||
95 | { |
||||||
96 | 2 | $this->removeConversions(); |
|||||
0 ignored issues
–
show
It seems like
removeConversions() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
97 | 2 | if ($this->getFile()->exists()) { |
|||||
98 | 2 | $this->getFile()->delete(); |
|||||
99 | } |
||||||
100 | 2 | } |
|||||
101 | |||||||
102 | /** |
||||||
103 | * Get the path to the original media file. |
||||||
104 | * |
||||||
105 | * @param string $conversionName |
||||||
106 | * |
||||||
107 | * @throws Exception |
||||||
108 | * |
||||||
109 | * @return string |
||||||
110 | */ |
||||||
111 | 2 | public function getPath(string $conversionName = ''): string |
|||||
112 | { |
||||||
113 | 2 | if (!$this->hasFile()) { |
|||||
114 | throw new Exception('Error getting path for media with no file'); |
||||||
115 | } |
||||||
116 | |||||||
117 | 2 | if ($conversionName) { |
|||||
118 | 2 | return $this->getBasePath($conversionName) . '/' . $this->getName(); |
|||||
0 ignored issues
–
show
It seems like
getBasePath() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
119 | } |
||||||
120 | |||||||
121 | return $this->getFile()->getPath(); |
||||||
122 | } |
||||||
123 | } |
||||||
124 |