Completed
Push — master ( 313f20...c23fe9 )
by Tim
9s
created

FilesystemTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 96%

Importance

Changes 0
Metric Value
dl 0
loc 144
c 0
b 0
f 0
wmc 11
lcom 1
cbo 1
ccs 24
cts 25
cp 0.96
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setFilesystemAdapter() 0 4 1
A getFilesystemAdapter() 0 4 1
A resolvePath() 0 21 3
A mkdir() 0 4 1
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
1
<?php
2
3
/**
4
 * TechDivision\Import\Subjects\FilesystemTrait
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\Subjects;
22
23
use TechDivision\Import\Adapter\FilesystemAdapterInterface;
24
25
/**
26
 * The trait implementation that provides filesystem handling functionality.
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
trait FilesystemTrait
35
{
36
37
    /**
38
     * Return's the root directory for the virtual filesystem.
39
     *
40
     * @return string The root directory for the virtual filesystem
41
     */
42
    protected $filesystemAdapater;
43
44
    /**
45
     * Set's the virtual filesystem instance.
46
     *
47
     * @param \TechDivision\Import\Adapter\FilesystemAdapterInterface $filesystemAdapter The filesystem adapter instance
48
     *
49
     * @return void
50
     */
51 13
    public function setFilesystemAdapter(FilesystemAdapterInterface $filesystemAdapter)
52
    {
53 13
        $this->filesystemAdapater = $filesystemAdapter;
54 13
    }
55
56
    /**
57
     * Return's the filesystem adapater instance.
58
     *
59
     * @return \TechDivision\Import\Adapter\FilesystemAdapterInterface The filesystem adapter instance
60
     */
61 13
    public function getFilesystemAdapter()
62
    {
63 13
        return $this->filesystemAdapater;
64
    }
65
66
    /**
67
     * This method tries to resolve the passed path and returns it. If the path
68
     * is relative, the actual working directory will be prepended.
69
     *
70
     * @param string $path The path to be resolved
71
     *
72
     * @return string The resolved path
73
     * @throws \InvalidArgumentException Is thrown, if the path can not be resolved
74
     */
75 3
    public function resolvePath($path)
76
    {
77
78
        // if we've an absolute path, return it immediately
79 3
        if ($this->getFilesystemAdapter()->isDir($path)) {
80
            return $path;
81
        }
82
83
        // temporarily save the path
84 3
        $originalPath = $path;
85
86
        // try to prepend the actual working directory, assuming we've a relative path
87 3
        if ($this->getFilesystemAdapter()->isDir($path = getcwd() . DIRECTORY_SEPARATOR . ltrim($path, '/'))) {
88 2
            return $path;
89
        }
90
91
        // throw an exception if the passed directory doesn't exists
92 1
        throw new \InvalidArgumentException(
93 1
            sprintf('Directory %s doesn\'t exist', $originalPath)
94
        );
95
    }
96
97
    /**
98
     * Creates a new directroy.
99
     *
100
     * @param string  $pathname  The directory path
101
     * @param integer $mode      The mode is 0777 by default, which means the widest possible access
102
     * @param string  $recursive Allows the creation of nested directories specified in the pathname
103
     *
104
     * @return boolean TRUE on success, else FALSE
105
     * @link http://php.net/mkdir
106
     */
107 1
    public function mkdir($pathname, $mode = 0700, $recursive = false)
108
    {
109 1
        return $this->getFilesystemAdapter()->mkdir($pathname, $mode, $recursive);
0 ignored issues
show
Unused Code introduced by
The call to FilesystemAdapterInterface::mkdir() has too many arguments starting with $recursive.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
110
    }
111
112
    /**
113
     * Query whether or not the passed filename exists.
114
     *
115
     * @param string $filename The filename to query
116
     *
117
     * @return boolean TRUE if the passed filename exists, else FALSE
118
     * @link http://php.net/is_file
119
     */
120 4
    public function isFile($filename)
121
    {
122 4
        return $this->getFilesystemAdapter()->isFile($filename);
123
    }
124
125
    /**
126
     * Tells whether the filename is a directory.
127
     *
128
     * @param string $filename Path to the file
129
     *
130
     * @return TRUE if the filename exists and is a directory, else FALSE
131
     * @link http://php.net/is_dir
132
     */
133 2
    public function isDir($filename)
134
    {
135 2
        return $this->getFilesystemAdapter()->isDir($filename);
136
    }
137
138
    /**
139
     * Creates an empty file with the passed filename.
140
     *
141
     * @param string $filename The name of the file to create
142
     *
143
     * @return boolean TRUE if the file can be created, else FALSE
144
     */
145 1
    public function touch($filename)
146
    {
147 1
        return $this->getFilesystemAdapter()->touch($filename);
148
    }
149
150
    /**
151
     * Renames a file or directory.
152
     *
153
     * @param string $oldname The old name
154
     * @param string $newname The new name
155
     *
156
     * @return boolean TRUE on success, else FALSE
157
     * @link http://php.net/rename
158
     */
159 1
    public function rename($oldname, $newname)
160
    {
161 1
        return $this->getFilesystemAdapter()->rename($oldname, $newname);
162
    }
163
164
    /**
165
     * Writes the passed data to file with the passed name.
166
     *
167
     * @param string $filename The name of the file to write the data to
168
     * @param string $data     The data to write to the file
169
     *
170
     * @return number The number of bytes written to the file
171
     * @link http://php.net/file_put_contents
172
     */
173 1
    public function write($filename, $data)
174
    {
175 1
        return $this->getFilesystemAdapter()->write($filename, $data);
176
    }
177
}
178