Completed
Branch dev (276354)
by Raffael
15:43
created

FileToStorageAdapter::start()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 17
nc 3
nop 0
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 FileToStorageAdapter implements DeltaInterface
17
{
18
    /**
19
     * Database.
20
     *
21
     * @var Database
22
     */
23
    protected $db;
24
25
    /**
26
     * Construct.
27
     *
28
     * @param Database $db
29
     */
30
    public function __construct(Database $db)
31
    {
32
        $this->db = $db;
33
    }
34
35
    /**
36
     * Start.
37
     *
38
     * @return bool
39
     */
40
    public function start(): bool
41
    {
42
        $cursor = $this->db->storage->find([
43
            'directory' => false,
44
            'file' => ['$exists' => true],
45
            'storage' => ['$exists' => false],
46
        ]);
47
48
        foreach ($cursor as $object) {
49
            if (isset($object['file'])) {
50
                $file = ['_id' => $object['file']];
51
            } else {
52
                $file = null;
53
            }
54
55
            $this->db->storage->updateOne(
56
                ['_id' => $object['_id']],
57
                [
58
                    '$unset' => ['file' => 1],
59
                    '$set' => [
60
                        'storage_adapter' => 'gridfs',
61
                        'storage' => $file,
62
                    ],
63
                ]
64
            );
65
        }
66
67
        return true;
68
    }
69
}
70