1 | <?php |
||
23 | class Gridfs implements AdapterInterface |
||
24 | { |
||
25 | /** |
||
26 | * Database. |
||
27 | * |
||
28 | * @var Database |
||
29 | */ |
||
30 | protected $db; |
||
31 | |||
32 | /** |
||
33 | * GridFS. |
||
34 | * |
||
35 | * @var Bucket |
||
36 | */ |
||
37 | protected $gridfs; |
||
38 | |||
39 | /** |
||
40 | * Logger. |
||
41 | * |
||
42 | * @var LoggerInterface |
||
43 | */ |
||
44 | protected $logger; |
||
45 | |||
46 | /** |
||
47 | * GridFS storage. |
||
48 | * |
||
49 | * @param Database |
||
50 | * @param LoggerInterface $logger |
||
51 | */ |
||
52 | public function __construct(Database $db, LoggerInterface $logger) |
||
53 | { |
||
54 | $this->db = $db; |
||
55 | $this->gridfs = $db->selectGridFSBucket(); |
||
56 | $this->logger = $logger; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | public function hasNode(NodeInterface $node, array $attributes): bool |
||
63 | { |
||
64 | return null !== $this->getFileById($attributes); |
||
|
|||
65 | } |
||
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | public function deleteFile(File $file, array $attributes): bool |
||
71 | { |
||
72 | if (!isset($attributes['_id'])) { |
||
73 | throw new Exception('attributes do not contain a gridfs id'); |
||
74 | } |
||
75 | |||
76 | $exists = $this->getFileById($attributes['_id']); |
||
77 | |||
78 | if (null === $exists) { |
||
79 | $this->logger->debug('gridfs content node ['.$exists['_id'].'] was not found, file reference=['.$file->getId().']', [ |
||
80 | 'category' => get_class($this), |
||
81 | ]); |
||
82 | |||
83 | return false; |
||
84 | } |
||
85 | |||
86 | if (!isset($exists['metadata']['references'])) { |
||
87 | $this->gridfs->delete($exists['_id']); |
||
88 | |||
89 | return true; |
||
90 | } |
||
91 | |||
92 | $refs = $exists['metadata']['references']; |
||
93 | if (($key = array_search($file->getId(), $refs)) !== false) { |
||
94 | unset($refs[$key]); |
||
95 | $refs = array_values($refs); |
||
96 | } |
||
97 | |||
98 | if (count($refs) >= 1) { |
||
99 | $this->logger->debug('gridfs content node ['.$exists['_id'].'] still has references left, just remove the reference ['.$file->getId().']', [ |
||
100 | 'category' => get_class($this), |
||
101 | ]); |
||
102 | |||
103 | $this->db->{'fs.files'}->updateOne(['_id' => $exists['_id']], [ |
||
104 | '$set' => ['metadata.references' => $refs], |
||
105 | ]); |
||
106 | } else { |
||
107 | $this->logger->debug('gridfs content node ['.$exists['_id'].'] has no references left, delete node completely', [ |
||
108 | 'category' => get_class($this), |
||
109 | ]); |
||
110 | |||
111 | $this->gridfs->delete($exists['_id']); |
||
112 | } |
||
113 | |||
114 | return true; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | public function getFile(File $file, array $attributes) |
||
128 | |||
129 | /** |
||
130 | * {@inheritdoc} |
||
131 | */ |
||
132 | public function storeFile(File $file, $contents): array |
||
153 | |||
154 | /** |
||
155 | * Create collection. |
||
156 | * |
||
157 | * @return array |
||
158 | */ |
||
159 | public function createCollection(Collection $collection): array |
||
163 | |||
164 | /** |
||
165 | * Get stored file. |
||
166 | * |
||
167 | * @param ObjectId $id |
||
168 | * |
||
169 | * @return array |
||
170 | */ |
||
171 | protected function getFileById(ObjectId $id): ?array |
||
175 | |||
176 | /** |
||
177 | * Get stored file. |
||
178 | * |
||
179 | * @param string $hash |
||
180 | * |
||
181 | * @return array |
||
182 | */ |
||
183 | protected function getFileByHash(string $hash): ?array |
||
187 | |||
188 | /** |
||
189 | * Store new file. |
||
190 | * |
||
191 | * @param File $file |
||
192 | * @param resource $contents |
||
193 | * |
||
194 | * @return array |
||
195 | */ |
||
196 | protected function storeNew(File $file, $contents): array |
||
214 | } |
||
215 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: