Skip to content

Commit 14dc886

Browse files
committed
Fix for #28
1 parent 9765bf6 commit 14dc886

File tree

2 files changed

+71
-11
lines changed

2 files changed

+71
-11
lines changed

src/BunnyCDNAdapter.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use League\Flysystem\Visibility;
2424
use League\MimeTypeDetection\FinfoMimeTypeDetector;
2525
use RuntimeException;
26+
use TypeError;
2627

2728
class BunnyCDNAdapter implements FilesystemAdapter
2829
{
@@ -280,6 +281,8 @@ public function visibility(string $path): FileAttributes
280281
return new FileAttributes($this->getObject($path)->path(), null, $this->pullzone_url ? 'public' : 'private');
281282
} catch (UnableToReadFile $e) {
282283
throw new UnableToRetrieveMetadata($e->getMessage());
284+
} catch (TypeError $e) {
285+
throw new UnableToRetrieveMetadata('Cannot retrieve visibility of folder');
283286
}
284287
}
285288

@@ -296,8 +299,8 @@ public function mimeType(string $path): FileAttributes
296299
try {
297300
$object = $this->getObject($path);
298301

299-
if ($object instanceof DirectoryAttributes) {
300-
throw new UnableToRetrieveMetadata('Cannot retrieve mimetype of folder');
302+
if($object instanceof DirectoryAttributes) {
303+
throw new TypeError();
301304
}
302305

303306
/** @var FileAttributes $object */
@@ -320,6 +323,8 @@ public function mimeType(string $path): FileAttributes
320323
return $object;
321324
} catch (UnableToReadFile $e) {
322325
throw new UnableToRetrieveMetadata($e->getMessage());
326+
} catch (TypeError $e) {
327+
throw new UnableToRetrieveMetadata('Cannot retrieve mimeType of folder');
323328
}
324329
}
325330

@@ -356,6 +361,8 @@ public function lastModified(string $path): FileAttributes
356361
return $this->getObject($path);
357362
} catch (UnableToReadFile $e) {
358363
throw new UnableToRetrieveMetadata($e->getMessage());
364+
} catch (TypeError $e) {
365+
throw new UnableToRetrieveMetadata('Last Modified only accepts files as parameters, not directories');
359366
}
360367
}
361368

@@ -366,17 +373,11 @@ public function lastModified(string $path): FileAttributes
366373
public function fileSize(string $path): FileAttributes
367374
{
368375
try {
369-
$object = $this->getObject($path);
370-
371-
if ($object instanceof DirectoryAttributes) {
372-
// @codeCoverageIgnoreStart
373-
throw new UnableToRetrieveMetadata('Cannot retrieve size of folder');
374-
// @codeCoverageIgnoreEnd
375-
}
376-
377-
return $object;
376+
return $this->getObject($path);
378377
} catch (UnableToReadFile $e) {
379378
throw new UnableToRetrieveMetadata($e->getMessage());
379+
} catch (TypeError $e) {
380+
throw new UnableToRetrieveMetadata('Cannot retrieve size of folder');
380381
}
381382
}
382383

tests/FlysystemTestSuite.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,63 @@ public function test_regression_issue_21()
279279
$this->assertIsArray($response);
280280
$this->assertCount(2, $response);
281281
}
282+
283+
/**
284+
* Github Issue - 28
285+
* https://github.com/PlatformCommunity/flysystem-bunnycdn/issues/28
286+
*
287+
* Issue present where a lot of TypeErrors will appear if you ask for lastModified on Directory (returns FileAttributes)
288+
*
289+
* @throws FilesystemException
290+
*/
291+
public function test_regression_issue_29()
292+
{
293+
$client = new MockClient(self::STORAGE_ZONE, 'api-key');
294+
295+
for($i = 0; $i < 10; $i++) {
296+
$client->add_response(
297+
new Response(200, [], json_encode(
298+
[
299+
/**
300+
* First with the milliseconds
301+
*/
302+
array_merge(
303+
$client::example_folder('/example_folder', self::STORAGE_ZONE),
304+
[
305+
'LastChanged' => date('Y-m-d\TH:i:s.v'),
306+
'DateCreated' => date('Y-m-d\TH:i:s.v'),
307+
]
308+
),
309+
]
310+
))
311+
);
312+
}
313+
314+
$adapter = new Filesystem(new BunnyCDNAdapter($client));
315+
$exception_count = 0;
316+
317+
try {
318+
$adapter->fileSize('/example_folder');
319+
} catch (\Exception $e) {
320+
$this->assertInstanceOf(UnableToRetrieveMetadata::class, $e);
321+
$exception_count++;
322+
}
323+
324+
try {
325+
$adapter->mimeType('/example_folder');
326+
} catch (\Exception $e) {
327+
$this->assertInstanceOf(UnableToRetrieveMetadata::class, $e);
328+
$exception_count++;
329+
}
330+
331+
try {
332+
$adapter->lastModified('/example_folder');
333+
} catch (\Exception $e) {
334+
$this->assertInstanceOf(UnableToRetrieveMetadata::class, $e);
335+
$exception_count++;
336+
}
337+
338+
// The fact that PHPUnit makes me do this is 🤬
339+
$this->assertEquals(3, $exception_count);
340+
}
282341
}

0 commit comments

Comments
 (0)