Completed
Pull Request — master (#94)
by Tim
05:44
created

LeagueFilesystemAdapter   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 109
c 0
b 0
f 0
wmc 9
lcom 1
cbo 1
ccs 0
cts 32
cp 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A mkdir() 0 4 2
A isFile() 0 4 1
A isDir() 0 4 1
A touch() 0 4 1
A rename() 0 4 1
A write() 0 4 1
A copy() 0 4 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Adapter\LeagueFilesystemAdapter
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Adapter;
22
23
use League\Flysystem\FilesystemInterface;
24
25
/**
26
 * Adapter for the league filesystem implementation.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2016 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import
32
 * @link      http://www.techdivision.com
33
 */
34
class LeagueFilesystemAdapter implements FilesystemAdapterInterface
35
{
36
37
    /**
38
     * The league filesystem implementation.
39
     *
40
     * @var \League\Flysystem\FilesystemInterface
41
     */
42
    protected $filesystem;
43
44
    /**
45
     * This is a utility class, so protect it against direct
46
     * instantiation.
47
     *
48
     * @param \League\Flysystem\FilesystemInterface $filesystem The league filesystem instance
49
     */
50
    public function __construct(FilesystemInterface $filesystem)
51
    {
52
        $this->filesystem = $filesystem;
53
    }
54
55
    /**
56
     * Creates a new directroy.
57
     *
58
     * @param string  $pathname The directory path
59
     * @param integer $mode     The mode is 0700 by default, which means the widest possible access
60
     *
61
     * @return boolean TRUE on success, else FALSE
62
     */
63
    public function mkdir($pathname, $mode = 0700)
64
    {
65
        return $this->filesystem->createDir($pathname, array('visibility' => $mode === 0755 ? 'public' : 'private'));
66
    }
67
68
    /**
69
     * Query whether or not the passed filename exists.
70
     *
71
     * @param string $filename The filename to query
72
     *
73
     * @return boolean TRUE if the passed filename exists, else FALSE
74
     */
75
    public function isFile($filename)
76
    {
77
        return $this->filesystem->has($filename);
78
    }
79
80
    /**
81
     * Tells whether the filename is a directory.
82
     *
83
     * @param string $filename Path to the file
84
     *
85
     * @return TRUE if the filename exists and is a directory, else FALSE
86
     */
87
    public function isDir($filename)
88
    {
89
        return $this->filesystem->has($filename);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->filesystem->has($filename); (boolean) is incompatible with the return type declared by the interface TechDivision\Import\Adap...AdapterInterface::isDir of type TechDivision\Import\Adapter\TRUE.

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...
90
    }
91
92
    /**
93
     * Creates an empty file with the passed filename.
94
     *
95
     * @param string $filename The name of the file to create
96
     *
97
     * @return boolean TRUE if the file can be created, else FALSE
98
     */
99
    public function touch($filename)
100
    {
101
        return $this->filesystem->put($filename, '');
102
    }
103
104
    /**
105
     * Renames a file or directory.
106
     *
107
     * @param string $oldname The old name
108
     * @param string $newname The new name
109
     *
110
     * @return boolean TRUE on success, else FALSE
111
     */
112
    public function rename($oldname, $newname)
113
    {
114
        return $this->filesystem->rename($oldname, $newname);
115
    }
116
117
    /**
118
     * Writes the passed data to file with the passed name.
119
     *
120
     * @param string $filename The name of the file to write the data to
121
     * @param string $data     The data to write to the file
122
     *
123
     * @return number The number of bytes written to the file
124
     */
125
    public function write($filename, $data)
126
    {
127
        return $this->filesystem->write($filename, $data);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->filesystem...rite($filename, $data); (boolean) is incompatible with the return type declared by the interface TechDivision\Import\Adap...AdapterInterface::write of type integer|double.

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...
128
    }
129
130
    /**
131
     * Copy's a file from source to destination.
132
     *
133
     * @param string $src  The source file
134
     * @param string $dest The destination file
135
     *
136
     * @return boolean TRUE on success, else FALSE
137
     */
138
    public function copy($src, $dest)
139
    {
140
        return $this->filesystem->copy($src, $dest);
141
    }
142
}
143