Completed
Pull Request — master (#1790)
by Andreas
16:15
created

DefaultGridFSRepository::uploadFromFile()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 7
cts 8
cp 0.875
rs 9.7
c 0
b 0
f 0
cc 3
nc 5
nop 4
crap 3.0175
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, $metadata = null, ?int $chunkSizeBytes = null)
46
    {
47 2
        $options = $this->prepareOptions($metadata, $chunkSizeBytes);
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, $metadata = null, ?int $chunkSizeBytes = null)
56
    {
57 8
        $options = $this->prepareOptions($metadata, $chunkSizeBytes);
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, $metadata = null, ?int $chunkSizeBytes = 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, $metadata, $chunkSizeBytes);
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
    /**
89
     * @param object|null $metadata
90
     */
91 10
    private function prepareOptions($metadata = null, ?int $chunkSizeBytes = null): array
92
    {
93
        $options = [
94 10
            'chunkSizeBytes' => $chunkSizeBytes ?: $this->class->getChunkSizeBytes(),
95
        ];
96
97 10
        if ($metadata) {
98 3
            $options += ['metadata' => (object) $this->uow->getPersistenceBuilder()->prepareInsertData($metadata)];
99
        }
100
101 10
        return $options;
102
    }
103
}
104