Completed
Push — master ( 72cf29...ecec76 )
by Sebastian
03:13
created

Path::hasChangingPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace phpbu\App\Backup;
3
4
use phpbu\App\Util;
5
6
/**
7
 * Path class.
8
 *
9
 * @package    phpbu
10
 * @subpackage Backup
11
 * @author     Sebastian Feldmann <[email protected]>
12
 * @author     Vitaly Baev <[email protected]>
13
 * @copyright  Sebastian Feldmann <[email protected]>
14
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
15
 * @link       https://phpbu.de/
16
 * @since      Class available since Release 5.1.0
17
 */
18
class Path
19
{
20
    /**
21
     * Path without trailing slashes.
22
     *
23
     * @var string
24
     */
25
    private $path;
26
27
    /**
28
     * Raw path, that could contain placeholders.
29
     *
30
     * @var string
31
     */
32
    private $pathRaw;
33
34
    /**
35
     * Part of path, that is not changing because of placeholders.
36
     *
37
     * @var string
38
     */
39
    private $pathNotChanging;
40
41
    /**
42
     * Indicates if the path changes over time.
43
     *
44
     * @var bool
45
     */
46
    private $pathIsChanging = false;
47
48
    /**
49
     * List of all path elements.
50
     *
51
     * @var string[]
52
     */
53
    private $pathElements = [];
54
55
    /**
56
     * Time for replacing placeholders
57
     *
58
     * @var int
59
     */
60
    private $time;
61
62
    /**
63
     * Whether leading slash is needed or not
64
     *
65
     * @var bool
66
     */
67
    private $isAbsolute;
68
69
    /**
70
     * Path constructor.
71
     *
72
     * @param string    $path
73
     * @param int|null  $time
74
     */
75 53
    public function __construct(string $path, $time = null)
76
    {
77 53
        $this->isAbsolute = Util\Path::hasLeadingSlash($path);
78 53
        $this->time       = $time;
79
80 53
        $this->setupPath(Util\Path::withoutTrailingSlash($path));
81 53
    }
82
83
    /**
84
     * Setup path
85
     *
86
     * @param string $path
87
     */
88 53
    private function setupPath(string $path)
89
    {
90 53
        $this->path            = $path;
91 53
        $this->pathRaw         = $path;
92 53
        $this->pathNotChanging = $path;
93
94
        // if path contains date placeholders determine the path that is not changing
95
        // and create final path by replacing all placeholders
96 53
        if (Util\Path::isContainingPlaceholder($this->pathRaw)) {
97 11
            $this->pathIsChanging  = true;
98 11
            $this->pathNotChanging = $this->detectPathNotChanging($this->pathRaw);
99 11
            $this->path            = Util\Path::replaceDatePlaceholders($this->pathRaw, $this->time);
100
        }
101 53
    }
102
103
    /**
104
     * Find path elements that can't change because of placeholder usage.
105
     *
106
     * @param  string $path
107
     * @return string
108
     */
109 11
    private function detectPathNotChanging(string $path) : string
110
    {
111 11
        $partsNotChanging     = [];
112 11
        $foundChangingElement = false;
113
114 11
        foreach (Util\Path::getDirectoryListFromAbsolutePath($path) as $depth => $dir) {
115 11
            $this->pathElements[] = $dir;
116
117
            // already found placeholder or found one right now
118
            // path isn't static anymore so don't add directory to path not changing
119 11
            if ($foundChangingElement || Util\Path::isContainingPlaceholder($dir)) {
120 11
                $foundChangingElement = true;
121 11
                continue;
122
            }
123
            // do not add the / element, leading slash will be re-added later
124 11
            if ($dir !== '/') {
125 11
                $partsNotChanging[] = $dir;
126
            }
127
        }
128 11
        $pathNotChanging = implode(DIRECTORY_SEPARATOR, $partsNotChanging);
129 11
        if ($this->isAbsolute) {
130 11
            $pathNotChanging = DIRECTORY_SEPARATOR . $pathNotChanging;
131
        }
132 11
        return $pathNotChanging;
133
    }
134
135
    /**
136
     * Return path element at given index.
137
     *
138
     * @param  int $index
139
     * @return string
140
     */
141 6
    public function getPathElementAtIndex(int $index): string
142
    {
143 6
        return $this->pathElements[$index];
144
    }
145
146
    /**
147
     * Return the full target path depth.
148
     *
149
     * @return int
150
     */
151 11
    public function getPathDepth(): int
152
    {
153 11
        return count($this->pathElements);
154
    }
155
156
    /**
157
     * Return the path to the backup file.
158
     *
159
     * @return string
160
     */
161 26
    public function getPath(): string
162
    {
163 26
        return $this->path;
164
    }
165
166
    /**
167
     * Return the path to the backup file.
168
     *
169
     * @return string
170
     */
171 4
    public function getPathRaw(): string
172
    {
173 4
        return $this->pathRaw;
174
    }
175
176
    /**
177
     * Is dirname configured with any date placeholders.
178
     *
179
     * @return bool
180
     */
181 3
    public function hasChangingPath(): bool
182
    {
183 3
        return $this->pathIsChanging;
184
    }
185
186
    /**
187
     * Return the part of the path that is not changing.
188
     *
189
     * @return string
190
     */
191 13
    public function getPathThatIsNotChanging(): string
192
    {
193 13
        return $this->pathNotChanging;
194
    }
195
196
    /**
197
     * Return path when casted to string.
198
     *
199
     * @return string
200
     */
201 2
    public function __toString()
202
    {
203 2
        return $this->path;
204
    }
205
}
206