Completed
Push — master ( f61a47...233e03 )
by Hong
02:41
created

Path::resetError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Storage
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Storage;
16
17
use Phossa2\Storage\Message\Message;
18
use Phossa2\Shared\Base\ObjectAbstract;
19
use Phossa2\Shared\Error\ErrorAwareTrait;
20
use Phossa2\Shared\Error\ErrorAwareInterface;
21
use Phossa2\Storage\Interfaces\PathInterface;
22
use Phossa2\Storage\Traits\FilesystemAwareTrait;
23
use Phossa2\Shared\Extension\ExtensionAwareTrait;
24
use Phossa2\Storage\Interfaces\FilesystemInterface;
25
use Phossa2\Shared\Extension\ExtensionAwareInterface;
26
use Phossa2\Storage\Interfaces\FilesystemAwareInterface;
27
use Phossa2\Storage\Interfaces\DriverInterface;
28
29
/**
30
 * Path
31
 *
32
 * @package Phossa2\Storage
33
 * @author  Hong Zhang <[email protected]>
34
 * @see     ObjectAbstract
35
 * @see     PathInterface
36
 * @see     ErrorAwareInterface
37
 * @see     FilesystemAwareInterface
38
 * @version 2.0.0
39
 * @since   2.0.0 added
40
 */
