PathGeneratorFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 28
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 14 2
A guardAgainstInvalidPathGenerator() 0 10 3
1
<?php
2
3
namespace Spatie\MediaLibrary\PathGenerator;
4
5
use Spatie\MediaLibrary\Exceptions\InvalidPathGenerator;
6
7
class PathGeneratorFactory
8
{
9
    public static function create()
10
    {
11
        $pathGeneratorClass = BasePathGenerator::class;
12
13
        $customPathClass = config('medialibrary.path_generator');
14
15
        if ($customPathClass) {
16
            $pathGeneratorClass = $customPathClass;
17
        }
18
19
        static::guardAgainstInvalidPathGenerator($pathGeneratorClass);
20
21
        return app($pathGeneratorClass);
22
    }
23
24
    protected static function guardAgainstInvalidPathGenerator(string $pathGeneratorClass)
25
    {
26
        if (! class_exists($pathGeneratorClass)) {
27
            throw InvalidPathGenerator::doesntExist($pathGeneratorClass);
28
        }
29
30
        if (! is_subclass_of($pathGeneratorClass, PathGenerator::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Spatie\MediaLibrary\Pat...or\PathGenerator::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
31
            throw InvalidPathGenerator::isntAPathGenerator($pathGeneratorClass);
32
        }
33
    }
34
}
35