Completed
Pull Request — master (#89)
by Tim
06:59
created

FilesystemTrait::rename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 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 League\Flysystem\FilesystemInterface;
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 virtual filesystem instance.
39
     *
40
     * @var \League\Flysystem\FilesystemInterface
41
     */
42
    protected $filesystem;
43
44
    /**
45
     * The root directory for the virtual filesystem.
46
     *
47
     * @var string
48
     */
49
    protected $rootDir;
50
51
    /**
52
     * Set's root directory for the virtual filesystem.
53
     *
54
     * @param string $rootDir The root directory for the virtual filesystem
55
     *
56
     * @return void
57
     */
58 1
    public function setRootDir($rootDir)
59
    {
60 1
        $this->rootDir = $rootDir;
61 1
    }
62
63
    /**
64
     * Return's the root directory for the virtual filesystem.
65
     *
66
     * @return string The root directory for the virtual filesystem
67
     */
68 1
    public function getRootDir()
69
    {
70 1
        return $this->rootDir;
71
    }
72
73
    /**
74
     * Set's the virtual filesystem instance.
75
     *
76
     * @param \League\Flysystem\FilesystemInterface $filesystem The filesystem instance
77
     *
78
     * @return void
79
     */
80 7
    public function setFilesystem(FilesystemInterface $filesystem)
81
    {
82 7
        $this->filesystem = $filesystem;
83 7
    }
84
85
    /**
86
     * Return's the virtual filesystem instance.
87
     *
88
     * @return \League\Flysystem\FilesystemInterface The filesystem instance
89
     */
90 7
    public function getFilesystem()
91
    {
92 7
        return $this->filesystem;
93
    }
94
95
    /**
96
     * This method tries to resolve the passed path and returns it. If the path
97
     * is relative, the actual working directory will be prepended.
98
     *
99
     * @param string $path The path to be resolved
100
     *
101
     * @return string The resolved path
102
     * @throws \InvalidArgumentException Is thrown, if the path can not be resolved
103
     */
104 3
    public function resolvePath($path)
105
    {
106
107
        // if we've an absolute path, return it immediately
108 3
        if ($this->getFilesystem()->has($path)) {
109 1
            return $path;
110
        }
111
112
        // temporarily save the path
113 2
        $originalPath = $path;
114
115
        // try to prepend the actual working directory, assuming we've a relative path
116 2
        if ($this->getFilesystem()->has($path = getcwd() . DIRECTORY_SEPARATOR . $path)) {
117 1
            return $path;
118
        }
119
120
        // throw an exception if the passed directory doesn't exists
121 1
        throw new \InvalidArgumentException(
122 1
            sprintf('Directory %s doesn\'t exist', $originalPath)
123
        );
124
    }
125
126
    /**
127
     * Creates a new directroy.
128
     *
129
     * @param string  $pathname  The directory path
130
     * @param integer $mode      The mode is 0777 by default, which means the widest possible access
131
     * @param string  $recursive Allows the creation of nested directories specified in the pathname
132
     *
133
     * @return boolean TRUE on success, else FALSE
134
     * @link http://php.net/mkdir
135
     */
136 1
    public function mkdir($pathname, $mode = 0700, $recursive = false)
137
    {
138 1
        return mkdir($pathname, $mode, $recursive);
139
    }
140
141
    /**
142
     * Query whether or not the passed filename exists.
143
     *
144
     * @param string $filename The filename to query
145
     *
146
     * @return boolean TRUE if the passed filename exists, else FALSE
147
     * @link http://php.net/is_file
148
     */
149 4
    public function isFile($filename)
150
    {
151 4
        return is_file($filename);
152
    }
153
154
    /**
155
     * Tells whether the filename is a directory.
156
     *
157
     * @param string $filename Path to the file
158
     *
159
     * @return TRUE if the filename exists and is a directory, else FALSE
160
     * @link http://php.net/is_dir
161
     */
162 2
    public function isDir($filename)
163
    {
164 2
        return is_dir($filename);
165
    }
166
167
    /**
168
     * Creates an empty file with the passed filename.
169
     *
170
     * @param string $filename The name of the file to create
171
     *
172
     * @return boolean TRUE if the file can be created, else FALSE
173
     */
174 1
    public function touch($filename)
175
    {
176 1
        return touch($filename);
177
    }
178
179
    /**
180
     * Renames a file or directory.
181
     *
182
     * @param string $oldname The old name
183
     * @param string $newname The new name
184
     *
185
     * @return boolean TRUE on success, else FALSE
186
     * @link http://php.net/rename
187
     */
188 1
    public function rename($oldname, $newname)
189
    {
190 1
        return rename($oldname, $newname);
191
    }
192
193
    /**
194
     * Writes the passed data to file with the passed name.
195
     *
196
     * @param string $filename The name of the file to write the data to
197
     * @param string $data     The data to write to the file
198
     *
199
     * @return number The number of bytes written to the file
200
     * @link http://php.net/file_put_contents
201
     */
202 1
    public function write($filename, $data)
203
    {
204 1
        return file_put_contents($filename, $data);
205
    }
206
}
207