BasicStorage   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 84
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A addSubFolder() 0 12 2
A save() 0 16 2
A getFolder() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of InFw\FileManager package.
5
 */
6
7
namespace InFw\FileManager;
8
9
use InFw\File\FileInterface;
10
11
/**
12
 * Class BasicStorage.
13
 */
14
class BasicStorage implements StorageInterface
15
{
16
    /**
17
     * Root folder for filesystem.
18
     *
19
     * @var string
20
     */
21
    private $root;
22
23
    /**
24
     * Path to storage from root.
25
     *
26
     * @var string
27
     */
28
    private $subFolder;
29
30
    /**
31
     * BasicStorage constructor.
32
     *
33
     * @param string $rootFolder
34
     */
35
    public function __construct($rootFolder)
36
    {
37
        if (false === is_dir($rootFolder)) {
38
            throw new \InvalidArgumentException(
39
                'Given folder ' . $rootFolder . ' does not exist.'
40
            );
41
        }
42
43
        $this->root = $rootFolder;
44
    }
45
46
    /**
47
     * Add sub-path to root.
48
     *
49
     * @param string $subFolder
50
     */
51
    public function addSubFolder($subFolder = '')
52
    {
53
        $path = $this->root . $this->subFolder;
54
55
        if (false === is_dir($path)) {
56
            throw new \InvalidArgumentException(
57
                'Given folder ' . $path . ' does not exist.'
58
            );
59
        }
60
61
        $this->subFolder = $subFolder;
62
    }
63
64
    /**
65
     * Persist file in a filesystem.
66
     *
67
     * @param FileInterface $file
68
     *
69
     * @return FileInterface
70
     */
71
    public function save(FileInterface $file)
72
    {
73
        if (false === file_exists($file->getTmpName())
74
        ) {
75
            throw new \InvalidArgumentException(
76
                'Cannot upload file to given folder.'
77
            );
78
        }
79
80
        file_put_contents(
81
            $this->root . $this->subFolder . $file->getName(),
82
            $file->getTmpName()
83
        );
84
85
        return $file;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $file; (InFw\File\FileInterface) is incompatible with the return type declared by the interface InFw\FileManager\StorageInterface::save of type boolean.

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...
86
    }
87
88
    /**
89
     * Get Folder.
90
     *
91
     * @return string
92
     */
93
    public function getFolder()
94
    {
95
        return $this->root . $this->subFolder;
96
    }
97
}
98