@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | * @param string $diskName |
35 | 35 | * @param string $modelClass |
36 | 36 | */ |
37 | - public function __construct( $diskName, $modelClass = ArboryFile::class ) |
|
37 | + public function __construct($diskName, $modelClass = ArboryFile::class) |
|
38 | 38 | { |
39 | 39 | $this->diskName = $diskName; |
40 | 40 | $this->modelClass = $modelClass; |
@@ -47,9 +47,9 @@ discard block |
||
47 | 47 | */ |
48 | 48 | public function getDisk() |
49 | 49 | { |
50 | - if( !$this->disk ) |
|
50 | + if (!$this->disk) |
|
51 | 51 | { |
52 | - $this->disk = Storage::disk( $this->diskName ); |
|
52 | + $this->disk = Storage::disk($this->diskName); |
|
53 | 53 | } |
54 | 54 | |
55 | 55 | return $this->disk; |
@@ -61,38 +61,38 @@ discard block |
||
61 | 61 | * @return ArboryFile|null |
62 | 62 | * @throws RuntimeException |
63 | 63 | */ |
64 | - public function createFromUploadedFile( UploadedFile $file, Model $owner ) |
|
64 | + public function createFromUploadedFile(UploadedFile $file, Model $owner) |
|
65 | 65 | { |
66 | - if( !$file->getRealPath() ) |
|
66 | + if (!$file->getRealPath()) |
|
67 | 67 | { |
68 | - throw new RuntimeException( 'Uploaded file does not have real path' ); |
|
68 | + throw new RuntimeException('Uploaded file does not have real path'); |
|
69 | 69 | } |
70 | 70 | |
71 | - if( !$file->getSize() ) |
|
71 | + if (!$file->getSize()) |
|
72 | 72 | { |
73 | - throw new RuntimeException( sprintf( |
|
73 | + throw new RuntimeException(sprintf( |
|
74 | 74 | 'The uploaded file size must be between 1 and %d (see "upload_max_filesize" in "php.ini") bytes', |
75 | 75 | UploadedFile::getMaxFilesize() ) |
76 | 76 | ); |
77 | 77 | } |
78 | 78 | |
79 | - $localFileName = $this->getLocalFilenameForUploadedFile( $file ); |
|
79 | + $localFileName = $this->getLocalFilenameForUploadedFile($file); |
|
80 | 80 | |
81 | - if( !$this->getDisk()->put( $localFileName, file_get_contents( $file->getRealPath() ) ) ) |
|
81 | + if (!$this->getDisk()->put($localFileName, file_get_contents($file->getRealPath()))) |
|
82 | 82 | { |
83 | - throw new RuntimeException( 'Could not store local file "' . $localFileName . '"' ); |
|
83 | + throw new RuntimeException('Could not store local file "' . $localFileName . '"'); |
|
84 | 84 | } |
85 | 85 | |
86 | 86 | $modelClass = $this->modelClass; |
87 | 87 | $arboryFile = new $modelClass( |
88 | - $this->getCreateAttributesForCreatedFile( $file, $localFileName, $owner ), |
|
88 | + $this->getCreateAttributesForCreatedFile($file, $localFileName, $owner), |
|
89 | 89 | $localFileName |
90 | 90 | ); |
91 | 91 | |
92 | 92 | /* @var $arboryFile ArboryFile */ |
93 | - if( !$arboryFile->save() ) |
|
93 | + if (!$arboryFile->save()) |
|
94 | 94 | { |
95 | - throw new RuntimeException( 'Could not save "' . $modelClass . '" to database' ); |
|
95 | + throw new RuntimeException('Could not save "' . $modelClass . '" to database'); |
|
96 | 96 | } |
97 | 97 | |
98 | 98 | return $arboryFile; |
@@ -105,24 +105,24 @@ discard block |
||
105 | 105 | * @return ArboryFile|null |
106 | 106 | * @throws RuntimeException |
107 | 107 | */ |
108 | - public function createFromBlob( $fileName, $fileContents, Model $owner ) |
|
108 | + public function createFromBlob($fileName, $fileContents, Model $owner) |
|
109 | 109 | { |
110 | - $localFileName = $this->getLocalFilenameForBlob( $fileContents ); |
|
110 | + $localFileName = $this->getLocalFilenameForBlob($fileContents); |
|
111 | 111 | |
112 | - if( !$this->getDisk()->put( $localFileName, $fileContents ) ) |
|
112 | + if (!$this->getDisk()->put($localFileName, $fileContents)) |
|
113 | 113 | { |
114 | - throw new RuntimeException( 'Could not store local file "' . $localFileName . '"' ); |
|
114 | + throw new RuntimeException('Could not store local file "' . $localFileName . '"'); |
|
115 | 115 | } |
116 | 116 | |
117 | 117 | $modelClass = $this->modelClass; |
118 | 118 | $arboryFile = new $modelClass( |
119 | - $this->getCreateAttributesForBlob( $fileName, $fileContents, $localFileName, $owner ) |
|
119 | + $this->getCreateAttributesForBlob($fileName, $fileContents, $localFileName, $owner) |
|
120 | 120 | ); |
121 | 121 | |
122 | 122 | /* @var $arboryFile ArboryFile */ |
123 | - if( !$arboryFile->save() ) |
|
123 | + if (!$arboryFile->save()) |
|
124 | 124 | { |
125 | - throw new RuntimeException( 'Could not save "' . $modelClass . '" to database' ); |
|
125 | + throw new RuntimeException('Could not save "' . $modelClass . '" to database'); |
|
126 | 126 | } |
127 | 127 | |
128 | 128 | return $arboryFile; |
@@ -132,16 +132,16 @@ discard block |
||
132 | 132 | * @param string $fileName |
133 | 133 | * @return string |
134 | 134 | */ |
135 | - protected function getFreeFileName( $fileName ) |
|
135 | + protected function getFreeFileName($fileName) |
|
136 | 136 | { |
137 | 137 | $uploadsDisk = $this->getDisk(); |
138 | 138 | |
139 | - while( $uploadsDisk->exists( $fileName ) ) |
|
139 | + while ($uploadsDisk->exists($fileName)) |
|
140 | 140 | { |
141 | - $fileNameParts = pathinfo( $fileName ); |
|
142 | - $fileName = $fileNameParts['filename'] . '-' . str_random( 10 ); |
|
141 | + $fileNameParts = pathinfo($fileName); |
|
142 | + $fileName = $fileNameParts[ 'filename' ] . '-' . str_random(10); |
|
143 | 143 | |
144 | - if (( $extension = array_get( $fileNameParts, 'extension', false ) )) |
|
144 | + if (($extension = array_get($fileNameParts, 'extension', false))) |
|
145 | 145 | { |
146 | 146 | $fileName .= '.' . $extension; |
147 | 147 | } |
@@ -155,13 +155,13 @@ discard block |
||
155 | 155 | * @param string $localFileName |
156 | 156 | * @return array |
157 | 157 | */ |
158 | - protected function getCreateAttributesForCreatedFile( UploadedFile $file, $localFileName, Model $owner ) |
|
158 | + protected function getCreateAttributesForCreatedFile(UploadedFile $file, $localFileName, Model $owner) |
|
159 | 159 | { |
160 | 160 | $realPath = $file->getRealPath(); |
161 | 161 | |
162 | 162 | return [ |
163 | 163 | 'disk' => $this->diskName, |
164 | - 'sha1' => sha1_file( $realPath ), |
|
164 | + 'sha1' => sha1_file($realPath), |
|
165 | 165 | 'original_name' => $file->getClientOriginalName(), |
166 | 166 | 'local_name' => $localFileName, |
167 | 167 | 'size' => $file->getClientSize(), |
@@ -176,14 +176,14 @@ discard block |
||
176 | 176 | * @param string $localFileName |
177 | 177 | * @return array |
178 | 178 | */ |
179 | - protected function getCreateAttributesForBlob( $originalFileName, $fileContents, $localFileName, Model $owner ) |
|
179 | + protected function getCreateAttributesForBlob($originalFileName, $fileContents, $localFileName, Model $owner) |
|
180 | 180 | { |
181 | 181 | return [ |
182 | 182 | 'disk' => $this->diskName, |
183 | - 'sha1' => sha1( $fileContents ), |
|
183 | + 'sha1' => sha1($fileContents), |
|
184 | 184 | 'original_name' => $originalFileName, |
185 | 185 | 'local_name' => $localFileName, |
186 | - 'size' => strlen( $fileContents ), |
|
186 | + 'size' => strlen($fileContents), |
|
187 | 187 | 'owner_id' => $owner->getKey(), |
188 | 188 | 'owner_type' => $owner->getMorphClass() |
189 | 189 | ]; |
@@ -193,30 +193,30 @@ discard block |
||
193 | 193 | * @param $fileContents |
194 | 194 | * @return string |
195 | 195 | */ |
196 | - protected function getLocalFilenameForBlob( $fileContents ): string |
|
196 | + protected function getLocalFilenameForBlob($fileContents): string |
|
197 | 197 | { |
198 | - return $this->getFreeFileName( sha1( $fileContents ) ); |
|
198 | + return $this->getFreeFileName(sha1($fileContents)); |
|
199 | 199 | } |
200 | 200 | |
201 | 201 | /** |
202 | 202 | * @param UploadedFile $file |
203 | 203 | * @return string |
204 | 204 | */ |
205 | - protected function getLocalFilenameForUploadedFile( UploadedFile $file ): string |
|
205 | + protected function getLocalFilenameForUploadedFile(UploadedFile $file): string |
|
206 | 206 | { |
207 | - return $this->getFreeFileName( sha1_file( $file->getRealPath() ) . '.' . $file->getClientOriginalExtension() ); |
|
207 | + return $this->getFreeFileName(sha1_file($file->getRealPath()) . '.' . $file->getClientOriginalExtension()); |
|
208 | 208 | } |
209 | 209 | |
210 | 210 | /** |
211 | 211 | * @param string $arboryFileId |
212 | 212 | * @return int |
213 | 213 | */ |
214 | - public function delete( $arboryFileId ) |
|
214 | + public function delete($arboryFileId) |
|
215 | 215 | { |
216 | - $arboryFile = $this->find( $arboryFileId ); |
|
216 | + $arboryFile = $this->find($arboryFileId); |
|
217 | 217 | |
218 | - $this->getDisk()->delete( $arboryFile->getLocalName() ); |
|
218 | + $this->getDisk()->delete($arboryFile->getLocalName()); |
|
219 | 219 | |
220 | - return parent::delete( $arboryFileId ); |
|
220 | + return parent::delete($arboryFileId); |
|
221 | 221 | } |
222 | 222 | } |