Completed
Push — 16.x ( f36db0...50411d )
by Tim
07:07
created

FilesystemTrait::mkdir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
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
     * The filesystem adapter instance.
39
     *
40
     * @return \TechDivision\Import\Adapter\FilesystemAdapterInterface
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
        // try to prepend the actual working directory, assuming we've a relative path
84 3
        if ($this->getFilesystemAdapter()->isDir($realpath = $this->realpath($path))) {
85
            return $realpath;
86
        }
87 3
88 2
        // throw an exception if the passed directory doesn't exists
89
        throw new \InvalidArgumentException(
90
            sprintf('Directory %s doesn\'t exist', $path)
91
        );
92 1
    }
93 1
94
    /**
95
     * Return's the resolved directory of the passed path.
96
     *
97
     * @param string $path The path to resolve
98
     *
99
     * @return string The resolved path
100
     */
101
    private function realpath(string $path = null) : string
102
    {
103
104
        // initialize the installation directory with the actual
105
        // working directory for backwards compatibility
106
        $installationDir = getcwd();
107 1
108
        // query whether or not the trait is used by a subject instance so we
109 1
        // can load the installation directory from the configuration itself
110
        if ($this instanceof SubjectInterface) {
111
            $installationDir = $this->getConfiguration()->getConfiguration()->getInstallationDir();
112
        }
113
114
        // concatenate and return the absolute path by prefixing the installation directory
115
        return $installationDir . DIRECTORY_SEPARATOR . ltrim($path, '/');
116
    }
117
118
    /**
119
     * Creates a new directroy.
120 4
     *
121
     * @param string  $pathname  The directory path
122 4
     * @param integer $mode      The mode is 0777 by default, which means the widest possible access
123
     * @param string  $recursive Allows the creation of nested directories specified in the pathname
124
     *
125
     * @return boolean TRUE on success, else FALSE
126
     * @link http://php.net/mkdir
127
     */
128
    public function mkdir($pathname, $mode = 0700, $recursive = false)
129
    {
130
        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...
131
    }
132
133 2
    /**
134
     * Query whether or not the passed filename exists.
135 2
     *
136
     * @param string $filename The filename to query
137
     *
138
     * @return boolean TRUE if the passed filename exists, else FALSE
139
     * @link http://php.net/is_file
140
     */
141
    public function isFile($filename)
142
    {
143
        return $this->getFilesystemAdapter()->isFile($filename);
144
    }
145 1
146
    /**
147 1
     * Tells whether the filename is a directory.
148
     *
149
     * @param string $filename Path to the file
150
     *
151
     * @return TRUE if the filename exists and is a directory, else FALSE
152
     * @link http://php.net/is_dir
153
     */
154
    public function isDir($filename)
155
    {
156
        return $this->getFilesystemAdapter()->isDir($filename);
157
    }
158
159 1
    /**
160
     * Creates an empty file with the passed filename.
161 1
     *
162
     * @param string $filename The name of the file to create
163
     *
164
     * @return boolean TRUE if the file can be created, else FALSE
165
     */
166
    public function touch($filename)
167
    {
168
        return $this->getFilesystemAdapter()->touch($filename);
169
    }
170
171
    /**
172
     * Renames a file or directory.
173 1
     *
174
     * @param string $oldname The old name
175 1
     * @param string $newname The new name
176
     *
177
     * @return boolean TRUE on success, else FALSE
178
     * @link http://php.net/rename
179
     */
180
    public function rename($oldname, $newname)
181
    {
182
        return $this->getFilesystemAdapter()->rename($oldname, $newname);
183
    }
184
185
    /**
186
     * Writes the passed data to file with the passed name.
187
     *
188
     * @param string $filename The name of the file to write the data to
189
     * @param string $data     The data to write to the file
190
     *
191
     * @return number The number of bytes written to the file
192
     * @link http://php.net/file_put_contents
193
     */
194
    public function write($filename, $data)
195
    {
196
        return $this->getFilesystemAdapter()->write($filename, $data);
197
    }
198
}
199