1 | <?php |
||
34 | class MongoFileService implements \Minotaur\Io\FileService |
||
35 | { |
||
36 | use MongoHelper; |
||
37 | |||
38 | /** |
||
39 | * @var \MongoDB\GridFS\Bucket |
||
40 | */ |
||
41 | private $bucket; |
||
42 | |||
43 | /** |
||
44 | * Creates a new MongoFileService |
||
45 | * |
||
46 | * @param $bucket - The GridFS Bucket |
||
47 | */ |
||
48 | 2 | public function __construct(Bucket $bucket) |
|
52 | |||
53 | /** |
||
54 | * Stores an uploaded file. |
||
55 | * |
||
56 | * You should specify `contentType` in the `metadata` Map. |
||
57 | * |
||
58 | * @param \Psr\Http\Message\UploadedFileInterface $file The uploaded file |
||
59 | * @param array<string,mixed> $metadata Any additional fields to persist. At the very least, try to supply `contentType`. |
||
60 | * @return ObjectID The document ID of the stored file |
||
61 | * @throws \Caridea\Dao\Exception\Unreachable If the connection fails |
||
62 | * @throws \Caridea\Dao\Exception\Violating If a constraint is violated |
||
63 | * @throws \Caridea\Dao\Exception\Generic If any other database problem occurs |
||
64 | */ |
||
65 | 1 | public function store(UploadedFileInterface $file, array $metadata): ObjectID |
|
77 | |||
78 | /** |
||
79 | * Gets the file as a PSR-7 Stream. |
||
80 | * |
||
81 | * @param $id - The document identifier, either a string or `ObjectID` |
||
82 | * @return \Psr\Http\Message\StreamInterface The readable stream |
||
83 | * @throws \Caridea\Dao\Exception\Unreachable If the connection fails |
||
84 | * @throws \Caridea\Dao\Exception\Unretrievable If the document doesn't exist |
||
85 | * @throws \Caridea\Dao\Exception\Generic If any other database problem occurs |
||
86 | */ |
||
87 | public function messageStream($id): StreamInterface |
||
95 | |||
96 | /** |
||
97 | * Gets a readable stream resource for the given ID. |
||
98 | * |
||
99 | * @param $id - The document identifier, either a string or `ObjectID` |
||
100 | * @return resource The readable stream |
||
101 | * @throws \Caridea\Dao\Exception\Unreachable If the connection fails |
||
102 | * @throws \Caridea\Dao\Exception\Unretrievable If the document doesn't exist |
||
103 | * @throws \Caridea\Dao\Exception\Generic If any other database problem occurs |
||
104 | */ |
||
105 | public function resource($id) |
||
109 | |||
110 | /** |
||
111 | * Efficiently writes the contents of a file to a Stream. |
||
112 | * |
||
113 | * @param \stdClass $file The file |
||
114 | * @param \Psr\Http\Message\StreamInterface $stream The stream |
||
115 | * @throws \Caridea\Dao\Exception\Unreachable If the connection fails |
||
116 | * @throws \Caridea\Dao\Exception\Violating If a constraint is violated |
||
117 | * @throws \Caridea\Dao\Exception\Generic If any other database problem occurs |
||
118 | */ |
||
119 | public function stream($file, StreamInterface $stream): void |
||
129 | |||
130 | /** |
||
131 | * Gets a stored file. |
||
132 | * |
||
133 | * @param mixed $id The document identifier, either a string or `ObjectID` |
||
134 | * @return \stdClass|null The stored file, or `null` |
||
135 | * @throws \Caridea\Dao\Exception\Unreachable If the connection fails |
||
136 | * @throws \Caridea\Dao\Exception\Unretrievable If the result cannot be retrieved |
||
137 | * @throws \Caridea\Dao\Exception\Generic If any other database problem occurs |
||
138 | */ |
||
139 | 1 | public function read($id): ?\stdClass |
|
146 | |||
147 | /** |
||
148 | * Deletes a stored file. |
||
149 | * |
||
150 | * @param mixed $id The document identifier, either a string or `ObjectID` |
||
151 | * @throws \Caridea\Dao\Exception\Unreachable If the connection fails |
||
152 | * @throws \Caridea\Dao\Exception\Unretrievable If the document doesn't exist |
||
153 | * @throws \Caridea\Dao\Exception\Generic If any other database problem occurs |
||
154 | */ |
||
155 | public function delete($id): void |
||
162 | |||
163 | /** |
||
164 | * Finds several files by some arbitrary criteria. |
||
165 | * |
||
166 | * @param array<string,mixed> $criteria Field to value pairs |
||
167 | * @return Traversable<\stdClass> The objects found |
||
168 | * @throws \Caridea\Dao\Exception\Unreachable If the connection fails |
||
169 | * @throws \Caridea\Dao\Exception\Unretrievable If the result cannot be retrieved |
||
170 | * @throws \Caridea\Dao\Exception\Generic If any other database problem occurs |
||
171 | */ |
||
172 | public function readAll(array $criteria): \Traversable |
||
182 | |||
183 | /** |
||
184 | * Executes something in the context of the collection. |
||
185 | * |
||
186 | * Exceptions are caught and translated. |
||
187 | * |
||
188 | * @param callable $cb The closure to execute, takes the Bucket. |
||
189 | * @return - Whatever the function returns, this method also returns |
||
190 | * @throws \Caridea\Dao\Exception If a database problem occurs |
||
191 | */ |
||
192 | 1 | protected function doExecute(callable $cb) |
|
200 | |||
201 | /** |
||
202 | * @return \MongoDB\GridFS\CollectionWrapper |
||
203 | */ |
||
204 | 1 | private function getCollectionWrapper(Bucket $b) |
|
210 | } |
||
211 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: