Completed
Push — master ( 3771a8...84c32b )
by Pol
12:36 queued 10:40
created

File::read()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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