Completed
Push — master ( 6526ca...e08ab6 )
by Rias
01:40 queued 11s
created

BaseUrlGenerator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 3
dl 0
loc 86
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setMedia() 0 6 1
A setConversion() 0 6 1
A setPathGenerator() 0 6 1
A getPathRelativeToRoot() 0 12 2
A rawUrlEncodeFilename() 0 4 1
A versionUrl() 0 8 2
1
<?php
2
3
namespace Spatie\MediaLibrary\UrlGenerator;
4
5
use Spatie\MediaLibrary\Models\Media;
6
use Spatie\MediaLibrary\Conversion\Conversion;
7
use Spatie\MediaLibrary\PathGenerator\PathGenerator;
8
use Illuminate\Contracts\Config\Repository as Config;
9
10
abstract class BaseUrlGenerator implements UrlGenerator
11
{
12
    /** @var \Spatie\MediaLibrary\Models\Media */
13
    protected $media;
14
15
    /** @var \Spatie\MediaLibrary\Conversion\Conversion|null */
16
    protected $conversion;
17
18
    /** @var \Spatie\MediaLibrary\PathGenerator\PathGenerator */
19
    protected $pathGenerator;
20
21
    /** @var \Illuminate\Contracts\Config\Repository */
22
    protected $config;
23
24
    /** @param \Illuminate\Contracts\Config\Repository $config */
25
    public function __construct(Config $config)
26
    {
27
        $this->config = $config;
28
    }
29
30
    /**
31
     * @param \Spatie\MediaLibrary\Models\Media $media
32
     *
33
     * @return \Spatie\MediaLibrary\UrlGenerator\UrlGenerator
34
     */
35
    public function setMedia(Media $media): UrlGenerator
36
    {
37
        $this->media = $media;
38
39
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Spatie\MediaLibrary\UrlGenerator\BaseUrlGenerator) is incompatible with the return type declared by the interface Spatie\MediaLibrary\UrlG...\UrlGenerator::setMedia of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
40
    }
41
42
    /**
43
     * @param \Spatie\MediaLibrary\Conversion\Conversion $conversion
44
     *
45
     * @return \Spatie\MediaLibrary\UrlGenerator\UrlGenerator
46
     */
47
    public function setConversion(Conversion $conversion): UrlGenerator
48
    {
49
        $this->conversion = $conversion;
50
51
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Spatie\MediaLibrary\UrlGenerator\BaseUrlGenerator) is incompatible with the return type declared by the interface Spatie\MediaLibrary\UrlG...enerator::setConversion of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
52
    }
53
54
    /**
55
     * @param \Spatie\MediaLibrary\PathGenerator\PathGenerator $pathGenerator
56
     *
57
     * @return \Spatie\MediaLibrary\UrlGenerator\UrlGenerator
58
     */
59
    public function setPathGenerator(PathGenerator $pathGenerator): UrlGenerator
60
    {
61
        $this->pathGenerator = $pathGenerator;
62
63
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Spatie\MediaLibrary\UrlGenerator\BaseUrlGenerator) is incompatible with the return type declared by the interface Spatie\MediaLibrary\UrlG...rator::setPathGenerator of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
64
    }
65
66
    /*
67
     * Get the path to the requested file relative to the root of the media directory.
68
     */
69
    public function getPathRelativeToRoot(): string
70
    {
71
        if (is_null($this->conversion)) {
72
            return $this->pathGenerator->getPath($this->media).($this->media->file_name);
73
        }
74
75
        return $this->pathGenerator->getPathForConversions($this->media)
76
            .pathinfo($this->media->file_name, PATHINFO_FILENAME)
77
            .'-'.$this->conversion->getName()
78
            .'.'
79
            .$this->conversion->getResultExtension($this->media->extension);
80
    }
81
82
    public function rawUrlEncodeFilename(string $path = ''): string
83
    {
84
        return pathinfo($path, PATHINFO_DIRNAME).'/'.rawurlencode(pathinfo($path, PATHINFO_BASENAME));
85
    }
86
87
    public function versionUrl(string $path = ''): string
88
    {
89
        if (! $this->config->get('medialibrary.version_urls')) {
90
            return $path;
91
        }
92
93
        return "{$path}?v={$this->media->updated_at->timestamp}";
94
    }
95
}
96