41
class Path extends ObjectAbstract implements PathInterface, ErrorAwareInterface, FilesystemAwareInterface, ExtensionAwareInterface
42
{
43
    use FilesystemAwareTrait, ErrorAwareTrait, ExtensionAwareTrait;
44
45
    /**
46
     * Full path with mount point
47
     *
48
     * @var    string
49
     * @access protected
50
     */
51
    protected $full;
52
53
    /**
54
     * relative path without mount point prefix
55
     *
56
     * @var    string
57
     * @access protected
58
     */
59
    protected $path;
60
61
    /**
62
     * Instantiate the path object
63
     *
64
     * @param  string $full full path
65
     * @param  string $path relative path without mount point
66
     * @param  FilesystemInterface $filesystem
67
     * @access public
68
     * @api
69
     */
70
    public function __construct(
71
        /*# string */ $full,
72
        /*# string */ $path,
73
        FilesystemInterface $filesystem
74
    ) {
75
        $this->setFilesystem($filesystem);
76
        $this->full = $full;
77
        $this->path = $path;
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83
    public function exists()/*# : bool */
84
    {
85
        if (!$this->getDriver()->exists($this->path)) {
86
            return $this->setError(
87
                Message::get(Message::MSG_PATH_NOTFOUND, $this->full),
88
                Message::MSG_PATH_NOTFOUND
89
            );
90
        }
91
        return true;
92
    }
93
94
    /**
95
     * {@inheritDoc}
96
     */
97 View Code Duplication
    public function getContent(/*# bool */ $stream = false)
98
    {
99
        // not exists or filesystem not readable
100
        if (!$this->exists() || !$this->isFilesystemReadable()) {
101
            return null;
102
        }
103
104
        $res = $this->getDriver()->getContent($this->path, $stream);
105
        $this->resetError();
106
        return $res;
107
    }
108
109
    /**
110
     * {@inheritDoc}
111
     */
112
    public function getMeta()/*# : array */
113
    {
114
        if (!$this->exists()) {
115
            return [];
116
        }
117
118
        $res = $this->getDriver()->getMeta($this->path);
119
        $this->resetError();
120
        return $res;
121
    }
122
123
    /**
124
     * {@inheritDoc}
125
     */
126
    public function getPath()/*# : string */
127
    {
128
        return $this->path;
129
    }
130
131
    /**
132
     * {@inheritDoc}
133
     */
134
    public function getFullPath()/*# : string */
135
    {
136
        return $this->full;
137
    }
138
139
    /**
140
     * {@inheritDoc}
141
     */
142
    public function setContent($content)/*# : bool */
143
    {
144
        if ($this->isFilesystemWritable()) {
145
            $res = $this->getDriver()->setContent($this->path, $content);
146
            $this->resetError();
147
            return $res;
148
        }
149
        return false;
150
    }
151
152
    /**
153
     * {@inheritDoc}
154
     */
155 View Code Duplication
    public function setMeta(array $meta)/*# : bool */
156
    {
157
        if (!$this->exists()) {
158
            return false;
159
        }
160
161
        if (!empty($meta)) {
162
            $res = $this->getDriver()->setMeta($this->path, $meta);
163
            $this->resetError();
164
            return $res;
165
        }
166
        return true;
167
    }
168
169
    /**
170
     * {@inheritDoc}
171
     */
172
    public function isDir()/*# : bool */
173
    {
174
        if ($this->exists()) {
175
            return $this->getDriver()->isDir($this->path);
176
        }
177
        return false;
178
    }
179
180
    /**
181
     * {@inheritDoc}
182
     */
183
    public function rename(/*# string */ $destination)/*# : bool */
184
    {
185
        return $this->alterAction($destination, 'rename');
186
    }
187
188
    /**
189
     * {@inheritDoc}
190
     */
191
    public function copy(/*# string */ $destination)/*# : bool */
192
    {
193
        return $this->alterAction($destination, 'copy');
194
    }
195
196
    /**
197
     * {@inheritDoc}
198
     */
199 View Code Duplication
    public function delete()/*# : bool */
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
200
    {
201
        if ($this->exists()) {
202
            if (!$this->isFilesystemDeletable()) {
203
                return false;
204
            }
205
206
            $res = $this->getDriver()->delete($this->path);
207
            $this->resetError();
208
            return $res;
209
        }
210
        return true;
211
    }
212
213
    /**
214
     * Reset error to driver's error
215
     *
216
     * @access protected
217
     */
218
    protected function resetError()
219
    {
220
        $this->copyError($this->getFilesystem()->getDriver());
221
    }
222
223
    /**
224
     * Get the driver
225
     *
226
     * @return DriverInterface
227
     * @access protected
228
     */
229
    protected function getDriver()/*# : DriverInterface */
230
    {
231
        return $this->getFilesystem()->getDriver();
232
    }
233
234
    /**
235
     * Do copy or rename
236
     *
237
     * @param  string $destination
238
     * @param  string $action 'copy' or 'rename'
239
     * @return bool
240
     * @access protected
241
     */
242
    protected function alterAction(
243
        /*# string */ $destination,
244
        /*# string */ $action
245
    )/*# : bool */ {
246
        if (!$this->exists() || !$this->isFilesystemWritable()) {
247
            return false;
248
        }
249
250
        // destination is direcotry
251
        if ($this->getDriver()->isDir($destination)) {
252
            $destination .= '/' . basename($this->path);
253
        }
254
255
        $res = $this->getDriver()->{$action}($this->path, $destination);
256
        $this->resetError();
257
        return (bool) $res;
258
    }
259
260
    /**
261
     * Check filesystem readable or not
262
     *
263
     * @return bool
264
     * @access protected
265
     */
266
    protected function isFilesystemReadable()/*# : bool */
267
    {
268
        if ($this->getFilesystem()->isReadable()) {
269
            return true;
270
        } else {
271
            return $this->setError(
272
                Message::get(Message::STR_FS_NONREADABLE, $this->full),
273
                Message::STR_FS_NONREADABLE
274
            );
275
        }
276
    }
277
278
    /**
279
     * Check filesystem writable or not
280
     *
281
     * @return bool
282
     * @access protected
283
     */
284
    protected function isFilesystemWritable()/*# : bool */
285
    {
286
        if ($this->getFilesystem()->isWritable()) {
287
            return true;
288
        } else {
289
            return $this->setError(
290
                Message::get(Message::STR_FS_NONWRITABLE, $this->full),
291
                Message::STR_FS_NONWRITABLE
292
            );
293
        }
294
    }
295
296
    /**
297
     * Check filesystem file deletable or not
298
     *
299
     * @return bool
300
     * @access protected
301
     */
302
    protected function isFilesystemDeletable()/*# : bool */
303
    {
304
        if ($this->getFilesystem()->isDeletable()) {
305
            return true;
306
        } else {
307
            return $this->setError(
308
                Message::get(Message::STR_FS_NONDELETABLE, $this->full),
309
                Message::STR_FS_NONDELETABLE
310
            );
311
        }
312
    }
313
}
314