Completed
Push — master ( 6695d4...9bc3b7 )
by Harry
04:41
created

FileNode::__toString()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Graze\DataFile\Node;
4
5
use Graze\DataFile\Format\FormatAwareInterface;
6
use Graze\DataFile\Format\FormatAwareTrait;
7
use Graze\DataFile\Modify\Exception\CopyFailedException;
8
use League\Flysystem\File;
9
10
class FileNode extends File implements FileNodeInterface, FormatAwareInterface
11
{
12
    use FormatAwareTrait;
13
14
    /**
15
     * @return string
16
     */
17 15
    public function __toString()
18
    {
19 15
        return $this->getPath();
20
    }
21
22
    /**
23
     * @return mixed
24
     */
25 10
    public function getDirectory()
26
    {
27 10
        return pathinfo($this->path, PATHINFO_DIRNAME) . '/';
28
    }
29
30
    /**
31
     * @return string
32
     */
33 17
    public function getFilename()
34
    {
35 17
        return pathinfo($this->path, PATHINFO_BASENAME);
36
    }
37
38
    /**
39
     * Returns the contents of the file as an array.
40
     *
41
     * @return array
42
     */
43 16
    public function getContents()
44
    {
45 16
        if ($this->exists()) {
46 16
            return explode("\n", trim($this->read()));
47
        } else {
48
            return [];
49
        }
50
    }
51
52
    /**
53
     * @param string|null $newPath
54
     *
55
     * @return static
56
     * @throws CopyFailedException When it is unable to copy the file
57
     */
58 4
    public function copy($newPath = null)
59
    {
60 4
        if (is_null($newPath)) {
61 1
            $newPath = $this->path . '-copy';
62
        }
63
64 4
        if (@$this->filesystem->copy($this->path, $newPath)) {
65 3
            return $this->getClone()->setPath($newPath);
66
        } else {
67 1
            $lastError = error_get_last();
68 1
            throw new CopyFailedException($this, $newPath, $lastError['message']);
69
        }
70
    }
71
72
    /**
73
     * Return a clone of this object
74
     *
75
     * @return static
76
     */
77 52
    public function getClone()
78
    {
79 52
        return clone $this;
80
    }
81
82
    /**
83
     * Clone sub objects
84
     */
85 52
    public function __clone()
86
    {
87 52
        if ($this->format) {
88 1
            $this->format = clone $this->format;
89
        }
90 52
    }
91
}
92