Completed
Push — develop ( 8f34f3...3bf98d )
by Jaap
13:27 queued 03:31
created

FlySystemAssets::get()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 3
nop 1
dl 0
loc 26
rs 8.5806
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of phpDocumentor.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright 2010-2016 Mike van Riel<[email protected]>
9
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
10
 * @link      http://phpdoc.org
11
 */
12
13
namespace phpDocumentor\Infrastructure\Renderer;
14
15
use League\Flysystem\File;
16
use League\Flysystem\Filesystem;
17
use League\Flysystem\FilesystemInterface;
18
use League\Flysystem\MountManager;
19
use phpDocumentor\DomainModel\Path;
20
use phpDocumentor\DomainModel\Renderer\Asset;
21
use phpDocumentor\DomainModel\Renderer\AssetNotFoundException;
22
use phpDocumentor\DomainModel\Renderer\Assets;
23
use phpDocumentor\DomainModel\Renderer\Template;
24
25
final class FlySystemAssets implements Assets
26
{
27
    /** @var FilesystemInterface|MountManager */
28
    private $filesystem;
29
30
    /**
31
     * @param FilesystemInterface|MountManager $filesystem
32
     */
33
    public function __construct($filesystem)
34
    {
35
        $this->filesystem = $filesystem;
36
    }
37
38
    /**
39
     * @inheritDoc
40
     */
41
    public function get(Path $location)
42
    {
43
        if (! $this->has($location)) {
44
            throw new AssetNotFoundException(sprintf('Asset at "%s" could not found', $location));
45
        }
46
47
        /** @var File $flyLocation */
48
        $flyLocation = $this->filesystem->get((string)$location);
49
        if ($flyLocation->isDir()) {
50
            $listing = $this->filesystem->listContents((string)$location, true);
51
            $locations = array_map(
52
                function ($value) {
53
                    if ($value['type'] === 'dir') {
54
                        return null;
55
                    }
56
57
                    return new Path($value['path']);
58
                },
59
                $listing
60
            );
61
62
            return new Asset\Folder($location, array_values(array_filter($locations)));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \phpDocumento...y_filter($locations))); (phpDocumentor\DomainModel\Renderer\Asset\Folder) is incompatible with the return type declared by the interface phpDocumentor\DomainModel\Renderer\Assets::get of type phpDocumentor\DomainModel\Renderer\Asset[].

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...
63
        }
64
65
        return new Asset($flyLocation->read());
0 ignored issues
show
Security Bug introduced by
It seems like $flyLocation->read() targeting League\Flysystem\File::read() can also be of type false; however, phpDocumentor\DomainMode...er\Asset::__construct() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
Bug Best Practice introduced by
The return type of return new \phpDocumento...($flyLocation->read()); (phpDocumentor\DomainModel\Renderer\Asset) is incompatible with the return type declared by the interface phpDocumentor\DomainModel\Renderer\Assets::get of type phpDocumentor\DomainModel\Renderer\Asset[].

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...
66
    }
67
68
    /**
69
     * @inheritDoc
70
     */
71
    public function has(Path $location)
72
    {
73
        return $this->filesystem->has((string)$location);
74
    }
75
76
    /**
77
     * Returns a new instance of this collection with the assets for the given template included.
78
     *
79
     * @param Template $template
80
     *
81
     * @return Assets
82
     */
83
    public function includeTemplateAssets(Template $template)
84
    {
85
        $assets = clone $this;
86
        if ($assets->filesystem instanceof Filesystem) {
87
            $assets->filesystem = new MountManager([$assets->filesystem]);
88
        }
89
90
        // TODO: Add template location to the mount manager
91
        // $assets->filesystem->mountFilesystem('', $template->)
92
93
        return $assets;
94
    }
95
}
96