Completed
Push — master ( 37faaa...541bbf )
by Raffael
10:18 queued 06:30
created

CreateUniqueUserMailIndexAllowNull::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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