Completed
Branch dev (276354)
by Raffael
15:43
created

Preview::deletePreview()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 21
nc 8
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2018 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
     * @param Database        $db
39
     * @param LoggerInterface $logger
40
     */
41
    public function __construct(Database $db, LoggerInterface $logger)
42
    {
43
        $this->db = $db;
44
        $this->logger = $logger;
45
    }
46
47
    /**
48
     * Get preview.
49
     *
50
     * @return string
51
     */
52
    public function getPreview(File $file): string
53
    {
54
        $preview = $file->getAppAttribute(__NAMESPACE__, 'preview');
55
        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...
56
            try {
57
                $stream = $this->db->selectGridFSBucket(['bucketName' => 'thumbnail'])
58
                    ->openDownloadStream($preview);
59
                $contents = stream_get_contents($stream);
60
                fclose($stream);
61
62
                return $contents;
63
            } catch (\Exception $e) {
64
                $this->logger->warning('failed download preview from gridfs for file ['.$file->getId().']', [
65
                    'category' => get_class($this),
66
                    'exception' => $e,
67
                ]);
68
            }
69
        }
70
71
        throw new Exception\PreviewNotFound('preview does not exists');
72
    }
73
74
    /**
75
     * Delete preview.
76
     *
77
     * @param File $file
78
     *
79
     * @return bool
80
     */
81
    public function deletePreview(File $file): bool
82
    {
83
        $bucket = $this->db->selectGridFSBucket(['bucketName' => 'thumbnail']);
84
85
        try {
86
            $preview = $file->getAppAttribute(__NAMESPACE__, 'preview');
87
            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...
88
                $references = $this->db->{'thumbnail.files'}->count([
89
                    'apps' => [__NAMESPACE__ => ['preview' => $preview]],
90
                ]);
91
92
                if (1 === $references) {
93
                    $this->logger->debug('delete preview ['.$preview.'] from file ['.$file->getId().']', [
94
                        'category' => get_class($this),
95
                    ]);
96
97
                    $bucket->delete($preview);
98
                } else {
99
                    $this->logger->debug('do not remove preview blob ['.$preview.'] from file ['.$file->getId().'], there are still other references left', [
100
                        'category' => get_class($this),
101
                    ]);
102
                }
103
104
                return true;
105
            }
106
107
            return false;
108
        } catch (\Exception $e) {
109
            $this->logger->error('failed to remove preview from file ['.$file->getId().']', [
110
                'category' => get_class($this),
111
                'exception' => $e,
112
            ]);
113
114
            throw $e;
115
        }
116
    }
117
}
118