PathTrait::getPath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 1
eloc 1
c 1
b 1
f 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Comodojo\Zip\Traits;
4
5
use \Comodojo\Zip\Interfaces\ZipInterface;
6
use \Comodojo\Exception\ZipException;
7
8
/**
9
 * Path helper trait.
10
 *
11
 * @package     Comodojo Zip
12
 * @author      Marco Giovinazzi <[email protected]>
13
 * @license     MIT
14
 *
15
 * LICENSE:
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 */
25
26
trait PathTrait
27
{
28
29
    /**
30
     * Current base path
31
     *
32
     * @var string
33
     */
34
    private ?string $path = null;
35
36
    /**
37
     * Set current base path (to add relative files to zip archive)
38
     *
39
     * @param string|null $path
40
     *
41
     * @return ZipInterface
42
     * @throws ZipException
43
     */
44 1
    public function setPath(?string $path = null): ZipInterface
45
    {
46 1
        if ($path === null) {
47
            $this->path = null;
48 1
        } else if (!file_exists($path)) {
49
            throw new ZipException("Not existent path: $path");
50
        } else {
51 1
            $this->path = $path;
52
        }
53
54 1
        return $this;
55
    }
56
57
    /**
58
     * Get current base path
59
     *
60
     * @return string|null
61
     */
62 15
    public function getPath(): ?string
63
    {
64 15
        return $this->path;
65
    }
66
}
67