Skip to content

Commit

Permalink
refactor: moved custom path generator equal conditions to a separate …
Browse files Browse the repository at this point in the history
…method
  • Loading branch information
aprokopenko committed Mar 3, 2023
1 parent a760283 commit cc276d0
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/Support/PathGenerator/PathGeneratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class PathGeneratorFactory
{
public static function create(Media $media): PathGenerator
{
$pathGeneratorClass = self::getPathGeneratorClass($media);
$pathGeneratorClass = static::getPathGeneratorClass($media);

static::guardAgainstInvalidPathGenerator($pathGeneratorClass);

Expand All @@ -22,21 +22,32 @@ protected static function getPathGeneratorClass(Media $media)
$defaultPathGeneratorClass = config('media-library.path_generator');

foreach (config('media-library.custom_path_generators', []) as $modelClass => $customPathGeneratorClass) {
if (
// model doesn't have morphMap
is_a($media->model_type, $modelClass, true)
// config is set via morphMap alias
|| $media->model_type === $modelClass
// config is set via morphMap class name
|| is_a((string)Relation::getMorphedModel($media->model_type), $modelClass, true)
) {
if (static::assertMediaModelTypeEqualsTo($media, $modelClass)) {
return $customPathGeneratorClass;
}
}

return $defaultPathGeneratorClass;
}

protected static function assertMediaModelTypeEqualsTo(Media $media, string $modelClass): bool
{
// model doesn't have morphMap, so morph type and class are equal
if (is_a($media->model_type, $modelClass, true)) {
return true;
}
// config is set via morphMap alias
if ($media->model_type === $modelClass) {
return true;
}
// config is set via morphMap class name
if (is_a((string)Relation::getMorphedModel($media->model_type), $modelClass, true)) {
return true;
}

return false;
}

protected static function guardAgainstInvalidPathGenerator(string $pathGeneratorClass): void
{
if (! class_exists($pathGeneratorClass)) {
Expand Down

0 comments on commit cc276d0

Please sign in to comment.