Completed
Push — master ( 7b9f4b...1cd743 )
by Andreas
13s queued 10s
created

ODM/MongoDB/Repository/DefaultGridFSRepository.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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