AbstractAdapter::removePathPrefix()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace League\Flysystem\Adapter;
4
5
use League\Flysystem\AdapterInterface;
6
7
abstract class AbstractAdapter implements AdapterInterface
8
{
9
    /**
10
     * @var string path prefix
11
     */
12
    protected $pathPrefix;
13
14
    /**
15
     * @var string
16
     */
17
    protected $pathSeparator = '/';
18
19
    /**
20
     * Set the path prefix.
21
     *
22
     * @param string $prefix
23
     *
24
     * @return self
25
     */
26 159
    public function setPathPrefix($prefix)
27
    {
28 159
        $is_empty = empty($prefix);
29
30 159
        if ( ! $is_empty) {
31 159
            $prefix = rtrim($prefix, $this->pathSeparator) . $this->pathSeparator;
32 159
        }
33
34 159
        $this->pathPrefix = $is_empty ? null : $prefix;
35 159
    }
36
37
    /**
38
     * Get the path prefix.
39
     *
40
     * @return string path prefix
41
     */
42 150
    public function getPathPrefix()
43
    {
44 150
        return $this->pathPrefix;
45
    }
46
47
    /**
48
     * Prefix a path.
49
     *
50
     * @param string $path
51
     *
52
     * @return string prefixed path
53
     */
54 144
    public function applyPathPrefix($path)
55
    {
56 144
        $path = ltrim($path, '\\/');
57
58 144
        if (strlen($path) === 0) {
59 12
            return $this->getPathPrefix() ?: '';
60
        }
61
62 135
        if ($prefix = $this->getPathPrefix()) {
63 132
            $path = $prefix . $path;
64 132
        }
65
66 135
        return $path;
67
    }
68
69
    /**
70
     * Remove a path prefix.
71
     *
72
     * @param string $path
73
     *
74
     * @return string path without the prefix
75
     */
76 60
    public function removePathPrefix($path)
77
    {
78 60
        $pathPrefix = $this->getPathPrefix();
79
80 60
        if ($pathPrefix === null) {
81 3
            return $path;
82
        }
83
84 57
        return substr($path, strlen($pathPrefix));
85
    }
86
}
87