Completed
Push — master ( 23d46a...ee003d )
by Pol
03:02
created

File::offsetPosition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace drupol\phpvfs\Node;
6
7
use drupol\phpvfs\Utils\Path;
8
9
/**
10
 * Class File.
11
 */
12
class File extends FilesystemNode implements FileInterface
13
{
14
    /**
15
     * File constructor.
16
     *
17
     * @param array $attributes
18
     */
19 13
    public function __construct(
20
        array $attributes = []
21
    ) {
22
        $attributes += [
23 13
            'content' => '',
24
            'position' => 0,
25
        ];
26
27 13
        parent::__construct($attributes, null);
28 13
    }
29
30
    /**
31
     * @param string $id
32
     * @param string $content
33
     * @param array $attributes
34
     *
35
     * @throws \Exception
36
     *
37
     * @return \drupol\phpvfs\Node\File
38
     */
39 12
    public static function create(string $id, string $content = '', array $attributes = [])
40
    {
41 12
        $path = Path::fromString($id);
42
43 12
        $dirname = $path->dirname();
44
45 12
        $basedir = null;
46 12
        if (!empty($dirname) && '.' !== $dirname) {
47 11
            $basedir = Directory::create($dirname);
48
        }
49
50
        $attributes = [
51 12
            'id' => $path->basename(),
52 12
            'content' => $content,
53 12
        ] + $attributes;
54
55 12
        $file = new self($attributes);
56
57 12
        if (null !== $basedir) {
58 11
            $basedir->add($file);
59
        }
60
61 12
        return $file;
62
    }
63
64
    /**
65
     * @return string
66
     */
67 3
    public function read(): string
68
    {
69 3
        return $this->getAttribute('content');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getAttribute('content') could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
70
    }
71
72
    /**
73
     * @param string $data
74
     *
75
     * @return \drupol\phpvfs\Node\FileInterface
76
     */
77 1
    public function write(string $data): FileInterface
78
    {
79 1
        $this->setAttribute('content', $data);
80
81 1
        return $this;
82
    }
83
}
84