Completed
Push — master ( a15851...522585 )
by Harry
02:42
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 13
    public function __toString()
18
    {
19 13
        return $this->getPath();
20
    }
21
22
    /**
23
     * @return mixed
24
     */
25 11
    public function getDirectory()
26
    {
27 11
        return pathinfo($this->path, PATHINFO_DIRNAME) . '/';
28
    }
29
30
    /**
31
     * @return string
32
     */
33 18
    public function getFilename()
34
    {
35 18
        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 21
    public function getContents()
44
    {
45 21
        if ($this->exists()) {
46 19
            return explode("\n", trim($this->read()));
47
        } else {
48 2
            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 1
        }
63
64 4
        if (@$this->filesystem->copy($this->path, $newPath)) {
65 4
            return $this->getClone()->setPath($newPath);
66
        } else {
67
            $lastError = error_get_last();
68
            throw new CopyFailedException($this, $newPath, $lastError['message']);
69
        }
70
    }
71
72
    /**
73
     * Return a clone of this object
74
     *
75
     * @return static
76
     */
77 56
    public function getClone()
78
    {
79 56
        return clone $this;
80
    }
81
82
    /**
83
     * Clone sub objects
84
     */
85 56
    public function __clone()
86
    {
87 56
        if ($this->format) {
88 1
            $this->format = clone $this->format;
89 1
        }
90 56
    }
91
}
92