Completed
Pull Request — master (#141)
by Raffael
11:16
created

Md5BlobIgnoreNull::start()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 14
cp 0
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 12
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\Migration\Delta;
13
14
use MongoDB\Database;
15
use MongoDB\Driver\Exception\CommandException;
16
17
class Md5BlobIgnoreNull implements DeltaInterface
18
{
19
    /**
20
     * Database.
21
     *
22
     * @var Database
23
     */
24
    protected $db;
25
26
    /**
27
     * Construct.
28
     */
29
    public function __construct(Database $db)
30
    {
31
        $this->db = $db;
32
    }
33
34
    /**
35
     * Change unique md5 index to ignore null blobs.
36
     */
37
    public function start(): bool
38
    {
39
        try {
40
            $this->createIndex();
41
        } catch (CommandException $e) {
0 ignored issues
show
Bug introduced by
The class MongoDB\Driver\Exception\CommandException 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...
42
            if ($e->getCode() === 85) {
43
                $this->db->selectCollection('fs.files')->dropIndex('md5_1');
44
                $this->createIndex();
45
            } else {
46
                throw $e;
47
            }
48
        }
49
50
        return true;
51
    }
52
53
    /**
54
     * Create index.
55
     */
56
    protected function createIndex(): string
57
    {
58
        return $this->db->selectCollection('fs.files')->createIndex(['md5' => 1], [
59
            'unique' => true,
60
            'partialFilterExpression' => [
61
                'md5' => ['$exists' => true],
62
            ],
63
        ]);
64
    }
65
}
66