Completed
Push — master ( a958ab...194459 )
by Raffael
07:50 queued 03:52
created

HistoryToFileStorageAdapter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 48
ccs 0
cts 29
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A start() 0 27 3
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 HistoryToFileStorageAdapter 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->aggregate([
39
            ['$unwind' => '$history'],
40
            ['$match' => [
41
                'history.storage' => ['$exists' => 0],
42
            ]],
43
        ]);
44
45
        foreach ($cursor as $object) {
46
            $this->db->storage->updateOne(
47
                [
48
                    '_id' => $object['_id'],
49
                    'history.version' => $object['history']['version'],
50
                ],
51
                [
52
                    '$set' => [
53
                        'history.$.storage' => $object['history']['file'] === null ? null : ['_id' => $object['history']['file']],
54
                        'history.$.storage_adapter' => 'gridfs',
55
                    ],
56
                    '$unset' => ['history.$.file' => true]
57
                ]
58
            );
59
        }
60
61
        return true;
62
    }
63
}
64