Completed
Push — master ( 2aab0a...440703 )
by Raffael
19:06 queued 15:05
created

SetPointerId::start()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 21
cp 0
rs 9.52
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
16
class SetPointerId implements DeltaInterface
17
{
18
    /**
19
     * Database.
20
     *
21
     * @var Database
22
     */
23
    protected $db;
24
25
    /**
26
     * Construct.
27
     */
28
    public function __construct(Database $db)
29
    {
30
        $this->db = $db;
31
    }
32
33
    /**
34
     * Start.
35
     */
36
    public function start(): bool
37
    {
38
        $cursor = $this->db->storage->find([
39
            'pointer' => ['$exists' => false],
40
        ]);
41
42
        foreach ($cursor as $object) {
43
            if (isset($object['reference'])) {
44
                $pointer = $object['reference'];
45
            } else {
46
                $pointer = $object['_id'];
47
            }
48
49
            $this->db->storage->updateOne(
50
                ['_id' => $object['_id']],
51
                [
52
                   '$set' => ['pointer' => $pointer],
53
                ]
54
            );
55
        }
56
57
        $this->db->selectCollection('storage')->createIndex(['pointer' => 1]);
58
59
        return true;
60
    }
61
}
62