diff --git a/config/cloudinary.php b/config/cloudinary.php index 7412d3f..a444b98 100644 --- a/config/cloudinary.php +++ b/config/cloudinary.php @@ -21,7 +21,6 @@ */ 'notification_url' => env('CLOUDINARY_NOTIFICATION_URL'), - /* |-------------------------------------------------------------------------- | Cloudinary Configuration @@ -36,7 +35,6 @@ /** * Upload Preset From Cloudinary Dashboard - * */ 'upload_preset' => env('CLOUDINARY_UPLOAD_PRESET'), diff --git a/database/migrations/2020_06_14_000001_create_media_table.php b/database/migrations/2020_06_14_000001_create_media_table.php index 2199fbf..fb79367 100644 --- a/database/migrations/2020_06_14_000001_create_media_table.php +++ b/database/migrations/2020_06_14_000001_create_media_table.php @@ -1,8 +1,8 @@ cloudinary = new Cloudinary($config); } @@ -50,11 +73,9 @@ public function __construct(string $config) * Create temporary stream with content. * Pass to writeStream. * - * @param string $path - * @param string $contents - * @param Config $config Config object - * + * @param Config $config Config object * @return void false on failure file meta data on success + * * @throws ApiError */ public function write(string $path, string $contents, Config $config): void @@ -69,11 +90,10 @@ public function write(string $path, string $contents, Config $config): void /** * Write a new file using a stream * - * @param string $path - * @param resource $contents - * @param Config $config Config object - * + * @param resource $contents + * @param Config $config Config object * @return void false on failure file meta data on success + * * @throws ApiError */ public function writeStream(string $path, $contents, Config $config): void @@ -83,8 +103,8 @@ public function writeStream(string $path, $contents, Config $config): void $resourceType = $config->get('resource_type', 'auto'); $uploadOptions = [ - 'public_id' => $publicId, - 'resource_type' => $resourceType + 'public_id' => $publicId, + 'resource_type' => $resourceType, ]; $resourceMetadata = stream_get_meta_data($contents); @@ -101,7 +121,7 @@ public function preparePublicId($path): string return str($path) ->when($this->isMedia($extension)) - ->beforeLast('.'.$extension); + ->beforeLast('.' . $extension)->toString(); } /** @@ -114,15 +134,10 @@ public function isMedia($extension): bool /** * Rename a file. - * - * @param string $path - * @param string $newpath - * - * @return bool */ public function rename(string $path, string $newpath): bool { - $pathInfo = pathinfo($path); + $pathInfo = pathinfo($path); $newPathInfo = pathinfo($newpath); $remotePath = ($pathInfo['dirname'] != '.') ? $pathInfo['dirname'] . '/' . $pathInfo['filename'] : $pathInfo['filename']; @@ -148,10 +163,7 @@ protected function uploadApi(): UploadApi /** * Upload a file * - * @param string $file - * @param array $options * - * @return void * @throws ApiError */ protected function upload(string $file, array $options = []): void @@ -163,10 +175,6 @@ protected function upload(string $file, array $options = []): void * Copy a file. * Copy content from existing url. * - * @param string $source - * @param string $destination - * @param Config $config - * @return void * @throws ApiError */ public function copy(string $source, string $destination, Config $config): void @@ -179,15 +187,11 @@ public function copy(string $source, string $destination, Config $config): void /** * Delete a file. - * - * @param string $path - * - * @return void */ public function delete(string $path): void { try { - $result = $this->uploadApi()->destroy($this->preparePublicId($path)); + $result = $this->uploadApi()->destroy($this->preparePublicId($path)); $finalResult = is_array($result) && $result['result'] == 'ok'; if ($finalResult != 'ok') { @@ -202,9 +206,7 @@ public function delete(string $path): void * Delete a directory. * Delete Files using directory as a prefix. * - * @param string $path * - * @return void * * @throws ApiError */ @@ -215,7 +217,6 @@ public function deleteDirectory(string $path): void /** * Expose the Cloudinary v2 Upload Functionality - * */ protected function adminApi(): AdminApi { @@ -225,10 +226,7 @@ protected function adminApi(): AdminApi /** * Create a directory. * - * @param string $path directory name - * @param Config $config - * - * @return void + * @param string $path directory name * * @throws ApiError */ @@ -239,10 +237,6 @@ public function createDirectory(string $path, Config $config): void /** * Check whether a file exists. - * - * @param string $path - * - * @return bool */ public function fileExists(string $path): bool { @@ -257,10 +251,6 @@ public function fileExists(string $path): bool /** * Check whether a directory exists. - * - * @param string $path - * - * @return bool */ public function directoryExists(string $path): bool { @@ -269,14 +259,10 @@ public function directoryExists(string $path): bool /** * Read a file. - * - * @param string $path - * - * @return string */ public function read(string $path): string { - $resource = (array)$this->adminApi()->asset($this->preparePublicId($path)); + $resource = (array) $this->adminApi()->asset($this->preparePublicId($path)); return file_get_contents($resource['secure_url']); } @@ -284,13 +270,12 @@ public function read(string $path): string /** * Read a file as a stream. * - * @param string $path * - * @return false + * @return resource */ - public function readStream(string $path): bool + public function readStream(string $path) { - $resource = (array)$this->adminApi()->asset($this->preparePublicId($path)); + $resource = (array) $this->adminApi()->asset($this->preparePublicId($path)); return fopen($resource['secure_url'], 'rb'); } @@ -298,8 +283,7 @@ public function readStream(string $path): bool /** * Set visibility for the file * - * @param string $path - * @param mixed $visibility + * @param mixed $visibility * * @throws UnableToSetVisibility */ @@ -311,7 +295,6 @@ public function setVisibility(string $path, $visibility): void /** * Check visibility of the file * - * @param string $path * @throws UnableToSetVisibility */ public function visibility(string $path): FileAttributes @@ -322,8 +305,6 @@ public function visibility(string $path): FileAttributes /** * List contents of a directory. * - * @param string $path - * @param bool $deep * @return iterable */ public function listContents(string $path = '', bool $deep = false): iterable @@ -333,7 +314,7 @@ public function listContents(string $path = '', bool $deep = false): iterable // get resources array $response = null; do { - $response = (array)$this->adminApi()->assets( + $response = (array) $this->adminApi()->assets( [ 'type' => 'upload', 'prefix' => $path, @@ -348,15 +329,12 @@ public function listContents(string $path = '', bool $deep = false): iterable foreach ($resources as $i => $resource) { $resources[$i] = $this->prepareFileAttributes($this->prepareResourceMetadata($resource)); } + return $resources; } /** * Transform array of resource metadata into a {@link FileAttributes} instance. - * - * @param array $metadata - * - * @return FileAttributes */ protected function prepareFileAttributes(array $metadata): FileAttributes { @@ -372,64 +350,49 @@ protected function prepareFileAttributes(array $metadata): FileAttributes /** * Prepare apropriate metadata for resource metadata given from cloudinary. - * @param array $resource - * @return array */ protected function prepareResourceMetadata(array $resource): array { $resource['type'] = 'file'; $resource['path'] = $resource['public_id']; - $resource = array_merge($resource, $this->prepareSize($resource)); - $resource = array_merge($resource, $this->prepareTimestamp($resource)); + $resource = array_merge($resource, $this->prepareSize($resource)); + $resource = array_merge($resource, $this->prepareTimestamp($resource)); return array_merge($resource, $this->prepareMimetype($resource)); } /** * prepare size response - * - * @param array $resource - * - * @return array */ protected function prepareSize(array $resource): array { $size = $resource['bytes']; + return compact('size'); } /** * prepare timestamp response - * - * @param array $resource - * - * @return array */ protected function prepareTimestamp(array $resource): array { $timestamp = strtotime($resource['created_at']); + return compact('timestamp'); } /** * prepare mimetype response - * - * @param array $resource - * - * @return array */ protected function prepareMimetype(array $resource): array { $mimetype = $resource['resource_type']; + return compact('mimetype'); } /** * Get all the meta data of a file or directory. - * - * @param string $path - * - * @return array */ public function getMetadata(string $path): array { @@ -438,20 +401,14 @@ public function getMetadata(string $path): array /** * Get Resource data - * @param string $path - * @return array */ public function getResource(string $path): array { - return (array)$this->adminApi()->asset($this->preparePublicId($path)); + return (array) $this->adminApi()->asset($this->preparePublicId($path)); } /** * Get all the meta data of a file or directory. - * - * @param string $path - * - * @return array */ public function getSize(string $path): array { @@ -460,10 +417,6 @@ public function getSize(string $path): array /** * Get the mimetype of a file. - * - * @param string $path - * - * @return array */ public function getMimetype(string $path): array { @@ -472,10 +425,6 @@ public function getMimetype(string $path): array /** * Get the mimetype of a file. - * - * @param string $path - * - * @return FileAttributes */ public function mimeType(string $path): FileAttributes { @@ -486,10 +435,6 @@ public function mimeType(string $path): FileAttributes /** * Get the timestamp of a file. - * - * @param string $path - * - * @return FileAttributes */ public function lastModified(string $path): FileAttributes { @@ -500,10 +445,6 @@ public function lastModified(string $path): FileAttributes /** * Get the filesize of a file - * - * @param string $path - * - * @return FileAttributes */ public function fileSize(string $path): FileAttributes { @@ -515,11 +456,7 @@ public function fileSize(string $path): FileAttributes /** * Move a file to another location * - * @param string $source - * @param string $destination - * @param Config $config * - * @return void * @throws ApiError */ public function move(string $source, string $destination, Config $config): void @@ -530,10 +467,6 @@ public function move(string $source, string $destination, Config $config): void /** * Get the timestamp of a file. - * - * @param string $path - * - * @return array */ public function getTimestamp(string $path): array { @@ -542,18 +475,15 @@ public function getTimestamp(string $path): array /** * Get the url of a file - * - * @param string $path - * - * @return string|false */ - public function getUrl(string $path): bool|string + public function getUrl(string $path): string { if ($path == '/') { return $path; } try { $resource = $this->getResource(Str::beforeLast($path, '.')); + return $resource['secure_url'] ?? ''; } catch (Exception) { return ''; diff --git a/src/CloudinaryEngine.php b/src/CloudinaryEngine.php index 255c562..259b4e2 100644 --- a/src/CloudinaryEngine.php +++ b/src/CloudinaryEngine.php @@ -10,25 +10,24 @@ namespace CloudinaryLabs\CloudinaryLaravel; use Cloudinary\Api\Admin\AdminApi; +use Cloudinary\Api\ApiResponse; +use Cloudinary\Api\BaseApiClient; +use Cloudinary\Api\Exception\ApiError; use Cloudinary\Api\Search\SearchApi; use Cloudinary\Api\Upload\UploadApi; +use Cloudinary\Asset\Analytics; use Cloudinary\Asset\File; use Cloudinary\Asset\Image; use Cloudinary\Asset\Video; use Cloudinary\Cloudinary; -use Cloudinary\Asset\Analytics; use Cloudinary\Configuration\Configuration; use Cloudinary\Tag\ImageTag; use Cloudinary\Tag\VideoTag; -use Cloudinary\Api\ApiResponse; -use Cloudinary\Api\BaseApiClient; -use Cloudinary\Api\Exception\ApiError; use Exception; use GuzzleHttp\Promise\PromiseInterface; /** * Class CloudinaryEngine - * @package CloudinaryLabs\CloudinaryLaravel */ class CloudinaryEngine { @@ -40,41 +39,57 @@ class CloudinaryEngine const PACKAGE_VERSION = '2.2.2'; public const ASSET_ID = 'asset_id'; + public const PUBLIC_ID = 'public_id'; + public const VERSION = 'version'; + public const VERSION_ID = 'version_id'; + public const SIGNATURE = 'signature'; + public const WIDTH = 'width'; + public const HEIGHT = 'height'; + public const FORMAT = 'format'; + public const RESOURCE_TYPE = 'resource_type'; + public const CREATED_AT = 'created_at'; + public const TAGS = 'tags'; + public const PAGES = 'pages'; + public const BYTES = 'bytes'; + public const TYPE = 'type'; + public const ETAG = 'etag'; + public const PLACEHOLDER = 'placeholder'; + public const URL = 'url'; + public const SECURE_URL = 'secure_url'; + public const PHASH = 'phash'; + public const ORIGINAL_FILENAME = 'original_filename'; /** * Instance of Cloudinary - * @var Cloudinary */ protected Cloudinary $cloudinary; /** * Cloudinary url - * @var string */ protected string $url; /** * Response from Cloudinary - * @var array|ApiResponse */ protected array|ApiResponse $response; @@ -88,7 +103,6 @@ public function __construct() /** * Create a Cloudinary Config Instance - * */ public function setCloudinaryConfig(): void { @@ -97,11 +111,10 @@ public function setCloudinaryConfig(): void /** * Set User Agent and Platform - * */ public function setUserPlatform(): void { - BaseApiClient::$userPlatform = 'CloudinaryLaravel/' . self::PACKAGE_VERSION; + BaseApiClient::$userPlatform = 'CloudinaryLaravel/'.self::PACKAGE_VERSION; } /** @@ -116,7 +129,6 @@ public function setAnalytics(): void /** * Create a Cloudinary Instance - * */ public function bootCloudinary(): void { @@ -125,7 +137,6 @@ public function bootCloudinary(): void /** * Expose the Cloudinary Admin Functionality - * */ public function admin(): AdminApi { @@ -134,7 +145,6 @@ public function admin(): AdminApi /** * Expose the Cloudinary Search Functionality - * */ public function search(): SearchApi { @@ -151,9 +161,8 @@ public function search(): SearchApi * * the remote FTP, HTTP or HTTPS URL address of an existing file * * a private storage bucket (S3 or Google Storage) URL of a whitelisted bucket * - * @param string $file The asset to upload. - * @param array $options The optional parameters. See the upload API documentation. - * + * @param string $file The asset to upload. + * @param array $options The optional parameters. See the upload API documentation. * @return CloudinaryEngine * * @throws ApiError @@ -169,7 +178,6 @@ public function upload(string $file, array $options = []): static /** * Expose the Cloudinary Upload Functionality - * */ public function uploadApi(): UploadApi { @@ -181,10 +189,9 @@ public function uploadApi(): UploadApi * * The upload is not signed so an upload preset is required. * - * @param string $file The asset to upload. - * @param string $uploadPreset The name of an upload preset. - * @param array $options The optional parameters. See the upload API documentation. - * + * @param string $file The asset to upload. + * @param string $uploadPreset The name of an upload preset. + * @param array $options The optional parameters. See the upload API documentation. * @return CloudinaryEngine * * @throws ApiError @@ -204,6 +211,7 @@ public function unsignedUpload(string $file, string $uploadPreset, array $option * The upload is not signed so an upload preset is required. * * This is asynchronous + * * @throws ApiError */ public function unsignedUploadAsync($file, $uploadPreset, $options = []): PromiseInterface @@ -212,9 +220,8 @@ public function unsignedUploadAsync($file, $uploadPreset, $options = []): Promis } /** - * @param $file - * @param array $options * @return $this + * * @throws ApiError */ public function uploadFile($file, array $options = []): static @@ -227,9 +234,8 @@ public function uploadFile($file, array $options = []): static } /** - * @param $file - * @param array $options * @return $this + * * @throws ApiError */ public function uploadVideo($file, array $options = []): static @@ -241,17 +247,11 @@ public function uploadVideo($file, array $options = []): static return $this; } - /** - * @return array|ApiResponse - */ public function getResponse(): array|ApiResponse { return $this->response; } - /** - * @return mixed - */ public function getAssetId(): mixed { return $this->response[self::ASSET_ID]; @@ -259,7 +259,6 @@ public function getAssetId(): mixed /** * Get the name of the file after it has been uploaded to Cloudinary - * @return string */ public function getFileName(): string { @@ -268,7 +267,6 @@ public function getFileName(): string /** * Get the public id of the file (also known as the name of the file) after it has been uploaded to Cloudinary - * @return string */ public function getPublicId(): string { @@ -277,96 +275,62 @@ public function getPublicId(): string /** * Get the name of the file before it was uploaded to Cloudinary - * @return string */ public function getOriginalFileName(): string { return $this->response[self::ORIGINAL_FILENAME]; } - /** - * @return mixed - */ public function getVersion(): mixed { return $this->response[self::VERSION]; } - /** - * @return mixed - */ public function getVersionId(): mixed { return $this->response[self::VERSION_ID]; } - /** - * @return mixed - */ public function getSignature(): mixed { return $this->response[self::SIGNATURE]; } - /** - * @return mixed - */ public function getWidth(): mixed { return $this->response[self::WIDTH]; } - /** - * @return mixed - */ public function getHeight(): mixed { return $this->response[self::HEIGHT]; } - /** - * @return mixed - */ public function getExtension(): mixed { return $this->response[self::FORMAT]; } - /** - * @return mixed - */ public function getFileType(): mixed { return $this->response[self::RESOURCE_TYPE]; } - /** - * @return mixed - */ public function getTimeUploaded(): mixed { return $this->response[self::CREATED_AT]; } - /** - * @return mixed - */ public function getTags(): mixed { return $this->response[self::TAGS]; } - /** - * @return mixed - */ public function getPages(): mixed { return $this->response[self::PAGES]; } - /** - * @return string - */ public function getReadableSize(): string { return $this->getHumanReadableSize($this->getSize()); @@ -375,19 +339,18 @@ public function getReadableSize(): string /** * Formats filesize in the way every human understands * - * @param $sizeInBytes * @return string Formatted Filesize, e.g. "113.24 MB". */ public function getHumanReadableSize($sizeInBytes): string { if ($sizeInBytes >= 1073741824) { - return number_format($sizeInBytes / 1073741824, 2) . ' GB'; + return number_format($sizeInBytes / 1073741824, 2).' GB'; } elseif ($sizeInBytes >= 1048576) { - return number_format($sizeInBytes / 1048576, 2) . ' MB'; + return number_format($sizeInBytes / 1048576, 2).' MB'; } elseif ($sizeInBytes >= 1024) { - return number_format($sizeInBytes / 1024, 2) . ' KB'; + return number_format($sizeInBytes / 1024, 2).' KB'; } elseif ($sizeInBytes > 1) { - return $sizeInBytes . ' bytes'; + return $sizeInBytes.' bytes'; } elseif ($sizeInBytes == 1) { return '1 byte'; } else { @@ -395,49 +358,31 @@ public function getHumanReadableSize($sizeInBytes): string } } - /** - * @return mixed - */ public function getSize(): mixed { return $this->response[self::BYTES]; } - /** - * @return mixed - */ public function getPlaceHolder(): mixed { return $this->response[self::PLACEHOLDER]; } - /** - * @return mixed - */ public function getPath(): mixed { return $this->response[self::URL]; } - /** - * @return mixed - */ public function getSecurePath(): mixed { return $this->response[self::SECURE_URL]; } - /** - * @return mixed - */ public function getPhash(): mixed { return $this->response[self::PHASH]; } - /** - * @return mixed - */ public function getEtag(): mixed { return $this->response[self::ETAG]; @@ -446,9 +391,7 @@ public function getEtag(): mixed /** * Fetches a new Image with current instance configuration. * - * @param string $publicId The public ID of the image. - * - * @return Image + * @param string $publicId The public ID of the image. */ public function getImage(string $publicId): Image { @@ -458,9 +401,7 @@ public function getImage(string $publicId): Image /** * Fetches a new Video with current instance configuration. * - * @param string|mixed $publicId The public ID of the video. - * - * @return Video + * @param string|mixed $publicId The public ID of the video. */ public function getVideo(mixed $publicId): Video { @@ -470,28 +411,18 @@ public function getVideo(mixed $publicId): Video /** * Fetches a raw file with current instance configuration. * - * @param string|mixed $publicId The public ID of the file. - * - * @return File + * @param string|mixed $publicId The public ID of the file. */ public function getFile(mixed $publicId): File { return $this->cloudinary->raw($publicId); } - /** - * @param $publicId - * @return ImageTag - */ public function getImageTag($publicId): ImageTag { return $this->cloudinary->imageTag($publicId); } - /** - * @param $publicId - * @return VideoTag - */ public function getVideoTag($publicId): VideoTag { return $this->cloudinary->videoTag($publicId); @@ -506,11 +437,9 @@ public function getVideoTag($publicId): VideoTag /** * Adds a tag to the assets specified. * - * @param string $tag The name of the tag to add. - * @param array $publicIds The public IDs of the assets to add the tag to. - * @param array $options The optional parameters. See the upload API documentation. - * - * @return ApiResponse + * @param string $tag The name of the tag to add. + * @param array $publicIds The public IDs of the assets to add the tag to. + * @param array $options The optional parameters. See the upload API documentation. * * @see https://cloudinary.com/documentation/image_upload_api_reference#tags_method */ @@ -532,11 +461,9 @@ public function addTagAsync($tag, $publicIds = [], $options = []): PromiseInterf /** * Removes a tag from the assets specified. * - * @param string $tag The name of the tag to remove. - * @param array|string $publicIds The public IDs of the assets to remove the tags from. - * @param array $options The optional parameters. See the upload API documentation. - * - * @return ApiResponse + * @param string $tag The name of the tag to remove. + * @param array|string $publicIds The public IDs of the assets to remove the tags from. + * @param array $options The optional parameters. See the upload API documentation. * * @see https://cloudinary.com/documentation/image_upload_api_reference#tags_method */ @@ -549,7 +476,6 @@ public function removeTag(string $tag, array|string $publicIds = [], array $opti * Removes a tag from the assets specified. * * This is an asynchronous function. - * */ public function removeTagAsync($tag, $publicIds = [], $options = []): PromiseInterface { @@ -559,10 +485,8 @@ public function removeTagAsync($tag, $publicIds = [], $options = []): PromiseInt /** * Removes all tags from the assets specified. * - * @param array $publicIds The public IDs of the assets to remove all tags from. - * @param array $options The optional parameters. See the upload API documentation. - * - * @return ApiResponse + * @param array $publicIds The public IDs of the assets to remove all tags from. + * @param array $options The optional parameters. See the upload API documentation. * * @see https://cloudinary.com/documentation/image_upload_api_reference#tags_method */ @@ -575,7 +499,6 @@ public function removeAllTags(array $publicIds = [], array $options = []): ApiRe * Removes all tags from the assets specified. * * This is an asynchronous function. - * */ public function removeAllTagsAsync($publicIds = [], $options = []): PromiseInterface { @@ -585,11 +508,9 @@ public function removeAllTagsAsync($publicIds = [], $options = []): PromiseInter /** * Replaces all existing tags on the assets specified with the tag specified. * - * @param string $tag The new tag with which to replace the existing tags. - * @param array|string $publicIds The public IDs of the assets to replace the tags of. - * @param array $options The optional parameters. See the upload API documentation. - * - * @return ApiResponse + * @param string $tag The new tag with which to replace the existing tags. + * @param array|string $publicIds The public IDs of the assets to replace the tags of. + * @param array $options The optional parameters. See the upload API documentation. * * @see https://cloudinary.com/documentation/image_upload_api_reference#tags_method */ @@ -602,7 +523,6 @@ public function replaceTag(string $tag, array|string $publicIds = [], array $opt * Replaces all existing tags on the assets specified with the tag specified. * * This is an asynchronous function. - * */ public function replaceTagAsync($tag, $publicIds = [], $options = []): PromiseInterface { @@ -622,10 +542,8 @@ public function replaceTagAsync($tag, $publicIds = [], $options = []): PromiseIn * * A single image file containing all the images with the specified tag (PNG by default). * * A CSS file that includes the style class names and the location of the individual images in the sprite. * - * @param string $tag The tag that indicates which images to include in the sprite. - * @param array $options The optional parameters. See the upload API documentation. - * - * @return ApiResponse + * @param string $tag The tag that indicates which images to include in the sprite. + * @param array $options The optional parameters. See the upload API documentation. * * @see https://cloudinary.com/documentation/image_upload_api_reference#sprite_method */ @@ -661,11 +579,6 @@ public function generatePDF($tag, $options = []): ApiResponse return $this->uploadApi()->multi($tag, $pdfOptions); } - /** - * @param $tag - * @param array $options - * @return ApiResponse - */ public function generatePDFAsync($tag, array $options = []): ApiResponse { $pdfOptions = array_merge($options, ['async' => true, 'format' => 'pdf']); @@ -673,11 +586,6 @@ public function generatePDFAsync($tag, array $options = []): ApiResponse return $this->uploadApi()->multi($tag, $pdfOptions); } - /** - * @param $tag - * @param array $options - * @return ApiResponse - */ public function generateAnimatedGIF($tag, array $options = []): ApiResponse { $gifOptions = array_merge($options, ['async' => false, 'format' => 'gif']); @@ -685,11 +593,6 @@ public function generateAnimatedGIF($tag, array $options = []): ApiResponse return $this->uploadApi()->multi($tag, $gifOptions); } - /** - * @param $tag - * @param array $options - * @return ApiResponse - */ public function generateAnimatedPNG($tag, array $options = []): ApiResponse { $pngOptions = array_merge($options, ['async' => false, 'format' => 'png']); @@ -697,11 +600,6 @@ public function generateAnimatedPNG($tag, array $options = []): ApiResponse return $this->uploadApi()->multi($tag, $pngOptions); } - /** - * @param $tag - * @param array $options - * @return ApiResponse - */ public function generateAnimatedPNGAsync($tag, array $options = []): ApiResponse { $pngOptions = array_merge($options, ['async' => true, 'format' => 'png']); @@ -709,11 +607,6 @@ public function generateAnimatedPNGAsync($tag, array $options = []): ApiResponse return $this->uploadApi()->multi($tag, $pngOptions); } - /** - * @param $tag - * @param array $options - * @return ApiResponse - */ public function generateAnimatedWEBP($tag, array $options = []): ApiResponse { $webpOptions = array_merge($options, ['async' => false, 'format' => 'webp']); @@ -721,11 +614,6 @@ public function generateAnimatedWEBP($tag, array $options = []): ApiResponse return $this->uploadApi()->multi($tag, $webpOptions); } - /** - * @param $tag - * @param array $options - * @return ApiResponse - */ public function generateAnimatedWEBPAsync($tag, array $options = []): ApiResponse { $webpOptions = array_merge($options, ['async' => true, 'format' => 'webp']); @@ -733,11 +621,6 @@ public function generateAnimatedWEBPAsync($tag, array $options = []): ApiRespons return $this->uploadApi()->multi($tag, $webpOptions); } - /** - * @param $tag - * @param array $options - * @return ApiResponse - */ public function generateAnimatedMP4($tag, array $options = []): ApiResponse { $mp4Options = array_merge($options, ['async' => false, 'format' => 'mp4']); @@ -745,11 +628,6 @@ public function generateAnimatedMP4($tag, array $options = []): ApiResponse return $this->uploadApi()->multi($tag, $mp4Options); } - /** - * @param $tag - * @param array $options - * @return ApiResponse - */ public function generateAnimatedMP4Async($tag, array $options = []): ApiResponse { $mp4Options = array_merge($options, ['async' => true, 'format' => 'mp4']); @@ -757,11 +635,6 @@ public function generateAnimatedMP4Async($tag, array $options = []): ApiResponse return $this->uploadApi()->multi($tag, $mp4Options); } - /** - * @param $tag - * @param array $options - * @return ApiResponse - */ public function generateAnimatedWEBM($tag, array $options = []): ApiResponse { $webmOptions = array_merge($options, ['async' => false, 'format' => 'webm']); @@ -769,11 +642,6 @@ public function generateAnimatedWEBM($tag, array $options = []): ApiResponse return $this->uploadApi()->multi($tag, $webmOptions); } - /** - * @param $tag - * @param array $options - * @return ApiResponse - */ public function generateAnimatedWEBMAsync($tag, array $options = []): ApiResponse { $webmOptions = array_merge($options, ['async' => true, 'format' => 'webm']); @@ -781,41 +649,21 @@ public function generateAnimatedWEBMAsync($tag, array $options = []): ApiRespons return $this->uploadApi()->multi($tag, $webmOptions); } - /** - * @param $tag - * @param array $options - * @return ApiResponse - */ public function multi($tag, array $options = []): ApiResponse { return $this->uploadApi()->multi($tag, $options); } - /** - * @param $tag - * @param array $options - * @return PromiseInterface - */ public function multiAsync($tag, array $options = []): PromiseInterface { return $this->uploadApi()->multiAsync($tag, $options); } - /** - * @param $publicId - * @param array $options - * @return ApiResponse - */ public function explode($publicId, array $options = []): ApiResponse { return $this->uploadApi()->explode($publicId, $options); } - /** - * @param $publicId - * @param array $options - * @return PromiseInterface - */ public function explodeAsync($publicId, array $options = []): PromiseInterface { return $this->uploadApi()->explodeAsync($publicId, $options); @@ -824,9 +672,8 @@ public function explodeAsync($publicId, array $options = []): PromiseInterface /** * Dynamically generates an image from a given textual string. * - * @param string $text The text string to generate an image for. - * @param array $options The optional parameters. See the upload API documentation. - * + * @param string $text The text string to generate an image for. + * @param array $options The optional parameters. See the upload API documentation. * @return CloudinaryEngine * * @see https://cloudinary.com/documentation/image_upload_api_reference#text_method @@ -838,11 +685,6 @@ public function generateImageFromText(string $text, array $options = []): static return $this; } - /** - * @param $text - * @param array $options - * @return PromiseInterface - */ public function generateImageFromTextAsync($text, array $options = []): PromiseInterface { return $this->uploadApi()->textAsync($text, $options); @@ -855,9 +697,7 @@ public function generateImageFromTextAsync($text, array $options = []): PromiseI */ /** - * @param array $options - * @param null $targetFormat - * @return ApiResponse + * @param null $targetFormat */ public function createArchive(array $options = [], $targetFormat = null): ApiResponse { @@ -865,46 +705,28 @@ public function createArchive(array $options = [], $targetFormat = null): ApiRes } /** - * @param array $options - * @param null $targetFormat - * @return PromiseInterface + * @param null $targetFormat */ public function createArchiveAsync(array $options = [], $targetFormat = null): PromiseInterface { return $this->uploadApi()->createArchiveAsync($options, $targetFormat); } - /** - * @param array $options - * @return ApiResponse - */ public function createZip(array $options = []): ApiResponse { return $this->uploadApi()->createZip($options); } - /** - * @param array $options - * @return PromiseInterface - */ public function createZipAsync(array $options = []): PromiseInterface { return $this->uploadApi()->createZipAsync($options); } - /** - * @param array $options - * @return string - */ public function downloadZipUrl(array $options = []): string { return $this->uploadApi()->downloadZipUrl($options); } - /** - * @param array $options - * @return string - */ public function downloadArchiveUrl(array $options = []): string { return $this->uploadApi()->downloadArchiveUrl($options); @@ -916,43 +738,21 @@ public function downloadArchiveUrl(array $options = []): string |-------------------------------------------------------------------------- */ - /** - * @param $context - * @param array $publicIds - * @param array $options - * @return ApiResponse - */ public function addContext($context, array $publicIds = [], array $options = []): ApiResponse { return $this->uploadApi()->addContext($context, $publicIds, $options); } - /** - * @param $context - * @param array $publicIds - * @param array $options - * @return PromiseInterface - */ public function addContextAsync($context, array $publicIds = [], array $options = []): PromiseInterface { return $this->uploadApi()->addContextAsync($context, $publicIds, $options); } - /** - * @param array $publicIds - * @param array $options - * @return ApiResponse - */ public function removeAllContext(array $publicIds = [], array $options = []): ApiResponse { return $this->uploadApi()->removeAllContext($publicIds, $options); } - /** - * @param array $publicIds - * @param array $options - * @return PromiseInterface - */ public function removeAllContextAsync(array $publicIds = [], array $options = []): PromiseInterface { return $this->uploadApi()->removeAllContextAsync($publicIds, $options); @@ -964,63 +764,31 @@ public function removeAllContextAsync(array $publicIds = [], array $options = [] |-------------------------------------------------------------------------- */ - /** - * @param $publicId - * @param array $options - * @return array|ApiResponse - */ public function destroy($publicId, array $options = []): array|ApiResponse { return $this->uploadApi()->destroy($publicId, $options); } - /** - * @param $publicId - * @param array $options - * @return PromiseInterface - */ public function destroyAsync($publicId, array $options = []): PromiseInterface { return $this->uploadApi()->destroyAsync($publicId, $options); } - /** - * @param $from - * @param $to - * @param array $options - * @return mixed - */ public function rename($from, $to, array $options = []): mixed { return $this->uploadApi()->rename($from, $to, $options); } - /** - * @param $from - * @param $to - * @param array $options - * @return PromiseInterface - */ public function renameAsync($from, $to, array $options = []): PromiseInterface { return $this->uploadApi()->renameAsync($from, $to, $options); } - /** - * @param $publicId - * @param array $options - * @return mixed - */ public function explicit($publicId, array $options = []): mixed { return $this->uploadApi()->explicit($publicId, $options); } - /** - * @param $publicId - * @param array $options - * @return PromiseInterface - */ public function explicitAsync($publicId, array $options = []): PromiseInterface { return $this->uploadApi()->explicitAsync($publicId, $options); @@ -1028,7 +796,7 @@ public function explicitAsync($publicId, array $options = []): PromiseInterface /** * Get Resource data - * @param string $path + * * @return ApiResponse|string; */ public function getResource(string $path): string|ApiResponse @@ -1043,7 +811,6 @@ public function getResource(string $path): string|ApiResponse /** * Get the url of a file * - * @param string $publicId * * @return string|false */ @@ -1051,6 +818,7 @@ public function getUrl(string $publicId): bool|string { $resource = $this->getResource($publicId); + return $resource['secure_url'] ?? ''; } } diff --git a/src/CloudinaryServiceProvider.php b/src/CloudinaryServiceProvider.php index f9563ff..7f30921 100644 --- a/src/CloudinaryServiceProvider.php +++ b/src/CloudinaryServiceProvider.php @@ -2,31 +2,27 @@ namespace CloudinaryLabs\CloudinaryLaravel; -use Illuminate\Http\UploadedFile; -use Illuminate\Support\Facades\Blade; -use Illuminate\Support\Facades\Route; -use Illuminate\Support\Facades\Storage; -use Illuminate\Support\ServiceProvider; -use League\Flysystem\Filesystem; -use Illuminate\Filesystem\FilesystemAdapter; use CloudinaryLabs\CloudinaryLaravel\Commands\BackupFilesCommand; use CloudinaryLabs\CloudinaryLaravel\Commands\DeleteFilesCommand; use CloudinaryLabs\CloudinaryLaravel\Commands\FetchFilesCommand; use CloudinaryLabs\CloudinaryLaravel\Commands\GenerateArchiveCommand; use CloudinaryLabs\CloudinaryLaravel\Commands\RenameFilesCommand; use CloudinaryLabs\CloudinaryLaravel\Commands\UploadFileCommand; - +use Illuminate\Filesystem\FilesystemAdapter; +use Illuminate\Http\UploadedFile; +use Illuminate\Support\Facades\Blade; +use Illuminate\Support\Facades\Route; +use Illuminate\Support\Facades\Storage; +use Illuminate\Support\ServiceProvider; +use League\Flysystem\Filesystem; /** * Class CloudinaryServiceProvider - * @package CloudinaryLabs\CloudinaryLaravel */ class CloudinaryServiceProvider extends ServiceProvider { /** * Perform post-registration booting of services. - * - * @return void */ public function boot(): void { @@ -42,8 +38,6 @@ public function boot(): void /** * Boot the package macros that extends Laravel Uploaded File API. - * - * @return void */ protected function bootMacros(): void { @@ -67,18 +61,14 @@ function ($folder = null, $publicId = null) { /** * Boot the package resources. - * - * @return void */ protected function bootResources(): void { - $this->loadViewsFrom(__DIR__ . '/../resources/views', 'cloudinary'); + $this->loadViewsFrom(__DIR__.'/../resources/views', 'cloudinary'); } /** * Boot the package directives. - * - * @return void */ protected function bootDirectives(): void { @@ -92,8 +82,6 @@ function () { /** * Boot the package components. - * - * @return void */ protected function bootComponents(): void { @@ -101,15 +89,15 @@ protected function bootComponents(): void Blade::component('cloudinary::components.image', $this->getComponentName('cld-image')); Blade::component('cloudinary::components.video', $this->getComponentName('cld-video')); } - - protected function getComponentName($componentName) + + protected function getComponentName($componentName) { - $version = explode(".", $this->app->version()); - if( (int)$version[0] <= 6 ) { - $componentName = str_replace("-", "_", $componentName); - } + $version = explode('.', $this->app->version()); + if ((int) $version[0] <= 6) { + $componentName = str_replace('-', '_', $componentName); + } - return $componentName; + return $componentName; } protected function bootCommands(): void @@ -125,7 +113,7 @@ protected function bootCommands(): void FetchFilesCommand::class, RenameFilesCommand::class, GenerateArchiveCommand::class, - DeleteFilesCommand::class + DeleteFilesCommand::class, ] ); } @@ -133,13 +121,11 @@ protected function bootCommands(): void /** * Boot the package's publishable resources. - * - * @return void */ protected function bootPublishing(): void { if ($this->app->runningInConsole()) { - $config = dirname(__DIR__) . '/config/cloudinary.php'; + $config = dirname(__DIR__).'/config/cloudinary.php'; $this->publishes( [ @@ -178,8 +164,6 @@ function ($app, $config) { /** * Boot the package routes. - * - * @return void */ protected function bootRoutes(): void { @@ -190,8 +174,6 @@ protected function bootRoutes(): void /** * Register any package services. - * - * @return void */ public function register(): void { @@ -199,7 +181,7 @@ public function register(): void $this->app->singleton( CloudinaryEngine::class, function () { - return new CloudinaryEngine(); + return new CloudinaryEngine; } ); } diff --git a/src/Commands/BackupFilesCommand.php b/src/Commands/BackupFilesCommand.php index 13e831e..03ce153 100644 --- a/src/Commands/BackupFilesCommand.php +++ b/src/Commands/BackupFilesCommand.php @@ -2,14 +2,13 @@ namespace CloudinaryLabs\CloudinaryLaravel\Commands; +use CloudinaryLabs\CloudinaryLaravel\CloudinaryEngine; use Exception; use Illuminate\Console\Command; use Illuminate\Support\Facades\File; -use CloudinaryLabs\CloudinaryLaravel\CloudinaryEngine; /** * Class BackupFilesCommand - * @package CloudinaryLabs\CloudinaryLaravel\Commands */ class BackupFilesCommand extends Command { @@ -31,16 +30,13 @@ class BackupFilesCommand extends Command /** * Execute the console command. - * - * @param CloudinaryEngine $engine - * @return void */ public function handle(CloudinaryEngine $engine): void { $files = $this->getFiles(); $folder = null; - if (!$files) { + if (! $files) { $this->warn( 'There are no files in the storage/app/public directory. Use --location flag to specify the name of the directory (if there are files in there) within the storage/app directory.' ); @@ -48,7 +44,7 @@ public function handle(CloudinaryEngine $engine): void return; } - if (!config('cloudinary.cloud_url')) { + if (! config('cloudinary.cloud_url')) { $this->warn('Please ensure your Cloudinary credentials are set before continuing.'); return; @@ -78,6 +74,6 @@ public function handle(CloudinaryEngine $engine): void public function getFiles($location = 'public'): array { - return File::allFiles(storage_path("app/".$location)); + return File::allFiles(storage_path('app/'.$location)); } } diff --git a/src/Commands/DeleteFilesCommand.php b/src/Commands/DeleteFilesCommand.php index cd396b3..fe2b922 100644 --- a/src/Commands/DeleteFilesCommand.php +++ b/src/Commands/DeleteFilesCommand.php @@ -2,13 +2,12 @@ namespace CloudinaryLabs\CloudinaryLaravel\Commands; +use CloudinaryLabs\CloudinaryLaravel\CloudinaryEngine; use Exception; use Illuminate\Console\Command; -use CloudinaryLabs\CloudinaryLaravel\CloudinaryEngine; /** * Class DeleteFilesCommand - * @package CloudinaryLabs\CloudinaryLaravel\Commands */ class DeleteFilesCommand extends Command { @@ -17,7 +16,7 @@ class DeleteFilesCommand extends Command * * @var string */ - protected $signature = "cloudinary:delete {publicId}"; + protected $signature = 'cloudinary:delete {publicId}'; /** * The console command description. @@ -28,13 +27,10 @@ class DeleteFilesCommand extends Command /** * Execute the console command. - * - * @param CloudinaryEngine $engine - * @return void */ public function handle(CloudinaryEngine $engine): void { - if (!config('cloudinary.cloud_url')) { + if (! config('cloudinary.cloud_url')) { $this->warn('Please ensure your Cloudinary credentials are set before continuing.'); return; diff --git a/src/Commands/FetchFilesCommand.php b/src/Commands/FetchFilesCommand.php index c8a5024..e708fd7 100644 --- a/src/Commands/FetchFilesCommand.php +++ b/src/Commands/FetchFilesCommand.php @@ -2,13 +2,12 @@ namespace CloudinaryLabs\CloudinaryLaravel\Commands; +use CloudinaryLabs\CloudinaryLaravel\CloudinaryEngine; use Exception; use Illuminate\Console\Command; -use CloudinaryLabs\CloudinaryLaravel\CloudinaryEngine; /** * Class FetchFilesCommand - * @package CloudinaryLabs\CloudinaryLaravel\Commands */ class FetchFilesCommand extends Command { @@ -17,7 +16,7 @@ class FetchFilesCommand extends Command * * @var string */ - protected $signature = "cloudinary:fetch {publicId}"; + protected $signature = 'cloudinary:fetch {publicId}'; /** * The console command description. @@ -28,13 +27,10 @@ class FetchFilesCommand extends Command /** * Execute the console command. - * - * @param CloudinaryEngine $engine - * @return void */ public function handle(CloudinaryEngine $engine): void { - if (!config('cloudinary.cloud_url')) { + if (! config('cloudinary.cloud_url')) { $this->warn('Please ensure your Cloudinary credentials are set before continuing.'); return; @@ -42,13 +38,13 @@ public function handle(CloudinaryEngine $engine): void $publicId = $this->argument('publicId'); - if (!is_string($publicId)) { - $this->warn("Please ensure a valid public Id is passed as an argument."); + if (! is_string($publicId)) { + $this->warn('Please ensure a valid public Id is passed as an argument.'); return; } - $this->info("Fetching file..."); + $this->info('Fetching file...'); try { $url = $engine->getImage($publicId)->toUrl(); diff --git a/src/Commands/GenerateArchiveCommand.php b/src/Commands/GenerateArchiveCommand.php index e836ac5..ffb02da 100644 --- a/src/Commands/GenerateArchiveCommand.php +++ b/src/Commands/GenerateArchiveCommand.php @@ -2,13 +2,12 @@ namespace CloudinaryLabs\CloudinaryLaravel\Commands; +use CloudinaryLabs\CloudinaryLaravel\CloudinaryEngine; use Exception; use Illuminate\Console\Command; -use CloudinaryLabs\CloudinaryLaravel\CloudinaryEngine; /** * Class GenerateArchiveCommand - * @package CloudinaryLabs\CloudinaryLaravel\Commands */ class GenerateArchiveCommand extends Command { @@ -17,9 +16,9 @@ class GenerateArchiveCommand extends Command * * @var string */ - protected $signature = "cloudinary:archive + protected $signature = 'cloudinary:archive {--tags=* : The tags of the assets you want included in the arhive} - {--public_ids=* : The public IDs of the assets you want included in the archive}"; + {--public_ids=* : The public IDs of the assets you want included in the archive}'; /** * The console command description. @@ -30,19 +29,16 @@ class GenerateArchiveCommand extends Command /** * Execute the console command. - * - * @param CloudinaryEngine $engine - * @return void */ public function handle(CloudinaryEngine $engine): void { - if (!config('cloudinary.cloud_url')) { + if (! config('cloudinary.cloud_url')) { $this->warn('Please ensure your Cloudinary credentials are set before continuing.'); return; } - if (!$this->option('tags') && !$this->option('public_ids')) { + if (! $this->option('tags') && ! $this->option('public_ids')) { $this->warn( 'Please ensure you pass in at least a tag with --tags, or at least a public_id with --public_ids' ); @@ -56,7 +52,7 @@ public function handle(CloudinaryEngine $engine): void $response = $engine->createArchive( [ 'tags' => $this->option('tags') ?? null, - 'public_ids' => $this->option('public_ids') ?? null + 'public_ids' => $this->option('public_ids') ?? null, ] )['secure_url']; diff --git a/src/Commands/RenameFilesCommand.php b/src/Commands/RenameFilesCommand.php index cd9693f..8319ccf 100644 --- a/src/Commands/RenameFilesCommand.php +++ b/src/Commands/RenameFilesCommand.php @@ -2,13 +2,12 @@ namespace CloudinaryLabs\CloudinaryLaravel\Commands; +use CloudinaryLabs\CloudinaryLaravel\CloudinaryEngine; use Exception; use Illuminate\Console\Command; -use CloudinaryLabs\CloudinaryLaravel\CloudinaryEngine; /** * Class RenameFilesCommand - * @package CloudinaryLabs\CloudinaryLaravel\Commands */ class RenameFilesCommand extends Command { @@ -17,7 +16,7 @@ class RenameFilesCommand extends Command * * @var string */ - protected $signature = "cloudinary:rename {fromPublicId} {toPublicId}"; + protected $signature = 'cloudinary:rename {fromPublicId} {toPublicId}'; /** * The console command description. @@ -28,13 +27,10 @@ class RenameFilesCommand extends Command /** * Execute the console command. - * - * @param CloudinaryEngine $engine - * @return void */ public function handle(CloudinaryEngine $engine): void { - if (!config('cloudinary.cloud_url')) { + if (! config('cloudinary.cloud_url')) { $this->warn('Please ensure your Cloudinary credentials are set before continuing.'); return; @@ -43,8 +39,8 @@ public function handle(CloudinaryEngine $engine): void $fromPublicId = $this->argument('fromPublicId'); $toPublicId = $this->argument('toPublicId'); - if (!is_string($fromPublicId) || !is_string($toPublicId)) { - $this->warn("Please ensure a valid public Id is passed as an argument."); + if (! is_string($fromPublicId) || ! is_string($toPublicId)) { + $this->warn('Please ensure a valid public Id is passed as an argument.'); return; } diff --git a/src/Commands/UploadFileCommand.php b/src/Commands/UploadFileCommand.php index 8c23e29..4f21b7b 100644 --- a/src/Commands/UploadFileCommand.php +++ b/src/Commands/UploadFileCommand.php @@ -2,14 +2,12 @@ namespace CloudinaryLabs\CloudinaryLaravel\Commands; +use CloudinaryLabs\CloudinaryLaravel\CloudinaryEngine; use Exception; use Illuminate\Console\Command; -use CloudinaryLabs\CloudinaryLaravel\CloudinaryEngine; - /** * Class UploadFileCommand - * @package CloudinaryLabs\CloudinaryLaravel\Commands */ class UploadFileCommand extends Command { @@ -18,7 +16,7 @@ class UploadFileCommand extends Command * * @var string */ - protected $signature = "cloudinary:upload {remote-url}"; + protected $signature = 'cloudinary:upload {remote-url}'; /** * The console command description. @@ -29,13 +27,10 @@ class UploadFileCommand extends Command /** * Execute the console command. - * - * @param CloudinaryEngine $engine - * @return void */ public function handle(CloudinaryEngine $engine): void { - if (!config('cloudinary.cloud_url')) { + if (! config('cloudinary.cloud_url')) { $this->warn('Please ensure your Cloudinary credentials are set before continuing.'); return; @@ -47,7 +42,7 @@ public function handle(CloudinaryEngine $engine): void return; } - if (!filter_var($this->argument('remote-url'), FILTER_VALIDATE_URL)) { + if (! filter_var($this->argument('remote-url'), FILTER_VALIDATE_URL)) { $this->warn('Please add a valid remote file url as an argument.'); return; diff --git a/src/Facades/Cloudinary.php b/src/Facades/Cloudinary.php index 4a953da..a458071 100644 --- a/src/Facades/Cloudinary.php +++ b/src/Facades/Cloudinary.php @@ -2,19 +2,16 @@ namespace CloudinaryLabs\CloudinaryLaravel\Facades; -use Illuminate\Support\Facades\Facade; use CloudinaryLabs\CloudinaryLaravel\CloudinaryEngine; +use Illuminate\Support\Facades\Facade; /** * Class Cloudinary - * @package CloudinaryLabs\CloudinaryLaravel\Facades */ class Cloudinary extends Facade { /** * Get the registered name of the component. - * - * @return string */ protected static function getFacadeAccessor(): string { diff --git a/src/MediaAlly.php b/src/MediaAlly.php index 55c82a6..ab6aeac 100644 --- a/src/MediaAlly.php +++ b/src/MediaAlly.php @@ -3,8 +3,8 @@ namespace CloudinaryLabs\CloudinaryLaravel; use Cloudinary\Api\Exception\ApiError; -use Exception; use CloudinaryLabs\CloudinaryLaravel\Model\Media; +use Exception; use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Http\UploadedFile; use Illuminate\Support\Collection; @@ -14,11 +14,9 @@ * * Provides functionality for attaching Cloudinary files to an eloquent model. * Whether the model should automatically reload its media relationship after modification. - * */ trait MediaAlly { - /** * Relationship for all attached media. */ @@ -27,20 +25,20 @@ public function medially(): MorphMany return $this->morphMany(Media::class, 'medially'); } - /** * Attach Media Files to a Model + * * @throws Exception */ public function attachMedia($file, $options = []): void { - if(!$file instanceof UploadedFile) { + if (! $file instanceof UploadedFile) { throw new Exception('Please pass in a file that exists'); } $response = resolve(CloudinaryEngine::class)->uploadFile($file->getRealPath(), $options); - $media = new Media(); + $media = new Media; $media->file_name = $response->getFileName(); $media->file_url = $response->getSecurePath(); $media->size = $response->getSize(); @@ -51,13 +49,14 @@ public function attachMedia($file, $options = []): void /** * Attach Rwmote Media Files to a Model + * * @throws ApiError */ public function attachRemoteMedia($remoteFile, $options = []): void { $response = resolve(CloudinaryEngine::class)->uploadFile($remoteFile, $options); - $media = new Media(); + $media = new Media; $media->file_name = $response->getFileName(); $media->file_url = $response->getSecurePath(); $media->size = $response->getSize(); @@ -67,16 +66,16 @@ public function attachRemoteMedia($remoteFile, $options = []): void } /** - * Get all the Media files relating to a particular Model record - */ + * Get all the Media files relating to a particular Model record + */ public function fetchAllMedia(): \Illuminate\Database\Eloquent\Collection { return $this->medially()->get(); } /** - * Get the first Media file relating to a particular Model record - */ + * Get the first Media file relating to a particular Model record + */ public function fetchFirstMedia() { return $this->medially()->first(); @@ -84,11 +83,8 @@ public function fetchFirstMedia() /** * Delete all/one/multiple file(s) associated with a particular Model record - * - * @param Media|Collection|null $media - * @return void */ - public function detachMedia(Media|Collection $media = null): void + public function detachMedia(Media|Collection|null $media = null): void { $items = []; @@ -100,15 +96,15 @@ public function detachMedia(Media|Collection $media = null): void $items = $this->medially()->whereIn('id', $media->pluck('id'))->get(); } - foreach($items as $item) { + foreach ($items as $item) { resolve(CloudinaryEngine::class)->destroy($item->getFileName()); $item->delete(); } } /** - * Get the last Media file relating to a particular Model record - */ + * Get the last Media file relating to a particular Model record + */ public function fetchLastMedia() { return $this->medially()->get()->last(); @@ -116,6 +112,7 @@ public function fetchLastMedia() /** * Update the Media files relating to a particular Model record + * * @throws Exception */ public function updateMedia($file, $options = []): void @@ -126,6 +123,7 @@ public function updateMedia($file, $options = []): void /** * Update the Media files relating to a particular Model record (Specificially existing remote files) + * * @throws ApiError */ public function updateRemoteMedia($file, $options = []): void @@ -133,5 +131,4 @@ public function updateRemoteMedia($file, $options = []): void $this->detachMedia(); $this->attachRemoteMedia($file, $options); } - } diff --git a/src/Model/Media.php b/src/Model/Media.php index 1a2737e..25864d2 100644 --- a/src/Model/Media.php +++ b/src/Model/Media.php @@ -2,23 +2,19 @@ namespace CloudinaryLabs\CloudinaryLaravel\Model; +use CloudinaryLabs\CloudinaryLaravel\CloudinaryEngine; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\MorphTo; -use CloudinaryLabs\CloudinaryLaravel\CloudinaryEngine; /** * Class Media - * @package CloudinaryLabs\CloudinaryLaravel\Model */ class Media extends Model { - protected $table = 'media'; /** * Create the polymorphic relation. - * - * @return MorphTo */ public function medially(): MorphTo { @@ -27,7 +23,6 @@ public function medially(): MorphTo /** * Get the file url / path of a Media File - * @return string */ public function getSecurePath(): string { @@ -36,7 +31,6 @@ public function getSecurePath(): string /** * Get the file name of a Media File - * @return string */ public function getFileName(): string { @@ -45,7 +39,6 @@ public function getFileName(): string /** * Get the mime type of a Media File - * @return string */ public function getFileType(): string { @@ -54,7 +47,6 @@ public function getFileType(): string /** * Get the Size of a Media File - * @return Integer */ public function getSize(): int { @@ -63,7 +55,6 @@ public function getSize(): int /** * Get the Readable Size of a Media File - * @return string */ public function getReadableSize(): string { diff --git a/src/Support/helpers.php b/src/Support/helpers.php index c7dc3a5..0d9478c 100644 --- a/src/Support/helpers.php +++ b/src/Support/helpers.php @@ -2,11 +2,8 @@ use CloudinaryLabs\CloudinaryLaravel\CloudinaryEngine; -if (!function_exists("cloudinary")) { +if (! function_exists('cloudinary')) { - /** - * @return CloudinaryEngine - */ function cloudinary(): CloudinaryEngine { return app(CloudinaryEngine::class); diff --git a/tests/Fixtures/Models/Example.php b/tests/Fixtures/Models/Example.php index 4040ac4..3f38220 100644 --- a/tests/Fixtures/Models/Example.php +++ b/tests/Fixtures/Models/Example.php @@ -2,8 +2,8 @@ namespace Tests\Fixtures\Models; -use Illuminate\Database\Eloquent\Model; use CloudinaryLabs\CloudinaryLaravel\MediaAlly; +use Illuminate\Database\Eloquent\Model; class Example extends Model { diff --git a/tests/Fixtures/migrations/0001_01_01_000000_create_examples_table.php b/tests/Fixtures/migrations/0001_01_01_000000_create_examples_table.php index 18ffda7..9a1e396 100644 --- a/tests/Fixtures/migrations/0001_01_01_000000_create_examples_table.php +++ b/tests/Fixtures/migrations/0001_01_01_000000_create_examples_table.php @@ -1,8 +1,8 @@ useEnvironmentPath(__DIR__ . '/..'); + $app->useEnvironmentPath(__DIR__.'/..'); $app->bootstrapWith([LoadEnvironmentVariables::class]); parent::getEnvironmentSetUp($app); @@ -25,7 +25,7 @@ protected function getPackageProviders($app) protected function defineDatabaseMigrations() { - $this->loadMigrationsFrom(__DIR__ . '/../database/migrations'); - $this->loadMigrationsFrom(__DIR__ . '/Fixtures/migrations'); + $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); + $this->loadMigrationsFrom(__DIR__.'/Fixtures/migrations'); } } diff --git a/tests/Unit/CloudinaryAdapterTest.php b/tests/Unit/CloudinaryAdapterTest.php index 65d06c3..40245c2 100644 --- a/tests/Unit/CloudinaryAdapterTest.php +++ b/tests/Unit/CloudinaryAdapterTest.php @@ -5,9 +5,9 @@ it('can get url given public id', function () { $file = 'baz.jpg'; - $url = 'https://res.cloudinary.com/foo/image/upload/bar/' . $file; + $url = 'https://res.cloudinary.com/foo/image/upload/bar/'.$file; - $this->mock('overload:' . Cloudinary\Api\Admin\AdminApi::class, function (MockInterface $mock) use ($url) { + $this->mock('overload:'.Cloudinary\Api\Admin\AdminApi::class, function (MockInterface $mock) use ($url) { $mock->shouldReceive('asset')->once()->andReturn(['secure_url' => $url]); })->makePartial(); diff --git a/tests/Unit/MediaAllyTest.php b/tests/Unit/MediaAllyTest.php index e105d60..0b3c377 100644 --- a/tests/Unit/MediaAllyTest.php +++ b/tests/Unit/MediaAllyTest.php @@ -5,7 +5,7 @@ use Tests\Fixtures\Models\Example; beforeEach(function () { - $this->mock('overload:' . Cloudinary\Api\Upload\UploadApi::class, function (MockInterface $mock) { + $this->mock('overload:'.Cloudinary\Api\Upload\UploadApi::class, function (MockInterface $mock) { $mock->shouldReceive('upload')->andReturn([ 'public_id' => 'file', 'bytes' => '123', @@ -24,7 +24,6 @@ $example->attachMedia(UploadedFile::fake()->image('file.jpg')); - expect($example->fetchAllMedia())->toHaveCount(1); });