Completed
Push — master ( 159bd4...9ef354 )
by Hong
02:36
created

Path::getFullPath()   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
28
/**
29
 * Path
30
 *
31
 * @package Phossa2\Storage
32
 * @author  Hong Zhang <[email protected]>
33
 * @see     ObjectAbstract
34
 * @see     PathInterface
35
 * @see     ErrorAwareInterface
36
 * @see     FilesystemAwareInterface
37
 * @version 2.0.0
38
 * @since   2.0.0 added
39
 */
40
class Path extends ObjectAbstract implements PathInterface, ErrorAwareInterface, FilesystemAwareInterface, ExtensionAwareInterface
41
{
42
    use FilesystemAwareTrait, ErrorAwareTrait, ExtensionAwareTrait;
43
44
    /**
45
     * Full path with mount point
46
     *
47
     * @var    string
48
     * @access protected
49
     */
50
    protected $full;
51
52
    /**
53
     * relative path without mount point prefix
54
     *
55
     * @var    string
56
     * @access protected
57
     */
58
    protected $path;
59
60
    /**
61
     * cached exists flag
62
     *
63
     * @var    bool
64
     * @access protected
65
     */
66
    protected $exists;
67
68
    /**
69
     * Instantiate the path object
70
     *
71
     * @param  string $full full path
72
     * @param  string $path relative path without mount point
73
     * @param  FilesystemInterface $filesystem
74
     * @access public
75
     * @api
76
     */
77
    public function __construct(
78
        /*# string */ $full,
79
        /*# string */ $path,
80
        FilesystemInterface $filesystem
81
    ) {
82
        $this->setFilesystem($filesystem);
83
        $this->full = $full;
84
        $this->path = $path;
85
    }
86
87
    /**
88
     * {@inheritDoc}
89
     */
90
    public function exists(/*# bool */ $check = false)/*# : bool */
91
    {
92
        if ($check || !is_bool($this->exists)) {
93
            $this->exists = $this->getFilesystem()->getDriver()->exists($this->path);
94
        }
95
96
        if (!$this->exists) {
97
            $this->setError(
98
                Message::get(Message::MSG_PATH_NOTFOUND, $this->full),
99
                Message::MSG_PATH_NOTFOUND
100
            );
101
        }
102
103
        return $this->exists;
104
    }
105
106
    /**
107
     * {@inheritDoc}
108
     */
109 View Code Duplication
    public function getContent(/*# bool */ $stream = false)
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...
110
    {
111
        // not exists or filesystem not readable
112
        if (!$this->exists() || !$this->isFilesystemReadable()) {
113
            return null;
114
        }
115
116
        $res = $this->getFilesystem()->getDriver()->getContent($this->path, $stream);
117
        $this->copyError($this->getFilesystem()->getDriver());
118
        return $res;
119
    }
120
121
    /**
122
     * {@inheritDoc}
123
     */
124
    public function getMeta()/*# : array */
125
    {
126
        if ($this->exists()) {
127
            $res = $this->getFilesystem()->getDriver()->getMeta($this->path);
128
            $this->copyError($this->getFilesystem()->getDriver());
129
            return $res;
130
        } else {
131
            return [];
132
        }
133
    }
134
135
    /**
136
     * {@inheritDoc}
137
     */
138
    public function getPath()/*# : string */
139
    {
140
        return $this->path;
141
    }
142
143
    /**
144
     * {@inheritDoc}
145
     */
146
    public function getFullPath()/*# : string */
147
    {
148
        return $this->full;
149
    }
150
151
    /**
152
     * {@inheritDoc}
153
     */
154 View Code Duplication
    public function setContent($content)/*# : 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...
155
    {
156
        if ($this->isFilesystemWritable()) {
157
            $res = $this->getFilesystem()->getDriver()
158
                ->setContent($this->path, $content);
159
            $this->copyError($this->getFilesystem()->getDriver());
160
            return $res;
161
        }
162
        return false;
163
    }
164
165
    /**
166
     * {@inheritDoc}
167
     */
168 View Code Duplication
    public function setMeta(array $meta)/*# : 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...
169
    {
170
        if (!$this->exists()) {
171
            return false;
172
        }
173
174
        if (!empty($meta)) {
175
            $res = $this->getFilesystem()->getDriver()->setMeta($this->path, $meta);
176
            $this->copyError($this->getFilesystem()->getDriver());
177
            return $res;
178
        }
179
        return true;
180
    }
181
182
    /**
183
     * {@inheritDoc}
184
     */
185
    public function rename(/*# string */ $destination)/*# : bool */
186
    {
187
        return $this->action($destination, 'rename');
188
    }
189
190
    /**
191
     * {@inheritDoc}
192
     */
193
    public function copy(/*# string */ $destination)/*# : bool */
194
    {
195
        return $this->action($destination, 'copy');
196
    }
197
198
    /**
199
     * {@inheritDoc}
200
     */
201
    public function delete()/*# : bool */
202
    {
203
        if ($this->exists()) {
204
            if (!$this->isFilesystemDeletable()) {
205
                return false;
206
            }
207
208
            $res = $this->getFilesystem()->getDriver()->delete($this->path);
209
            $this->copyError($this->getFilesystem()->getDriver());
210
            return $res;
211
        }
212
        return true;
213
    }
214
215
    /**
216
     * Do copy or rename
217
     *
218
     * @param  string $destination
219
     * @param  string $action 'copy' or 'rename'
220
     * @return bool
221
     * @access protected
222
     */
223 View Code Duplication
    protected function action(
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...
224
        /*# string */ $destination,
225
        /*# string */ $action
226
    )/*# : bool */ {
227
        if (!$this->exists() || !$this->isFilesystemWritable()) {
228
            return false;
229
        }
230
231
        $res = $this->getFilesystem()->getDriver()->{$action}($this->path, $destination);
232
        $this->copyError($this->getFilesystem()->getDriver());
233
        return (bool) $res;
234
    }
235
236
    /**
237
     * Check filesystem readable or not
238
     *
239
     * @return bool
240
     * @access protected
241
     */
242
    protected function isFilesystemReadable()/*# : bool */
243
    {
244
        if ($this->getFilesystem()->isReadable()) {
245
            return true;
246
        } else {
247
            return $this->setError(
248
                Message::get(Message::STR_FS_NONREADABLE, $this->full),
249
                Message::STR_FS_NONREADABLE
250
            );
251
        }
252
    }
253
254
    /**
255
     * Check filesystem writable or not
256
     *
257
     * @return bool
258
     * @access protected
259
     */
260
    protected function isFilesystemWritable()/*# : bool */
261
    {
262
        if ($this->getFilesystem()->isWritable()) {
263
            return true;
264
        } else {
265
            return $this->setError(
266
                Message::get(Message::STR_FS_NONWRITABLE, $this->full),
267
                Message::STR_FS_NONWRITABLE
268
            );
269
        }
270
    }
271
272
    /**
273
     * Check filesystem file deletable or not
274
     *
275
     * @return bool
276
     * @access protected
277
     */
278
    protected function isFilesystemDeletable()/*# : bool */
279
    {
280
        if ($this->getFilesystem()->isDeletable()) {
281
            return true;
282
        } else {
283
            return $this->setError(
284
                Message::get(Message::STR_FS_NONDELETABLE, $this->full),
285
                Message::STR_FS_NONDELETABLE
286
            );
287
        }
288
    }
289
}
290