MountableTrait::getMountPoint()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
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\Traits;
16
17
use Phossa2\Storage\Message\Message;
18
use Phossa2\Storage\Exception\LogicException;
19
use Phossa2\Storage\Interfaces\MountableInterface;
20
use Phossa2\Storage\Interfaces\FilesystemInterface;
21
22
/**
23
 * MountableTrait
24
 *
25
 * Managing the mout points of the storage.
26
 *
27
 * @package Phossa2\Storage
28
 * @author  Hong Zhang <[email protected]>
29
 * @see     MountableInterface
30
 * @version 2.0.0
31
 * @since   2.0.0 added
32
 */
33
trait MountableTrait
34
{
35
    /**
36
     * filesystem map
37
     *
38
     * @var    FilesystemInterface[]
39
     * @access protected
40
     */
41
    protected $filesystems = [];
42
43
    /**
44
     * {@inheritDoc}
45
     */
46 View Code Duplication
    public function mount(
47
        /*# string */ $mountPoint,
48
        FilesystemInterface $filesystem
49
    )/*# : bool */ {
50
        // normalize mount point
51
        $mp = $this->cleanMountPoint($mountPoint);
52
53
        // mounted already
54
        if (isset($this->filesystems[$mp])) {
55
            throw new LogicException(
56
                Message::get(Message::STR_MOUNT_EXISTS, $mountPoint),
57
                Message::STR_MOUNT_EXISTS
58
            );
59
        }
60
61
        $this->filesystems[$mp] = $filesystem;
62
        return true;
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68 View Code Duplication
    public function umount(/*# string */ $mountPoint)/*# : bool */
69
    {
70
        // normalize mount point
71
        $mp = $this->cleanMountPoint($mountPoint);
72
73
        // not mounted
74
        if (!isset($this->filesystems[$mp])) {
75
            throw new LogicException(
76
                Message::get(Message::STR_MOUNT_NOT_EXISTS, $mountPoint),
77
                Message::STR_MOUNT_NOT_EXISTS
78
            );
79
        }
80
81
        // umount now
82
        unset($this->filesystems[$mp]);
83
84
        return true;
85
    }
86
87
    /**
88
     * Clean path to standard mount point
89
     *
90
     * @param  string $path
91
     * @return string
92
     * @access protected
93
     */
94
    protected function cleanMountPoint(/*# string */ $path)/*# : string */
95
    {
96
        return '/' . trim($path, " \t\r\n/");
97
    }
98
99
    /**
100
     * Get the filesystem at mount point
101
     *
102
     * @param  string $mountPoint
103
     * @return FilesystemInterface
104
     * @access protected
105
     */
106
    protected function getFilesystemAt(
107
        /*# string */ $mountPoint
108
    )/*# : FilesystemInterface */ {
109
        return $this->filesystems[$mountPoint];
110
    }
111
112
    /**
113
     * Find mount point of the path
114
     *
115
     * @param  string $path
116
     * @return string
117
     * @access protected
118
     */
119
    protected function getMountPoint(/*# string */ $path)/*# : string */
120
    {
121
        while ($path !== '') {
122
            if (isset($this->filesystems[$path])) {
123
                return $path;
124
            }
125
            $path = substr($path, 0, strrpos($path, '/'));
126
        }
127
        return '/';
128
    }
129
}
130