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

Storage::put()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 3
eloc 11
nc 2
nop 3
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\Storage\Traits\PathAwareTrait;
20
use Phossa2\Shared\Error\ErrorAwareInterface;
21
use Phossa2\Storage\Interfaces\StorageInterface;
22
use Phossa2\Shared\Extension\ExtensionAwareTrait;
23
use Phossa2\Storage\Interfaces\PathAwareInterface;
24
use Phossa2\Shared\Extension\ExtensionAwareInterface;
25
26
/**
27
 * Storage
28
 *
29
 * @package Phossa2\Storage
30
 * @author  Hong Zhang <[email protected]>
31
 * @see     ObjectAbstract
32
 * @see     StorageInterface
33
 * @see     PathAwareInterface
34
 * @see     ErrorAwareInterface
35
 * @see     ExtensionAwareInterface
36
 * @version 2.0.0
37
 * @since   2.0.0 added
38
 */
39
class Storage extends ObjectAbstract implements StorageInterface, PathAwareInterface, ErrorAwareInterface, ExtensionAwareInterface
40
{
41
    use PathAwareTrait, ExtensionAwareTrait;
42
43
    /**
44
     * {@inheritDoc}
45
     */
46
    public function has(/*# string */ $path)/*# : bool */
47
    {
48
        // found
49
        if ($this->path($path)->exists()) {
50
            return true;
51
        }
52
53
        // not found
54
        return $this->setError(
55
            Message::get(Message::MSG_PATH_NOTFOUND, $path),
56
            Message::MSG_PATH_NOTFOUND
57
        );
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63
    public function get(/*# string */ $path, /*# bool */ $stream = false)
64
    {
65
        $obj = $this->path($path);
66
        $res = $obj->getContent($stream);
67
68
        // append mount point if directory
69
        if (is_array($res)) {
70
            return $this->prependMountPoint(
71
                $res, $this->getMountPoint($this->normalize($path))
72
            );
73
        }
74
75
        $this->copyError($obj);
76
77
        return $res;
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83
    public function put(
84
        /*# string */ $path,
85
        $content,
86
        array $meta = []
87
    )/*# : bool */ {
88
        $obj = $this->path($path);
89
90
        // write content
91
        if (null !== $content && !$obj->setContent($content)) {
92
            $this->copyError($obj);
93
            return false;
94
        }
95
96
        // set meta if any
97
        $res = $obj->setMeta($meta);
98
        $this->copyError($obj);
99
        return $res;
100
    }
101
102
    /**
103
     * {@inheritDoc}
104
     */
105
    public function meta(/*# string */ $path)/*# : array */
106
    {
107
        $obj = $this->path($path);
108
        $res = $obj->getMeta();
109
        $this->copyError($obj);
110
        return $res;
111
    }
112
113
    /**
114
     * {@inheritDoc}
115
     */
116
    public function copy(
117
        /*# string */ $from,
118
        /*# string */ $to
119
    )/*# : bool */ {
120
        // same filesystem copy
121
        if ($this->isSameFilesystem($from, $to)) {
122
            return $this->sameFilesystemAction($from, $to, 'copy');
123
        }
124
125
        $content = $this->get($from);
126
        if (is_null($content)) {
127
            return false;
128
        } elseif (is_array($content)) {
129
            return $this->copyDir($content, $to);
130
        } else {
131
            return $this->put($to, $content);
132
        }
133
    }
134
135
    /**
136
     * {@inheritDoc}
137
     */
138
    public function move(
139
        /*# string */ $from,
140
        /*# string */ $to
141
    )/*# : bool */ {
142
        // same filesystem move
143
        if ($this->isSameFilesystem($from, $to)) {
144
            return $this->sameFilesystemAction($from, $to, 'rename');
145
        }
146
147
        // diff filesystem move
148
        if ($this->copy($from, $to)) {
149
            return $this->delete($from);
150
        }
151
        return false;
152
    }
153
154
    /**
155
     * {@inheritDoc}
156
     */
157
    public function delete(/*# string */ $path)/*# : bool */
158
    {
159
        $obj = $this->path($path);
160
        $res = $obj->delete();
161
        $this->copyError($obj);
162
        return $res;
163
    }
164
165
    /**
166
     * Copy or rename
167
     *
168
     * @param  string $from
169
     * @param  string $to
170
     * @param  string $action 'copy' or 'rename'
171
     * @return bool
172
     * @access protected
173
     */
174
    protected function sameFilesystemAction(
175
        /*# string */ $from,
176
        /*# string */ $to,
177
        /*# string */ $action = 'copy'
178
    )/*# : bool */ {
179
        $obj = $this->path($from);
180
        $res = $obj->{$action}($this->path($to)->getPath());
181
        $this->copyError($obj);
182
        return $res;
183
    }
184
185
    /**
186
     * Exists on same filesystem ?
187
     *
188
     * @param  string $path1
189
     * @param  string $path2
190
     * @return bool
191
     * @access protected
192
     */
193
    protected function isSameFilesystem(
194
        /*# string */ $path1,
195
        /*# string */ $path2
196
    )/*# : bool */ {
197
        return $this->path($path1)->getFilesystem() === $this->path($path2)->getFilesystem();
198
    }
199
200
    /**
201
     * Copy an array of paths to destination
202
     *
203
     * @param  array $paths
204
     * @param  string $destination
205
     * @access protected
206
     */
207
    protected function copyDir(array $paths, /*# string */ $destination)
208
    {
209
        foreach ($paths as $path) {
210
            $base = basename($path);
211
            $dest = $this->mergePath($destination, $base);
212
            if (!$this->copy($path, $dest)) {
213
                return false;
214
            }
215
        }
216
        return true;
217
    }
218
219
    /**
220
     * Prepend mount point prefix to the array of paths
221
     *
222
     * @param  array $paths
223
     * @param  string $mountPoint
224
     * @return array
225
     * @access protected
226
     */
227
    protected function prependMountPoint(
228
        array $paths,
229
        /*# string */ $mountPoint
230
    )/*# : array */ {
231
        $res = [];
232
        foreach ($paths as $p) {
233
            $res[] = $this->mergePath($mountPoint, $p);
234
        }
235
        return $res;
236
    }
237
}
238