Completed
Push — master ( afaf42...8ac772 )
by Raffael
20:10 queued 16:21
created

Blackhole::storeFile()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 0
cts 24
cp 0
rs 9.424
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 20
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2018 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Balloon\Filesystem\Storage\Adapter;
13
14
use Balloon\Filesystem\Node\Collection;
15
use Balloon\Filesystem\Node\File;
16
use Balloon\Filesystem\Node\NodeInterface;
17
use Balloon\Filesystem\Storage\Exception;
18
use Balloon\Server\User;
19
use MongoDB\BSON\ObjectId;
20
21
class Blackhole implements AdapterInterface
22
{
23
    /**
24
     * Storage.
25
     *
26
     * @var array
27
     */
28
    protected $streams = [];
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function hasNode(NodeInterface $node): bool
34
    {
35
        return true;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function deleteFile(File $file, ?int $version = null): ?array
42
    {
43
        return $file->getAttributes()['storage'];
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function readonly(NodeInterface $node, bool $readonly = true): ?array
50
    {
51
        return $node->getAttributes()['storage'];
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function forceDeleteFile(File $file, ?int $version = null): bool
58
    {
59
        return true;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function openReadStream(File $file)
66
    {
67
        return true;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function createCollection(Collection $parent, string $name): array
74
    {
75
        return [];
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function deleteCollection(Collection $collection): ?array
82
    {
83
        return $collection->getAttributes()['storage'];
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function move(NodeInterface $node, Collection $parent): ?array
90
    {
91
        $storage = $node->getAttributes()['storage'];
92
        $parent = $parent->getAttributes()['storage'];
93
        $storage['path'] = dirname($parent['path']).DIRECTORY_SEPARATOR.$node->getName();
0 ignored issues
show
Bug introduced by
Consider using $node->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
94
95
        return $storage;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function forceDeleteCollection(Collection $collection): bool
102
    {
103
        return true;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function undelete(NodeInterface $node): ?array
110
    {
111
        return $node->getAttributes()['storage'];
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function rename(NodeInterface $node, string $new_name): ?array
118
    {
119
        $storage = $node->getAttributes()['storage'];
120
        $storage['path'] = dirname($storage['path']).DIRECTORY_SEPARATOR.$new_name;
121
122
        return $storage;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function storeTemporaryFile($stream, User $user, ?ObjectId $session = null): ObjectId
129
    {
130
        $session = new ObjectId();
131
        $this->streams[(string) $session] = $stream;
132
133
        return $session;
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function storeFile(File $file, ObjectId $session): array
140
    {
141
        $hash = hash_init('md5');
142
143
        if (!isset($this->streams[(string) $session])) {
144
            throw new Exception\BlobNotFound('temporary blob '.$session.' has not been found');
145
        }
146
147
        $stream = $this->streams[(string) $session];
148
        $size = 0;
149
150
        while (!feof($stream)) {
151
            $buffer = fgets($stream, 65536);
152
153
            if ($buffer === false) {
154
                continue;
155
            }
156
157
            $size += mb_strlen($buffer, '8bit');
158
            hash_update($hash, $buffer);
159
        }
160
161
        unset($this->streams[(string) $session]);
162
        $md5 = hash_final($hash);
163
164
        return [
165
            'reference' => $file->getAttributes()['storage'],
166
            'size' => $size,
167
            'hash' => $md5,
168
        ];
169
    }
170
}
171