Completed
Pull Request — master (#1790)
by Andreas
17:41
created

DefaultGridFSRepository::getDocumentBucket()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ODM\MongoDB\Repository;
6
7
use Doctrine\ODM\MongoDB\DocumentNotFoundException;
8
use Doctrine\ODM\MongoDB\MongoDBException;
9
use MongoDB\GridFS\Bucket;
10
use MongoDB\GridFS\Exception\FileNotFoundException;
11
use const PATHINFO_BASENAME;
12
use function fclose;
13
use function fopen;
14
use function pathinfo;
15
16
class DefaultGridFSRepository extends DocumentRepository implements GridFSRepository
17
{
18
    /**
19
     * @see Bucket::openDownloadStream()
20
     */
21 1
    public function openDownloadStream($id)
22
    {
23
        try {
24 1
            return $this->getDocumentBucket()->openDownloadStream($this->class->getDatabaseIdentifierValue($id));
25
        } 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...
26
            throw DocumentNotFoundException::documentNotFound($this->getClassName(), $id);
27
        }
28
    }
29
30
    /**
31
     * @see Bucket::downloadToStream
32
     */
33 2
    public function downloadToStream($id, $destination): void
34
    {
35
        try {
36 2
            $this->getDocumentBucket()->downloadToStream($this->class->getDatabaseIdentifierValue($id), $destination);
37
        } 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...
38
            throw DocumentNotFoundException::documentNotFound($this->getClassName(), $id);
39
        }
40 2
    }
41
42
    /**
43
     * @see Bucket::openUploadStream
44
     */
45 2
    public function openUploadStream(string $filename, ?UploadOptions $uploadOptions = null)
46
    {
47 2
        $options = $this->prepareOptions($uploadOptions);
48
49 2
        return $this->getDocumentBucket()->openUploadStream($filename, $options);
50
    }
51
52
    /**
53
     * @see Bucket::uploadFromStream
54
     */
55 8
    public function uploadFromStream(string $filename, $source, ?UploadOptions $uploadOptions = null)
56
    {
57 8
        $options = $this->prepareOptions($uploadOptions);
58
59 8
        $databaseIdentifier = $this->getDocumentBucket()->uploadFromStream($filename, $source, $options);
60 8
        $documentIdentifier = $this->class->getPHPIdentifierValue($databaseIdentifier);
61
62 8
        return $this->dm->getReference($this->getClassName(), $documentIdentifier);
63
    }
64
65 4
    public function uploadFromFile(string $source, ?string $filename = null, ?UploadOptions $uploadOptions = null)
66
    {
67 4
        $resource = fopen($source, 'r');
68 4
        if ($resource === false) {
69
            throw MongoDBException::cannotReadGridFSSourceFile($source);
70
        }
71
72 4
        if ($filename === null) {
73 3
            $filename = pathinfo($source, PATHINFO_BASENAME);
74
        }
75
76
        try {
77 4
            return $this->uploadFromStream($filename, $resource, $uploadOptions);
78
        } finally {
79 4
            fclose($resource);
80
        }
81
    }
82
83 10
    private function getDocumentBucket(): Bucket
84
    {
85 10
        return $this->dm->getDocumentBucket($this->documentName);
86
    }
87
88 10
    private function prepareOptions(?UploadOptions $uploadOptions = null): array
89
    {
90 10
        if ($uploadOptions === null) {
91 5
            $uploadOptions = new UploadOptions();
92
        }
93
94
        $options = [
95 10
            'chunkSizeBytes' => $uploadOptions->chunkSizeBytes ?: $this->class->getChunkSizeBytes(),
96
        ];
97
98 10
        if (is_object($uploadOptions->metadata)) {
99 3
            $options += ['metadata' => (object) $this->uow->getPersistenceBuilder()->prepareInsertData($uploadOptions->metadata)];
100
        }
101
102 10
        return $options;
103
    }
104
}
105