Completed
Pull Request — master (#1790)
by Andreas
23:59
created

DefaultGridFSRepository::downloadToStream()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ODM\MongoDB\Repository;
6
7
use Doctrine\ODM\MongoDB\DocumentNotFoundException;
8
use MongoDB\GridFS\Bucket;
9
use MongoDB\GridFS\Exception\FileNotFoundException;
10
11
class DefaultGridFSRepository extends DocumentRepository implements GridFSRepository
12
{
13
    public function downloadToStream($id, $destination): void
14
    {
15
        try {
16
            $this->getDocumentBucket()->downloadToStream($this->class->getDatabaseIdentifierValue($id), $destination);
17
        } catch (FileNotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class MongoDB\GridFS\Exception\FileNotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
18
            throw DocumentNotFoundException::documentNotFound($this->getClassName(), $id);
19
        }
20
    }
21
22
    public function openUploadStream(string $filename, $metadata = null)
23
    {
24
        $options = $this->prepareMetadataOptions($metadata);
25
26
        return $this->getDocumentBucket()->openUploadStream($filename, $options);
27
    }
28
29
    public function uploadFromStream($filename, $source, $metadata = null)
30
    {
31
        $options = $this->prepareMetadataOptions($metadata);
32
33
        $id = $this->getDocumentBucket()->uploadFromStream($filename, $source, $options);
34
35
        // TODO: apply primary read preference
36
37
        return $this->find($id);
38
    }
39
40
    private function getDocumentBucket(): Bucket
41
    {
42
        return $this->dm->getDocumentBucket($this->documentName);
43
    }
44
45
    /**
46
     * @param object|null $metadata
47
     */
48
    private function prepareMetadataOptions($metadata = null): array
49
    {
50
        if ($metadata === null) {
51
            return [];
52
        }
53
54
        return ['metadata' => (object) $this->uow->getPersistenceBuilder()->prepareInsertData($metadata)];
55
    }
56
}
57