Completed
Pull Request — master (#1790)
by Andreas
14:35
created

DefaultGridFSRepository::prepareMetadataOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 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 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 1
    public function downloadToStream($id, $destination): void
19
    {
20
        try {
21 1
            $this->getDocumentBucket()->downloadToStream($this->class->getDatabaseIdentifierValue($id), $destination);
22
        } 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...
23
            throw DocumentNotFoundException::documentNotFound($this->getClassName(), $id);
24
        }
25 1
    }
26
27 1
    public function openUploadStream(string $filename, $metadata = null)
28
    {
29 1
        $options = $this->prepareMetadataOptions($metadata);
30
31 1
        return $this->getDocumentBucket()->openUploadStream($filename, $options);
32
    }
33
34 6
    public function uploadFromStream($filename, $source, $metadata = null)
35
    {
36 6
        $options = $this->prepareMetadataOptions($metadata);
37
38 6
        $id = $this->getDocumentBucket()->uploadFromStream($filename, $source, $options);
39
40
        // TODO: apply primary read preference
41
42 6
        return $this->find($id);
43
    }
44
45 3
    public function uploadFromFile(string $source, ?string $filename = null, $metadata = null)
46
    {
47 3
        $resource = fopen($source, 'r');
48 3
        if (! $resource) {
49
            throw MongoDBException::cannotReadGridFSSourceFile($source);
50
        }
51
52 3
        if (! $filename) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $filename of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
53 2
            $filename = pathinfo($source, PATHINFO_BASENAME);
54
        }
55
56
        try {
57 3
            return $this->uploadFromStream($filename, $resource, $metadata);
58
        } finally {
59 3
            fclose($resource);
60
        }
61
    }
62
63 7
    private function getDocumentBucket(): Bucket
64
    {
65 7
        return $this->dm->getDocumentBucket($this->documentName);
66
    }
67
68
    /**
69
     * @param object|null $metadata
70
     */
71 7
    private function prepareMetadataOptions($metadata = null): array
72
    {
73 7
        if ($metadata === null) {
74 5
            return [];
75
        }
76
77 2
        return ['metadata' => (object) $this->uow->getPersistenceBuilder()->prepareInsertData($metadata)];
78
    }
79
}
80