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

Md5BlobIgnoreNull   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 49
ccs 0
cts 27
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A start() 0 15 3
A createIndex() 0 9 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\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