Completed
Push — master ( d63719...95bf49 )
by Hong
02:43
created

Path::setContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 10
loc 10
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
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 with no leading '/' and 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 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...
147
    {
148
        if ($this->isFilesystemWritable()) {
149
            $res = $this->getFilesystem()->getDriver()
150
                ->setContent($this->path, $content);
151
            $this->copyError($this->getFilesystem()->getDriver());
152
            return $res;
153
        }
154
        return false;
155
    }
156
157
    /**
158
     * {@inheritDoc}
159
     */
160
    public function setMeta(array $meta)/*# : bool */
161
    {
162
        if (!$this->exists(true)) {
163
            return false;
164
        }
165
166
        if (!empty($meta)) {
167
            $res = $this->getFilesystem()->getDriver()->setMeta($this->path, $meta);
168
            $this->copyError($this->getFilesystem()->getDriver());
169
            return $res;
170
        }
171
        return true;
172
    }
173
174
    /**
175
     * {@inheritDoc}
176
     */
177
    public function rename(/*# string */ $destination)/*# : bool */
178
    {
179
        return $this->action($destination, 'rename');
180
    }
181
182
    /**
183
     * {@inheritDoc}
184
     */
185
    public function copy(/*# string */ $destination)/*# : bool */
186
    {
187
        return $this->action($destination, 'copy');
188
    }
189
190
    /**
191
     * {@inheritDoc}
192
     */
193
    public function delete()/*# : bool */
194
    {
195
        if ($this->exists()) {
196
            if (!$this->isFilesystemDeletable()) {
197
                return false;
198
            }
199
200
            $res = $this->getFilesystem()->getDriver()->delete($this->path);
201
            $this->copyError($this->getFilesystem()->getDriver());
202
            return $res;
203
        }
204
        return true;
205
    }
206
207
    /**
208
     * Do copy or rename
209
     *
210
     * @param  string $destination
211
     * @param  string $action 'copy' or 'rename'
212
     * @return bool
213
     * @access protected
214
     */
215 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...
216
        /*# string */ $destination,
217
        /*# string */ $action
218
    )/*# : bool */ {
219
        if (!$this->exists() || !$this->isFilesystemWritable()) {
220
            return false;
221
        }
222
223
        $res = $this->getFilesystem()->getDriver()->{$action}($this->path, $destination);
224
        $this->copyError($this->getFilesystem()->getDriver());
225
        return (bool) $res;
226
    }
227
228
    /**
229
     * Check filesystem readable or not
230
     *
231
     * @return bool
232
     * @access protected
233
     */
234
    protected function isFilesystemReadable()/*# : bool */
235
    {
236
        if ($this->getFilesystem()->isReadable()) {
237
            return true;
238
        } else {
239
            return $this->setError(
240
                Message::get(Message::STR_FS_NONREADABLE, $this->full),
241
                Message::STR_FS_NONREADABLE
242
            );
243
        }
244
    }
245
246
    /**
247
     * Check filesystem writable or not
248
     *
249
     * @return bool
250
     * @access protected
251
     */
252
    protected function isFilesystemWritable()/*# : bool */
253
    {
254
        if ($this->getFilesystem()->isWritable()) {
255
            return true;
256
        } else {
257
            return $this->setError(
258
                Message::get(Message::STR_FS_NONWRITABLE, $this->full),
259
                Message::STR_FS_NONWRITABLE
260
            );
261
        }
262
    }
263
264
    /**
265
     * Check filesystem file deletable or not
266
     *
267
     * @return bool
268
     * @access protected
269
     */
270
    protected function isFilesystemDeletable()/*# : bool */
271
    {
272
        if ($this->getFilesystem()->isDeletable()) {
273
            return true;
274
        } else {
275
            return $this->setError(
276
                Message::get(Message::STR_FS_NONDELETABLE, $this->full),
277
                Message::STR_FS_NONDELETABLE
278
            );
279
        }
280
    }
281
}
282