diff --git a/readme.md b/readme.md index 7446d34..6e8d327 100644 --- a/readme.md +++ b/readme.md @@ -2,7 +2,7 @@ # Flysystem Adapter for BunnyCDN Storage -[![Build Status](https://github.com/PlatformCommunity/flysystem-bunnycdn/actions/workflows/php.yml/badge.svg)](https://github.com/PlatformCommunity/flysystem-bunnycdn/actions) [![Codecov](https://img.shields.io/codecov/c/github/PlatformCommunity/flysystem-bunnycdn)](https://codecov.io/gh/PlatformCommunity/flysystem-bunnycdn) [![Packagist Version](https://img.shields.io/packagist/v/platformcommunity/flysystem-bunnycdn)](https://packagist.org/packages/platformcommunity/flysystem-bunnycdn) [![Packagist](https://img.shields.io/packagist/l/platformcommunity/flysystem-bunnycdn)](https://github.com/PlatformCommunity/flysystem-bunnycdn/blob/master/LICENSE) [![Packagist](https://img.shields.io/packagist/dm/platformcommunity/flysystem-bunnycdn)](https://packagist.org/packages/platformcommunity/flysystem-bunnycdn) +[![Build Status](https://github.com/PlatformCommunity/flysystem-bunnycdn/actions/workflows/php.yml/badge.svg)](https://github.com/PlatformCommunity/flysystem-bunnycdn/actions) [![Codecov](https://img.shields.io/codecov/c/github/PlatformCommunity/flysystem-bunnycdn)](https://codecov.io/gh/PlatformCommunity/flysystem-bunnycdn) [![Packagist Version](https://img.shields.io/packagist/v/platformcommunity/flysystem-bunnycdn)](https://packagist.org/packages/platformcommunity/flysystem-bunnycdn) ![Minimum PHP Version: 7.2](https://img.shields.io/badge/php-min%207.2-important) [![Licence: MIT](https://img.shields.io/packagist/l/platformcommunity/flysystem-bunnycdn)](https://github.com/PlatformCommunity/flysystem-bunnycdn/blob/master/LICENSE) [![Downloads](https://img.shields.io/packagist/dm/platformcommunity/flysystem-bunnycdn)](https://packagist.org/packages/platformcommunity/flysystem-bunnycdn) ## Installation diff --git a/src/BunnyCDNAdapter.php b/src/BunnyCDNAdapter.php index da53df0..ba68609 100644 --- a/src/BunnyCDNAdapter.php +++ b/src/BunnyCDNAdapter.php @@ -20,6 +20,7 @@ use League\Flysystem\UnableToWriteFile; use League\Flysystem\UnixVisibility\PortableVisibilityConverter; use League\Flysystem\Visibility; +use RuntimeException; use stdClass; use League\Flysystem\PathPrefixer; @@ -41,13 +42,20 @@ class BunnyCDNAdapter implements FilesystemAdapter private $prefixer; /** - * BunnyCDNAdapter constructor. + * Pull Zone URL + * @var string + */ + private $pullzone_url; + + /** * @param BunnyCDNStorage $bunnyCDNStorage + * @param string $pullzone_url */ - public function __construct(BunnyCDNStorage $bunnyCDNStorage) + public function __construct(BunnyCDNStorage $bunnyCDNStorage, string $pullzone_url = '') { $this->bunnyCDNStorage = $bunnyCDNStorage; $this->prefixer = new PathPrefixer($this->bunnyCDNStorage->storageZoneName); + $this->pullzone_url = $pullzone_url; } /** @@ -208,15 +216,6 @@ protected function normalizeObject(stdClass $fileObject): array ]; } - /** - * @param $path - * @return array - */ - public function getMetadata($path): array - { - return $this->normalizeObject($this->getObject($path)); - } - /** * @param $path * @return string @@ -376,4 +375,18 @@ public function move(string $source, string $destination, Config $config): void $this->write($destination, $this->read($source), new Config()); $this->delete($source); } + + /** + * getURL method for Laravel users who want to use BunnyCDN's PullZone to retrieve a public URL + * @param string $path + * @return string + */ + public function getUrl(string $path): string + { + if($this->pullzone_url === '') { + throw new RuntimeException('In order to get a visible URL for a BunnyCDN object, you must pass the "pullzone_url" parameter to the BunnyCDNAdapter.'); + } + + return $this->pullzone_url.'/'.ltrim($path, '/'); + } } diff --git a/tests/BunnyCDNAdapterTest.php b/tests/BunnyCDNAdapterTest.php index 5f73c6e..da84903 100644 --- a/tests/BunnyCDNAdapterTest.php +++ b/tests/BunnyCDNAdapterTest.php @@ -86,7 +86,7 @@ private function getBunnyCDNMockObject($storageZoneName = self::STORAGE_ZONE_NAM $mock->apiAccessKey = $apiAccessKey; return $mock; -// return new BunnyCDNStorage('test12391248982', 'f053a6e4-5b85-46a3-922fc6664a1e-cb04-47e0', 'de'); + return new BunnyCDNStorage('', '', 'sg'); } /** @@ -134,7 +134,12 @@ protected static function getBunnyCDNAdapterMethod($name): \ReflectionMethod public function it_write() { $adapter = new BunnyCDNAdapter($this->getBunnyCDNMockObject()); - $this->assertNull($adapter->write('testing/test123.txt', self::TEST_FILE_CONTENTS, new Config())); + try { + self::assertNull($adapter->delete('testing/test.txt')); + } catch (Exception $exception) { + + } + $this->assertNull($adapter->write('testing/test.txt', self::TEST_FILE_CONTENTS, new Config())); } /** @@ -263,6 +268,8 @@ public function it_list_contents() /** * @test * @return void + * @throws Exception + * @throws Exception */ public function it_get_size() { @@ -294,6 +301,7 @@ public function it_rename() /** * @test * @throws Exception + * @throws \League\Flysystem\FilesystemException */ public function it_create_dir() { @@ -331,6 +339,8 @@ public function it_get_timestamp() /** * @test * @return void + * @throws \League\Flysystem\FilesystemException + * @throws \League\Flysystem\FilesystemException */ public function it_get_visibility() { @@ -344,6 +354,8 @@ public function it_get_visibility() /** * @test * @return void + * @throws \League\Flysystem\FilesystemException + * @throws \League\Flysystem\FilesystemException */ public function it_set_visibility() { @@ -352,7 +364,25 @@ public function it_set_visibility() self::expectException(\League\Flysystem\UnableToSetVisibility::class); $adapter->setVisibility('testing/test.txt', 878); + } + + /** + * @test + * @return void + * @throws \League\Flysystem\FilesystemException + * @throws \League\Flysystem\FilesystemException + */ + public function it_get_public_url() + { + $adapter = new BunnyCDNAdapter($this->getBunnyCDNMockObject(), 'https://testing1827129361.b-cdn.net'); + + $this->assertEquals('https://testing1827129361.b-cdn.net/testing/test.txt', $adapter->getUrl('/testing/test.txt')); + + $adapter = new BunnyCDNAdapter($this->getBunnyCDNMockObject()); + + $this->expectException(RuntimeException::class); + $adapter->getUrl('/testing/test.txt'); } }