Completed
Pull Request — master (#145)
by Vitaly
10:03
created

Path::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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