Skip to content

Commit

Permalink
Fixing issue #26 (#27)
Browse files Browse the repository at this point in the history
* Added an `Okipa\MediaLibraryExt\Exceptions\CollectionNotFound` exception throw when the given collection is not found in the targeted model.
  • Loading branch information
aquintard authored Jan 21, 2021
1 parent 305cdac commit 91616bb
Show file tree
Hide file tree
Showing 15 changed files with 189 additions and 137 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Changelog

## [9.1.0](https://github.com/Okipa/laravel-medialibrary-ext/compare/8.1.0...Okipa:9.0.0)
## [9.2.0](https://github.com/Okipa/laravel-medialibrary-ext/compare/9.1.0...9.2.0)

2020-11-14

* Added an `Okipa\MediaLibraryExt\Exceptions\CollectionNotFound` exception throw when the given collection is not found in the targeted model.

## [9.1.0](https://github.com/Okipa/laravel-medialibrary-ext/compare/9.0.0...9.1.0)

2020-11-14

Expand Down
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ This package extension will follow the [base package](https://github.com/spatie/
* [Installation](#installation)
* [Documentation](#documentation)
* [Translations](#translations)
* [Extra features](#extra-features)
* [Extension features](#extension-features)
* [Validation rules](#media-validation-rules)
* [Media caption](#media-caption)
* [Testing](#testing)
Expand Down Expand Up @@ -96,7 +96,7 @@ Here is the list of the sentences available for translation:
* `{1}Accepted type: :types.|[2,*]Accepted types: :types.`
* `Max. file size: :size Mb.`

## Extra features
## Extension features

### Media validation rules

Expand All @@ -120,13 +120,13 @@ Will generate:
['mimetypes:image/jpeg,image/png', 'mimes:jpg,jpeg,jpe,png', 'dimensions:min_width=60,min_height=20', 'max:5000'];
```

#### Available methods:
#### Available public methods:

* `->getMediaValidationRules(string $collectionName): array`: returns all the validation rules for the given collection.
* `->getMediaMimesValidationRules(string $collectionName): string`: returns only the mimes validation rule for the given collection.
* `->getMediaMimeTypesValidationRules(string $collectionName): string`: returns only the mime types validation rule for the given collection.
* `->getMediaDimensionValidationRules(string $collectionName): string`: returns only the dimensions validation rule for the given collection.
* `->getMediaSizeValidationRule(): string`: returns only the config max file size validation rule.
* `->getMediaMimesValidationRules(string $collectionName): string`: returns only the mimes validation rules for the given collection.
* `->getMediaMimeTypesValidationRules(string $collectionName): string`: returns only the mime types validation rules for the given collection.
* `->getMediaDimensionValidationRules(string $collectionName): string`: returns only the dimension validation rules for the given collection.
* `->getMediaSizeValidationRule(): string`: returns only the max file size validation rule set from the base package `media-library.max_file_size` configuration value.

### Media caption

Expand All @@ -146,13 +146,17 @@ Will generate:
Min. width: 150 px. Min. height: 70 px. Accepted types: jpg, jpeg, jpe, png. Max file size: 5Mb.
```

#### Available methods:
#### Available public methods:

* `getMediaCaption(string $collectionName): string`: returns a complete caption for the given collection.
* `getMediaDimensionsCaption(string $collectionName): string`: returns only the dimensions caption for the given collection.
* `getMediaMimeTypesCaption(string $collectionName): string`: returns only the mime types caption for the given collection.
* `getMediaSizeCaption(): string`: returns only the config max file size caption only.

### Exceptions

In order to avoid careless mistakes when using public methods that are requiring a `string $collectionName` argument provided by this extension, an `Okipa\MediaLibraryExt\Exceptions\CollectionNotFound` exception will be thrown when the given collection name is not found in the targeted model.

## Testing

``` bash
Expand Down
2 changes: 1 addition & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<rule ref="PSR1.Classes.ClassDeclaration.MissingNamespace">
<exclude-pattern>./tests/*</exclude-pattern>
</rule>
<rule ref="PSR1.Methods.CamelCapsMethodName">
<rule ref="PSR1.Methods.CamelCapsMethodName.NotCamelCaps">
<exclude-pattern>./tests/*</exclude-pattern>
</rule>
<rule ref="PSR12"/>
Expand Down
10 changes: 10 additions & 0 deletions src/Exceptions/CollectionNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Okipa\MediaLibraryExt\Exceptions;

use Exception;

class CollectionNotFound extends Exception
{
//
}
116 changes: 97 additions & 19 deletions src/ExtendsMediaAbilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,27 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Okipa\MediaLibraryExt\Exceptions\CollectionNotFound;
use Spatie\MediaLibrary\Conversions\Conversion;
use Spatie\MediaLibrary\MediaCollections\MediaCollection;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Symfony\Component\Mime\MimeTypes;

trait ExtendsMediaAbilities
{
abstract public function getMediaCollection(string $collectionName = 'default'): ?MediaCollection;

abstract public function registerAllMediaConversions(Media $media = null): void;

/**
* @param string $collectionName
*
* @return string
* @throws \Okipa\MediaLibraryExt\Exceptions\CollectionNotFound
*/
public function getMediaCaption(string $collectionName): string
{
if (! $this->getMediaCollection($collectionName)) {
return '';
}
$this->checkIfMediaCollectionExist($collectionName);
$dimensionsCaption = $this->getMediaDimensionsCaption($collectionName);
$mimeTypesCaption = $this->getMediaMimeTypesCaption($collectionName);
$sizeCaption = $this->getMediaSizeCaption();
Expand All @@ -28,8 +37,25 @@ public function getMediaCaption(string $collectionName): string
. $sizeCaption;
}

abstract public function getMediaCollection(string $collectionName = 'default'): ?MediaCollection;
/**
* @param string $collectionName
*
* @throws \Okipa\MediaLibraryExt\Exceptions\CollectionNotFound
*/
protected function checkIfMediaCollectionExist(string $collectionName): void
{
$mediaCollection = $this->getMediaCollection($collectionName);
if (! $mediaCollection) {
throw new CollectionNotFound("The collection {$mediaCollection} as not been found.");
}
}

/**
* @param string $collectionName
*
* @return string
* @throws \Okipa\MediaLibraryExt\Exceptions\CollectionNotFound
*/
public function getMediaDimensionsCaption(string $collectionName): string
{
$maxDimensions = $this->getMediaMaxDimensions($collectionName);
Expand All @@ -46,6 +72,12 @@ public function getMediaDimensionsCaption(string $collectionName): string
return $caption;
}

/**
* @param string $collectionName
*
* @return array
* @throws \Okipa\MediaLibraryExt\Exceptions\CollectionNotFound
*/
protected function getMediaMaxDimensions(string $collectionName): array
{
if (! $this->mediaHasDimensions($collectionName)) {
Expand All @@ -65,20 +97,40 @@ protected function getMediaMaxDimensions(string $collectionName): array
return $this->getMaxWidthAndMaxHeight($sizes);
}

/**
* @param string $collectionName
*
* @return bool
* @throws \Okipa\MediaLibraryExt\Exceptions\CollectionNotFound
*/
protected function mediaHasDimensions(string $collectionName): bool
{
$mediaCollection = $this->getMediaCollection($collectionName);
if (! $mediaCollection) {
return false;
}
$mediaCollection = $this->getMediaCollectionOrFail($collectionName);
$mediaConversions = $this->getAllMediaConversions($collectionName);
if ($mediaConversions->isEmpty()) {
return false;
}

return ! count(array_filter($mediaCollection->acceptsMimeTypes, function ($mimeTypes) {
return ! Str::startsWith($mimeTypes, 'image');
}));
return ! count(array_filter(
$mediaCollection->acceptsMimeTypes,
static fn($mimeTypes) => ! Str::startsWith($mimeTypes, 'image')
));
}

/**
* @param string $collectionName
*
* @return \Spatie\MediaLibrary\MediaCollections\MediaCollection
* @throws \Okipa\MediaLibraryExt\Exceptions\CollectionNotFound
*/
protected function getMediaCollectionOrFail(string $collectionName = 'default'): MediaCollection
{
$mediaCollection = $this->getMediaCollection($collectionName);
if (! $mediaCollection) {
throw new CollectionNotFound("The collection {$mediaCollection} as not been found.");
}

return $mediaCollection;
}

protected function getAllMediaConversions(string $collectionName): Collection
Expand All @@ -90,8 +142,6 @@ protected function getAllMediaConversions(string $collectionName): Collection
}));
}

abstract public function registerAllMediaConversions(Media $media = null): void;

protected function getMaxWidthAndMaxHeight(array $sizes): array
{
$width = ! empty($sizes) ? max(Arr::pluck($sizes, 'width')) : null;
Expand All @@ -100,9 +150,15 @@ protected function getMaxWidthAndMaxHeight(array $sizes): array
return compact('width', 'height');
}

/**
* @param string $collectionName
*
* @return string
* @throws \Okipa\MediaLibraryExt\Exceptions\CollectionNotFound
*/
public function getMediaMimeTypesCaption(string $collectionName): string
{
$mediaCollection = $this->getMediaCollection($collectionName);
$mediaCollection = $this->getMediaCollectionOrFail($collectionName);
if (! empty($mediaCollection->acceptsMimeTypes)) {
$extensions = $this->getExtensionsFromMimeTypes($mediaCollection->acceptsMimeTypes);
$extensionsString = implode(',', $extensions);
Expand Down Expand Up @@ -140,11 +196,15 @@ public function getMediaSizeCaption(): string
: '';
}

/**
* @param string $collectionName
*
* @return array
* @throws \Okipa\MediaLibraryExt\Exceptions\CollectionNotFound
*/
public function getMediaValidationRules(string $collectionName): array
{
if (! $this->getMediaCollection($collectionName)) {
return [];
}
$this->checkIfMediaCollectionExist($collectionName);
$mimesConstraints = $this->getMediaMimesValidationRules($collectionName);
$mimeTypeConstraints = $this->getMediaMimeTypesValidationRules($collectionName);
$dimensionConstraints = $this->getMediaDimensionValidationRules($collectionName);
Expand All @@ -158,9 +218,15 @@ public function getMediaValidationRules(string $collectionName): array
]));
}

/**
* @param string $collectionName
*
* @return string
* @throws \Okipa\MediaLibraryExt\Exceptions\CollectionNotFound
*/
public function getMediaMimesValidationRules(string $collectionName): string
{
$mediaCollection = $this->getMediaCollection($collectionName);
$mediaCollection = $this->getMediaCollectionOrFail($collectionName);
$validationString = '';
if (! empty($mediaCollection->acceptsMimeTypes)) {
$acceptedExtensions = $this->getExtensionsFromMimeTypes($mediaCollection->acceptsMimeTypes);
Expand All @@ -172,13 +238,19 @@ public function getMediaMimesValidationRules(string $collectionName): string
return $validationString;
}

/**
* @param string $collectionName
*
* @return string
* @throws \Okipa\MediaLibraryExt\Exceptions\CollectionNotFound
*/
public function getMediaMimeTypesValidationRules(string $collectionName): string
{
$mediaCollection = $this->getMediaCollectionOrFail($collectionName);
$mediaConversions = $this->getAllMediaConversions($collectionName);
if ($mediaConversions->isEmpty()) {
return '';
}
$mediaCollection = $this->getMediaCollection($collectionName);
$validationString = '';
if (! empty($mediaCollection->acceptsMimeTypes)) {
$validationString .= 'mimetypes:' . implode(',', $mediaCollection->acceptsMimeTypes);
Expand All @@ -187,6 +259,12 @@ public function getMediaMimeTypesValidationRules(string $collectionName): string
return $validationString;
}

/**
* @param string $collectionName
*
* @return string
* @throws \Okipa\MediaLibraryExt\Exceptions\CollectionNotFound
*/
public function getMediaDimensionValidationRules(string $collectionName): string
{
$maxDimensions = $this->getMediaMaxDimensions($collectionName);
Expand Down
2 changes: 1 addition & 1 deletion tests/MediaLibraryExtTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class MediaLibraryExtTestCase extends TestCase
{
protected function getPackageProviders($app)
protected function getPackageProviders($app): array
{
return [MediaLibraryServiceProvider::class];
}
Expand Down
10 changes: 5 additions & 5 deletions tests/Unit/Captions/CollectionCaptionsTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

namespace Okipa\MediaLibraryExt\Tests\Unit\Extension\UrlGenerator;
namespace Okipa\MediaLibraryExt\Tests\Unit\Captions;

use Okipa\MediaLibraryExt\Exceptions\CollectionNotFound;
use Okipa\MediaLibraryExt\Tests\MediaLibraryExtTestCase;
use Okipa\MediaLibraryExt\Tests\Models\InteractsWithMediaModel;
use Spatie\Image\Manipulations;
Expand All @@ -10,11 +11,10 @@
class CollectionCaptionsTest extends MediaLibraryExtTestCase
{
/** @test */
public function it_returns_none_with_non_existing_collection(): void
public function it_throws_exception_when_it_is_called_with_non_existing_collection(): void
{
config()->set('media-library.max_file_size', 1000000);
$captionString = (new InteractsWithMediaModel())->getMediaCaption('test');
self::assertEquals('', $captionString);
$this->expectException(CollectionNotFound::class);
(new InteractsWithMediaModel())->getMediaCaption('test');
}

/** @test */
Expand Down
Loading

0 comments on commit 91616bb

Please sign in to comment.