Completed
Push — master ( f8aac3...6e7b55 )
by Sebastian
07:15
created

Path::detectPathNotChanging()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 14
cts 14
cp 1
rs 8.439
c 0
b 0
f 0
cc 6
eloc 14
nc 8
nop 1
crap 6
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
     * List of not changing path elements.
57
     *
58
     * @var string[]
59
     */
60
    private $pathElementsNotChanging = [];
61
62
    /**
63
     * Time for replacing placeholders
64
     *
65
     * @var int
66
     */
67
    private $time;
68
69
    /**
70
     * Whether leading slash is needed or not
71
     *
72
     * @var bool
73
     */
74
    private $isAbsolute;
75 53
76
    /**
77 53
     * Path constructor.
78 53
     *
79
     * @param string    $path
80 53
     * @param int|null  $time
81 53
     */
82
    public function __construct(string $path, $time = null)
83
    {
84
        $this->isAbsolute = Util\Path::hasLeadingSlash($path);
85
        $this->time       = $time;
86
87
        $this->setupPath(Util\Path::withoutTrailingSlash($path));
88 53
    }
89
90 53
    /**
91 53
     * Setup path
92 53
     *
93
     * @param string $path
94
     */
95
    private function setupPath(string $path)
96 53
    {
97 11
        $this->path                    = $path;
98 11
        $this->pathRaw                 = $path;
99 11
        $this->pathNotChanging         = $path;
100
        $this->pathElements            = Util\Path::getDirectoryListFromAbsolutePath($path);
101 53
        $this->pathElementsNotChanging = $this->pathElements;
102
103
        // if path contains date placeholders determine the path that is not changing
104
        // and create final path by replacing all placeholders
105
        if (Util\Path::isContainingPlaceholder($this->pathRaw)) {
106
            $this->handleChangingPath();
107
        }
108
    }
109 11
110
    /**
111 11
     * Find path elements that can't change because of placeholder usage.
112 11
     *
113
     * @return void
114 11
     */
115 11
    private function handleChangingPath()
116
    {
117
        $this->pathIsChanging          = true;
118
        $this->pathElementsNotChanging = [];
119 11
        $foundChangingElement          = false;
120 11
121 11
        // collect path elements that do not change
122
        foreach ($this->pathElements as $depth => $dir) {
123
            // already found placeholder or found one right now
124 11
            // path isn't static anymore so don't add directory to path not changing
125 11
            if ($foundChangingElement || Util\Path::isContainingPlaceholder($dir)) {
126
                $foundChangingElement = true;
127
                continue;
128 11
            }
129 11
            $this->pathElementsNotChanging[] = $dir;
130 11
        }
131
        $pathNotChanging = implode(DIRECTORY_SEPARATOR, $this->pathElementsNotChanging);
132 11
        if ($this->isAbsolute) {
133
            $pathNotChanging = substr($pathNotChanging, 1);
134
        }
135
        $this->pathNotChanging = $pathNotChanging;
136
        $this->path            = Util\Path::replaceDatePlaceholders($this->pathRaw, $this->time);
137
    }
138
139
    /**
140
     * Return path element at given index.
141 6
     *
142
     * @param  int $index
143 6
     * @return string
144
     */
145
    public function getPathElementAtIndex(int $index): string
146
    {
147
        return $this->pathElements[$index];
148
    }
149
150
    /**
151 11
     * Return the full target path depth.
152
     *
153 11
     * @return int
154
     */
155
    public function getPathDepth(): int
156
    {
157
        return count($this->pathElements);
158
    }
159
160
    /**
161 26
     * Return depth of path that is not changing.
162
     *
163 26
     * @return int
164
     */
165
    public function getPathThatIsNotChangingDepth(): int
166
    {
167
        return count($this->pathElementsNotChanging);
168
    }
169
170
    /**
171 4
     * Return the path to the backup file.
172
     *
173 4
     * @return string
174
     */
175
    public function getPath(): string
176
    {
177
        return $this->path;
178
    }
179
180
    /**
181 3
     * Return the path to the backup file.
182
     *
183 3
     * @return string
184
     */
185
    public function getPathRaw(): string
186
    {
187
        return $this->pathRaw;
188
    }
189
190
    /**
191 13
     * Is dirname configured with any date placeholders.
192
     *
193 13
     * @return bool
194
     */
195
    public function hasChangingPath(): bool
196
    {
197
        return $this->pathIsChanging;
198
    }
199
200
    /**
201 2
     * Return the part of the path that is not changing.
202
     *
203 2
     * @return string
204
     */
205
    public function getPathThatIsNotChanging(): string
206
    {
207
        return $this->pathNotChanging;
208
    }
209
210
    /**
211
     * Return path when casted to string.
212
     *
213
     * @return string
214
     */
215
    public function __toString()
216
    {
217
        return $this->path;
218
    }
219
}
220