Test Failed
Push — master ( 8c814b...380005 )
by Raffael
08:49
created

Preview::storePreview()   B

Complexity

Conditions 5
Paths 52

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 47
ccs 0
cts 0
cp 0
rs 8.8452
c 0
b 0
f 0
cc 5
crap 30
nc 52
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Balloon\App\Preview;
13
14
use Balloon\Filesystem\Node\File;
15
use MongoDB\BSON\ObjectId;
16
use MongoDB\Database;
17
use Psr\Log\LoggerInterface;
18
19
class Preview
20
{
21
    /**
22
     * Logger.
23
     *
24
     * @var LoggerInterface
25
     */
26
    protected $logger;
27
28
    /**
29
     * Database.
30
     *
31
     * @var Database
32
     */
33
    protected $db;
34
35
    /**
36
     * Constructor.
37
     */
38
    public function __construct(Database $db, LoggerInterface $logger)
39
    {
40
        $this->db = $db;
41
        $this->logger = $logger;
42
    }
43
44
    /**
45
     * Get preview.
46
     */
47
    public function getPreview(File $file): string
48
    {
49
        $preview = $file->getAppAttribute(__NAMESPACE__, 'preview');
50
        if ($preview instanceof ObjectId) {
0 ignored issues
show
Bug introduced by
The class MongoDB\BSON\ObjectId does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
51
            try {
52
                $stream = $this->db->selectGridFSBucket(['bucketName' => 'thumbnail'])
53
                    ->openDownloadStream($preview);
54
                $contents = stream_get_contents($stream);
55
                fclose($stream);
56
57
                return $contents;
58
            } catch (\Exception $e) {
59
                $this->logger->warning('failed download preview from gridfs for file ['.$file->getId().']', [
60
                    'category' => get_class($this),
61
                    'exception' => $e,
62
                ]);
63
            }
64
        }
65
66
        throw new Exception\PreviewNotFound('preview does not exists');
67
    }
68
69
    /**
70
     * Delete preview.
71
     */
72
    public function deletePreview(File $file): bool
73
    {
74
        $bucket = $this->db->selectGridFSBucket(['bucketName' => 'thumbnail']);
75
76
        try {
77
            $preview = $file->getAppAttribute(__NAMESPACE__, 'preview');
78
            if ($preview instanceof ObjectId) {
0 ignored issues
show
Bug introduced by
The class MongoDB\BSON\ObjectId does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
79
                $references = $this->db->{'thumbnail.files'}->count([
80
                    'apps' => [__NAMESPACE__ => ['preview' => $preview]],
81
                ]);
82
83
                if (1 === $references) {
84
                    $this->logger->debug('delete preview ['.$preview.'] from file ['.$file->getId().']', [
85
                        'category' => get_class($this),
86
                    ]);
87
88
                    $bucket->delete($preview);
89
                } else {
90
                    $this->logger->debug('do not remove preview blob ['.$preview.'] from file ['.$file->getId().'], there are still other references left', [
91
                        'category' => get_class($this),
92
                    ]);
93
                }
94
95
                return true;
96
            }
97
98
            return false;
99
        } catch (\Exception $e) {
100
            $this->logger->error('failed to remove preview from file ['.$file->getId().']', [
101
                'category' => get_class($this),
102
                'exception' => $e,
103
            ]);
104
105
            throw $e;
106
        }
107
    }
108
}
109