Completed
Push — master ( afaf42...8ac772 )
by Raffael
20:10 queued 16:21
created

CoreInstallation::start()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 60
ccs 0
cts 52
cp 0
rs 8.8727
c 0
b 0
f 0
cc 3
nc 4
nop 0
crap 12

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Balloon\Server;
15
use MongoDB\Database;
16
17
class CoreInstallation implements DeltaInterface
18
{
19
    /**
20
     * Database.
21
     *
22
     * @var Database
23
     */
24
    protected $db;
25
26
    /**
27
     * Server.
28
     *
29
     * @var Server
30
     */
31
    protected $server;
32
33
    /**
34
     * Construct.
35
     */
36
    public function __construct(Database $db, Server $server)
37
    {
38
        $this->db = $db;
39
        $this->server = $server;
40
    }
41
42
    /**
43
     * Initialize database.
44
     */
45
    public function start(): bool
46
    {
47
        $collections = [];
48
        foreach ($this->db->listCollections() as $collection) {
49
            $collections[] = $collection->getName();
50
        }
51
52
        $this->db->user->createIndex(['username' => 1], ['unique' => true]);
53
        $this->db->group->createIndex(['member' => 1]);
54
55
        $this->db->selectCollection('fs.files')->createIndex(['md5' => 1], [
56
            'unique' => true,
57
            'partialFilterExpression' => [
58
                'md5' => ['$exists' => true],
59
            ],
60
        ]);
61
62
        $this->db->selectCollection('fs.chunks')->createIndex(
63
            ['files_id' => 1, 'n' => 1],
64
            ['unique' => true]
65
        );
66
67
        $this->db->storage->createIndexes([
68
            ['key' => ['acl.id' => 1]],
69
            ['key' => ['hash' => 1]],
70
            ['key' => ['parent' => 1, 'owner' => 1]],
71
            ['key' => ['reference' => 1]],
72
            ['key' => ['shared' => 1]],
73
            ['key' => ['deleted' => 1]],
74
            ['key' => [
75
                'owner' => 1,
76
                'directory' => 1,
77
                'delete' => 1,
78
            ]],
79
        ]);
80
81
        $this->db->delta->createIndexes([
82
            ['key' => ['owner' => 1]],
83
            ['key' => ['timestamp' => 1]],
84
            ['key' => ['node' => 1]],
85
            ['key' => ['share' => 1]],
86
        ]);
87
88
        if (!in_array('queue', $collections, true)) {
89
            $this->db->createCollection(
90
                'queue',
91
                [
92
                'capped' => true,
93
                'size' => 100000, ]
94
            );
95
        }
96
97
        $this->server->addUser('admin', [
98
            'password' => 'admin',
99
            'mail' => '[email protected]',
100
            'admin' => true,
101
        ]);
102
103
        return true;
104
    }
105
}
106