Skip to content

Commit

Permalink
Merge pull request #26 from PlatformCommunity/feature/laravel-pint
Browse files Browse the repository at this point in the history
Added Laravel Pint
  • Loading branch information
sifex authored Jul 13, 2022
2 parents d2082e0 + 410f237 commit 2294fd4
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 129 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ jobs:
- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-interaction

- name: Run linter
run: ./vendor/bin/pint -v --test

- name: Run tests
run: vendor/bin/phpunit -c ./phpunit.xml

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"mockery/mockery": "^1.3",
"league/flysystem-adapter-test-utilities": "^3",
"league/flysystem-memory": "^3.0",
"fzaninotto/faker": "^1.5"
"fzaninotto/faker": "^1.5",
"laravel/pint": "^0.2.3"
},
"autoload-dev": {
"psr-4": {
Expand Down
83 changes: 45 additions & 38 deletions src/BunnyCDNAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use League\Flysystem\FilesystemAdapter;
use League\Flysystem\FilesystemException;
use League\Flysystem\InvalidVisibilityProvided;
use League\Flysystem\PathPrefixer;
use League\Flysystem\StorageAttributes;
use League\Flysystem\UnableToCheckExistence;
use League\Flysystem\UnableToCopyFile;
Expand All @@ -24,14 +23,12 @@
use League\Flysystem\Visibility;
use League\MimeTypeDetection\FinfoMimeTypeDetector;
use RuntimeException;
use stdClass;
use function PHPUnit\Framework\stringContains;

class BunnyCDNAdapter implements FilesystemAdapter
{

/**
* Pull Zone URL
*
* @var string
*/
private string $pullzone_url;
Expand All @@ -42,8 +39,8 @@ class BunnyCDNAdapter implements FilesystemAdapter
private BunnyCDNClient $client;

/**
* @param BunnyCDNClient $client
* @param string $pullzone_url
* @param BunnyCDNClient $client
* @param string $pullzone_url
*/
public function __construct(BunnyCDNClient $client, string $pullzone_url = '')
{
Expand All @@ -54,7 +51,7 @@ public function __construct(BunnyCDNClient $client, string $pullzone_url = '')
/**
* @param $source
* @param $destination
* @param Config $config
* @param Config $config
* @return void
*/
public function copy($source, $destination, Config $config): void
Expand All @@ -71,7 +68,7 @@ public function copy($source, $destination, Config $config): void
/**
* @param $path
* @param $contents
* @param Config $config
* @param Config $config
*/
public function write($path, $contents, Config $config): void
{
Expand Down Expand Up @@ -100,8 +97,8 @@ public function read($path): string
}

/**
* @param string $path
* @param bool $deep
* @param string $path
* @param bool $deep
* @return iterable
*/
public function listContents(string $path = '', bool $deep = false): iterable
Expand All @@ -127,7 +124,7 @@ public function listContents(string $path = '', bool $deep = false): iterable
}

/**
* @param array $bunny_file_array
* @param array $bunny_file_array
* @return StorageAttributes
*/
protected function normalizeObject(array $bunny_file_array): StorageAttributes
Expand All @@ -136,18 +133,18 @@ protected function normalizeObject(array $bunny_file_array): StorageAttributes
true => new DirectoryAttributes(
Util::normalizePath(
str_replace(
$bunny_file_array['StorageZoneName'] . '/',
$bunny_file_array['StorageZoneName'].'/',
'/',
$bunny_file_array['Path'] . $bunny_file_array['ObjectName']
$bunny_file_array['Path'].$bunny_file_array['ObjectName']
)
)
),
false => new FileAttributes(
Util::normalizePath(
str_replace(
$bunny_file_array['StorageZoneName'] . '/',
$bunny_file_array['StorageZoneName'].'/',
'/',
$bunny_file_array['Path'] . $bunny_file_array['ObjectName']
$bunny_file_array['Path'].$bunny_file_array['ObjectName']
)
),
$bunny_file_array['Length'],
Expand All @@ -160,14 +157,14 @@ protected function normalizeObject(array $bunny_file_array): StorageAttributes
}

/**
* @param array $bunny_file_array
* @param array $bunny_file_array
* @return array
*/
private function extractExtraMetadata(array $bunny_file_array): array
{
return [
'type' => $bunny_file_array['IsDirectory'] ? 'dir' : 'file',
'dirname' => Util::splitPathIntoDirectoryAndFile($bunny_file_array['Path'])['dir'],
'type' => $bunny_file_array['IsDirectory'] ? 'dir' : 'file',
'dirname' => Util::splitPathIntoDirectoryAndFile($bunny_file_array['Path'])['dir'],
'guid' => $bunny_file_array['Guid'],
'object_name' => $bunny_file_array['ObjectName'],
'timestamp' => self::parse_bunny_timestamp($bunny_file_array['LastChanged']),
Expand All @@ -184,8 +181,9 @@ private function extractExtraMetadata(array $bunny_file_array): array
/**
* Detects the mime type from the provided file path
*
* @param string $path
* @param string $path
* @return string
*
* @throws Exceptions\BunnyCDNException
* @throws Exceptions\NotFoundException
*/
Expand All @@ -194,7 +192,7 @@ public function detectMimeType(string $path): string
$detector = new FinfoMimeTypeDetector();
$mimeType = $detector->detectMimeTypeFromPath($path);

if (!$mimeType) {
if (! $mimeType) {
return $detector->detectMimeTypeFromBuffer(stream_get_contents($this->readStream($path), 80));
}

Expand All @@ -204,7 +202,7 @@ public function detectMimeType(string $path): string
/**
* @param $path
* @param $contents
* @param Config $config
* @param Config $config
* @return void
*/
public function writeStream($path, $contents, Config $config): void
Expand All @@ -215,6 +213,7 @@ public function writeStream($path, $contents, Config $config): void
/**
* @param $path
* @return resource
*
* @throws Exceptions\BunnyCDNException
* @throws Exceptions\NotFoundException
*/
Expand All @@ -231,7 +230,7 @@ public function deleteDirectory(string $path): void
{
try {
$this->client->delete(
rtrim($path, '/') . '/'
rtrim($path, '/').'/'
);
// @codeCoverageIgnoreStart
} catch (Exceptions\BunnyCDNException $e) {
Expand All @@ -250,9 +249,9 @@ public function createDirectory(string $path, Config $config): void
$this->client->make_directory($path);
// @codeCoverageIgnoreStart
} catch (Exceptions\BunnyCDNException $e) {
# Lol apparently this is "idempotent" but there's an exception... Sure whatever..
// Lol apparently this is "idempotent" but there's an exception... Sure whatever..
match ($e->getMessage()) {
'Directory already exists' => "",
'Directory already exists' => '',
default => throw UnableToCreateDirectory::atLocation($path, $e->getMessage())
};
}
Expand All @@ -265,7 +264,7 @@ public function createDirectory(string $path, Config $config): void
*/
public function setVisibility(string $path, string $visibility): void
{
throw UnableToSetVisibility::atLocation($path, "BunnyCDN does not support visibility");
throw UnableToSetVisibility::atLocation($path, 'BunnyCDN does not support visibility');
}

/**
Expand All @@ -281,8 +280,11 @@ public function visibility(string $path): FileAttributes
}

/**
* @throws UnableToRetrieveMetadata
* @throws FilesystemException
* @param string $path
* @return FileAttributes
*
* @throws Exceptions\BunnyCDNException
* @throws Exceptions\NotFoundException
* @codeCoverageIgnore
*/
public function mimeType(string $path): FileAttributes
Expand All @@ -295,10 +297,10 @@ public function mimeType(string $path): FileAttributes
}

/** @var FileAttributes $object */
if (!$object->mimeType()) {
if (! $object->mimeType()) {
$mimeType = $this->detectMimeType($path);

if(!$mimeType || $mimeType === 'text/plain') {
if (! $mimeType || $mimeType === 'text/plain') { // Really not happy about this being required by Fly's Test case
throw new UnableToRetrieveMetadata('Unknown Mimetype');
}

Expand Down Expand Up @@ -332,14 +334,16 @@ protected function getObject(string $path = ''): StorageAttributes
if (count($list) === 1) {
return $list[0];
} elseif (count($list) > 1) {
throw UnableToReadFile::fromLocation($path, 'More than one file was returned for path:"' . $path . '", contact package author.');
// @codeCoverageIgnoreStart
throw UnableToReadFile::fromLocation($path, 'More than one file was returned for path:"'.$path.'", contact package author.');
// @codeCoverageIgnoreEnd
} else {
throw UnableToReadFile::fromLocation($path, 'Error 404:"' . $path . '"');
throw UnableToReadFile::fromLocation($path, 'Error 404:"'.$path.'"');
}
}

/**
* @param string $path
* @param string $path
* @return FileAttributes
*/
public function lastModified(string $path): FileAttributes
Expand All @@ -352,7 +356,7 @@ public function lastModified(string $path): FileAttributes
}

/**
* @param string $path
* @param string $path
* @return FileAttributes
*/
public function fileSize(string $path): FileAttributes
Expand All @@ -361,7 +365,9 @@ public function fileSize(string $path): FileAttributes
$object = $this->getObject($path);

if ($object instanceof DirectoryAttributes) {
// @codeCoverageIgnoreStart
throw new UnableToRetrieveMetadata('Cannot retrieve size of folder');
// @codeCoverageIgnoreEnd
}

return $object;
Expand Down Expand Up @@ -394,7 +400,7 @@ public function delete($path): void
$this->client->delete($path);
// @codeCoverageIgnoreStart
} catch (Exceptions\BunnyCDNException $e) {
if (!str_contains($e->getMessage(), '404')) { # Urgh
if (! str_contains($e->getMessage(), '404')) { // Urgh
throw UnableToDeleteFile::atLocation($path, $e->getMessage());
}
}
Expand All @@ -410,7 +416,7 @@ public function directoryExists(string $path): bool
}

/**
* @param string $path
* @param string $path
* @return bool
*/
public function fileExists(string $path): bool
Expand All @@ -423,12 +429,13 @@ public function fileExists(string $path): bool
return Util::normalizePath($item->path()) === Util::normalizePath($path);
})->toArray();

return (bool)count($count);
return (bool) count($count);
}

/**
* getURL method for Laravel users who want to use BunnyCDN's PullZone to retrieve a public URL
* @param string $path
*
* @param string $path
* @return string
* @codeCoverageIgnore
*/
Expand All @@ -438,7 +445,7 @@ public function getUrl(string $path): string
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 rtrim($this->pullzone_url, '/') . '/' . ltrim($path, '/');
return rtrim($this->pullzone_url, '/').'/'.ltrim($path, '/');
}

private static function parse_bunny_timestamp(string $timestamp): int
Expand Down
Loading

0 comments on commit 2294fd4

Please sign in to comment.