Completed
Push — 16.x ( bd3602...588b8a )
by Tim
02:11
created

LeagueFilesystemAdapter   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 200
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 200
c 0
b 0
f 0
wmc 16
lcom 1
cbo 1
ccs 0
cts 61
cp 0
rs 10

14 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 delete() 0 4 1
A copy() 0 4 1
A listContents() 0 17 2
A removeDir() 0 4 1
A glob() 0 4 1
A size() 0 4 1
A read() 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 = 0755)
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->put($filename, $data);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->filesystem->put($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
     * Delete the file with the passed name.
132
     *
133
     * @param string $filename The name of the file to be deleted
134
     *
135
     * @return boolean TRUE on success, else FALSE
136
     */
137
    public function delete($filename)
138
    {
139
        return $this->filesystem->delete($filename);
140
    }
141
142
    /**
143
     * Copy's a file from source to destination.
144
     *
145
     * @param string $src  The source file
146
     * @param string $dest The destination file
147
     *
148
     * @return boolean TRUE on success, else FALSE
149
     */
150
    public function copy($src, $dest)
151
    {
152
        return $this->filesystem->copy($src, $dest);
153
    }
154
155
    /**
156
     * List the filenames of a directory.
157
     *
158
     * @param string  $directory The directory to list
159
     * @param boolean $recursive Whether to list recursively
160
     *
161
     * @return array A list of filenames
162
     */
163
    public function listContents($directory = '', $recursive = false)
164
    {
165
166
        // initialize the array for the filenames
167
        $files = array();
168
169
        // load the filenames of the passed directory
170
        $contents = $this->filesystem->listContents($directory, $recursive);
171
172
        // prepare the array with the file names
173
        foreach ($contents as $key => $file) {
174
            $files[$key] = $file['path'];
175
        }
176
177
        // return the filenames
178
        return $files;
179
    }
180
181
    /**
182
     * Removes the passed directory recursively.
183
     *
184
     * @param string  $src       Name of the directory to remove
185
     * @param boolean $recursive TRUE if the directory has to be deleted recursive, else FALSE
186
     *
187
     * @return void
188
     * @throws \Exception Is thrown, if the directory can not be removed
189
     */
190
    public function removeDir($src, $recursive = false)
191
    {
192
        $this->filesystem->deleteDir($src);
193
    }
194
195
    /**
196
     * Find and return pathnames matching a pattern
197
     *
198
     * @param string $pattern No tilde expansion or parameter substitution is done.
199
     * @param int    $flags   Flags that changes the behaviour
200
     *
201
     * @return array Containing the matched files/directories, an empty array if no file matched or FALSE on error
202
     */
203
    public function glob(string $pattern, int $flags = 0)
204
    {
205
        throw new \Exception(sprintf('Method "%s" has not yet been implemented', __METHOD__));
206
    }
207
208
    /**
209
     * Return's the size of the file with the passed name.
210
     *
211
     * @param string $filename The name of the file to return the size for
212
     *
213
     * @return int The size of the file in bytes
214
     * @throws \Exception  Is thrown, if the size can not be calculated
215
     */
216
    public function size($filename)
217
    {
218
        return $this->filesystem->getSize($filename);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression $this->filesystem->getSize($filename); of type integer|false adds false to the return on line 218 which is incompatible with the return type declared by the interface TechDivision\Import\Adap...mAdapterInterface::size of type integer. It seems like you forgot to handle an error condition.
Loading history...
219
    }
220
221
    /**
222
     * Read's and return's the content of the file with the passed name as array.
223
     *
224
     * @param string $filename The name of the file to return its content for
225
     *
226
     * @return array The content of the file as array
227
     * @throws \Exception  Is thrown, if the file is not accessible
228
     */
229
    public function read($filename)
230
    {
231
        return $this->filesystem->read($filename);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->filesystem->read($filename); (string|false) is incompatible with the return type declared by the interface TechDivision\Import\Adap...mAdapterInterface::read of type array.

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...
232
    }
233
}
234