Passed
Push — master ( 5a4455...a3a054 )
by Pol
02:30
created

File::seekToEnd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
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
class File extends Vfs implements FileInterface
10
{
11
    /**
12
     * File constructor.
13
     *
14
     * @param array $attributes
15
     */
16 11
    public function __construct(
17
        array $attributes = []
18
    ) {
19
        $attributes += [
20 11
            'content' => '',
21
            'position' => 0,
22
        ];
23
24 11
        parent::__construct($attributes, null);
25 11
    }
26
27
    /**
28
     * @return bool
29
     */
30 1
    public function atEof()
31
    {
32 1
        return $this->getPosition() >= \strlen($this->getAttribute('content'));
33
    }
34
35
    /**
36
     * @param string $id
37
     * @param string $content
38
     * @param array $attributes
39
     *
40
     * @throws \Exception
41
     *
42
     * @return \drupol\phpvfs\Node\File
43
     */
44 10
    public static function create(string $id, string $content = '', array $attributes = [])
45
    {
46 10
        $path = Path::fromString($id);
47
48 10
        $dirname = $path->dirname();
49
50 10
        $basedir = null;
51 10
        if (!empty($dirname)) {
52 9
            $basedir = Directory::create($dirname);
53
        }
54
55
        $attributes = [
56 10
            'id' => $path->basename(),
57 10
            'content' => $content,
58 10
        ] + $attributes;
59
60 10
        $file = new self($attributes);
61
62 10
        if (null !== $basedir) {
63 9
            $basedir->add($file);
64
        }
65
66 10
        return $file;
67
    }
68
69
    /**
70
     * @return int
71
     */
72 7
    public function getPosition(): int
73
    {
74 7
        return (int) $this->getAttribute('position');
75
    }
76
77
    /**
78
     * @param int $bytes
79
     *
80
     * @return string
81
     */
82 3
    public function read(int $bytes): string
83
    {
84 3
        $data = \substr(
85 3
            $this->getAttribute('content'),
86 3
            $this->getPosition(),
87 3
            $bytes
88
        );
89
90 3
        $this->offsetPosition($bytes);
91
92 3
        return $data;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 1
    public function seekToEnd()
99
    {
100 1
        return $this->setPosition(
101 1
            \strlen($this->getAttribute('content'))
102
        );
103
    }
104
105
    /**
106
     * @param int $position
107
     *
108
     * @return $this
109
     */
110 7
    public function setPosition(int $position): FileInterface
111
    {
112 7
        $this->setAttribute('position', $position);
113
114 7
        return $this;
115
    }
116
117
    /**
118
     * @return int
119
     */
120 7
    public function size(): int
121
    {
122 7
        return \strlen($this->getAttribute('content'));
123
    }
124
125
    /**
126
     * @param string $data
127
     *
128
     * @return int
129
     */
130 6
    public function write(string $data): int
131
    {
132 6
        $content = $this->getAttribute('content');
133 6
        $content = \substr($content, 0, $this->getPosition());
134
135 6
        $content .= $data;
136 6
        $this->setAttribute('content', $content);
137
138 6
        $written = \strlen($data);
139 6
        $this->offsetPosition($written);
140
141 6
        return $written;
142
    }
143
144
    /**
145
     * @param int $offset
146
     *
147
     * @return int
148
     */
149 7
    protected function offsetPosition($offset): int
150
    {
151 7
        $contentSize = $this->size();
152 7
        $newPosition = $this->getPosition() + $offset;
153
154 7
        $newPosition = $contentSize < $newPosition ?
155 3
            $contentSize :
156 7
            $newPosition;
157
158 7
        $this->setPosition($newPosition);
159
160 7
        return $newPosition;
161
    }
162
}
163