Skip to content

Commit

Permalink
Remove throw config
Browse files Browse the repository at this point in the history
  • Loading branch information
Casmo committed Mar 27, 2023
1 parent 866dbbc commit fcd5bd8
Showing 1 changed file with 24 additions and 41 deletions.
65 changes: 24 additions & 41 deletions src/UploadcareAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use League\Flysystem\UnableToMoveFile;
use League\Flysystem\UnableToReadFile;
use League\Flysystem\UnableToRetrieveMetadata;
use League\Flysystem\UnableToSetVisibility;
use League\Flysystem\UnableToWriteFile;
use Psr\Http\Message\StreamInterface;
use Uploadcare\Exception\HttpException;
Expand All @@ -44,14 +43,6 @@ public function __construct(\Uploadcare\Api $api, ?array $config = [])
$this->config = $config;
}

/**
* Determine if Flysystem exceptions should be thrown.
*/
protected function throwsExceptions(): bool
{
return (bool) ($this->config['throw'] ?? true);
}

/**
* Get the cdn
*/
Expand All @@ -71,8 +62,7 @@ public function fileExists(string $path): bool
if ($e->getCode() == 404) {
return false;
}
throw_if($this->throwsExceptions(), (new UnableToCheckExistence($e->getMessage())));
return false;
throw new UnableToCheckExistence($e->getMessage());
}

return true;
Expand All @@ -89,8 +79,7 @@ public function directoryExists(string $path): bool
if ($e->getCode() == 404) {
return false;
}
throw_if($this->throwsExceptions(), (new UnableToCheckExistence($e->getMessage())));
return false;
throw new UnableToCheckExistence($e->getMessage());
}

return true;
Expand All @@ -108,14 +97,14 @@ public function write(string $path, string $contents, Config $config): void
);
}
catch (InvalidArgumentException $e) {
throw_if($this->throwsExceptions(), (new UnableToWriteFile($e->getMessage())));
throw new UnableToWriteFile($e->getMessage());
}
}

/**
* @throws UnableToWriteFile
*/
public function writeGetUuid(string $path, string $contents, $config): string
public function writeGetUuid(string $path, string $contents, $config): string|bool
{
try {
$result = $this->api->uploader()->fromContent(
Expand All @@ -124,7 +113,7 @@ public function writeGetUuid(string $path, string $contents, $config): string
);
}
catch (InvalidArgumentException $e) {
throw_if($this->throwsExceptions(), (new UnableToWriteFile($e->getMessage())));
throw new UnableToWriteFile($e->getMessage());
}

return $result->getUuid();
Expand All @@ -144,7 +133,7 @@ public function writeStream(string $path, $contents, Config $config): void
);
}
catch (InvalidArgumentException $e) {
throw_if($this->throwsExceptions(), (new UnableToWriteFile($e->getMessage())));
throw new UnableToWriteFile($e->getMessage());
}
}

Expand All @@ -153,7 +142,7 @@ public function writeStream(string $path, $contents, Config $config): void
*
* @throws UnableToWriteFile
*/
public function writeStreamGetUuid(string $path, $contents, $config): string
public function writeStreamGetUuid(string $path, $contents, $config): string|bool
{
try {
$result = $this->api->uploader()->fromResource(
Expand All @@ -162,7 +151,7 @@ public function writeStreamGetUuid(string $path, $contents, $config): string
);
}
catch (InvalidArgumentException $e) {
throw_if($this->throwsExceptions(), (new UnableToWriteFile($e->getMessage())));
throw new UnableToWriteFile($e->getMessage());
}

return $result->getUuid();
Expand All @@ -187,21 +176,15 @@ public function putGetUuid($path, $contents, $options = [])
return $this->putFileGetUuid($path, $contents, $options);
}

try {
if ($contents instanceof StreamInterface) {
return $this->writeStreamGetUuid($path, $contents->detach(), $options);
}

$uuid = is_resource($contents)
? $this->writeStreamGetUuid($path, $contents, $options)
: $this->writeGetUuid($path, $contents, $options);
if ($contents instanceof StreamInterface) {
return $this->writeStreamGetUuid($path, $contents->detach(), $options);
}

return $uuid;
} catch (UnableToWriteFile|UnableToSetVisibility $e) {
throw_if($this->throwsExceptions(), $e);
$uuid = is_resource($contents)
? $this->writeStreamGetUuid($path, $contents, $options)
: $this->writeGetUuid($path, $contents, $options);

return false;
}
return $uuid;
}

public function putFileGetUuid($path, $file = null, $options = [])
Expand Down Expand Up @@ -265,7 +248,7 @@ public function read(string $path): string
$content = file_get_contents($url);
}
catch (ErrorException $e) {
throw_if($this->throwsExceptions(), (new UnableToReadFile($e->getMessage())));
throw new UnableToReadFile($e->getMessage());
}

return $content;
Expand All @@ -284,7 +267,7 @@ public function readStream(string $path)
$stream = fopen($url, 'rb');
}
catch (ErrorException $e) {
throw_if($this->throwsExceptions(), (new UnableToReadFile($e->getMessage())));
throw new UnableToReadFile($e->getMessage());
}

return $stream;
Expand All @@ -299,7 +282,7 @@ public function delete(string $path): void
$this->api->file()->deleteFile($path);
}
catch (HttpException $e) {
throw_if($this->throwsExceptions(), (new UnableToDeleteFile($e->getMessage())));
throw new UnableToDeleteFile($e->getMessage());
}
}

Expand All @@ -311,7 +294,7 @@ public function deleteDirectory(string $path): void
try {
$this->api->group()->removeGroup($path);
} catch (\Uploadcare\Exception\HttpException $e) {
throw_if($this->throwsExceptions(), (new UnableToDeleteDirectory($e->getMessage())));
throw new UnableToDeleteDirectory($e->getMessage());
}
}

Expand All @@ -320,15 +303,15 @@ public function deleteDirectory(string $path): void
*/
public function createDirectory(string $path, Config $config): void
{
throw_if($this->throwsExceptions(), (new UnableToCreateDirectory('Unable to create group')));
throw new UnableToCreateDirectory('Unable to create group');
}

/**
* @throws InvalidVisibilityProvided
*/
public function setVisibility(string $path, string $visibility): void
{
throw_if($this->throwsExceptions(), (new InvalidVisibilityProvided()));
throw new InvalidVisibilityProvided();
}

/**
Expand All @@ -345,7 +328,7 @@ public function getFileinfo(string $path): FileAttributes
$info = $this->api->file()->fileInfo($path);
}
catch (\Exception $e) {
throw_if($this->throwsExceptions(), (new UnableToRetrieveMetadata($e->getMessage())));
throw new UnableToRetrieveMetadata($e->getMessage());
}

return new FileAttributes(
Expand Down Expand Up @@ -393,14 +376,14 @@ public function listContents(string $path, bool $deep): iterable
*/
public function move(string $source, string $destination, Config $config): void
{
throw_if($this->throwsExceptions(), (new UnableToMoveFile()));
throw new UnableToMoveFile();
}

/**
* @throws UnableToCopyFile
*/
public function copy(string $source, string $destination, Config $config): void
{
throw_if($this->throwsExceptions(), (new UnableToCopyFile()));
throw new UnableToCopyFile();
}
}

0 comments on commit fcd5bd8

Please sign in to comment.