Completed
Push — master ( b64fc1...5651bd )
by Raffael
16:20 queued 08:39
created

AddKindAttributeMap::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * tubee
7
 *
8
 * @copyright   Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Tubee\Migration;
13
14
use MongoDB\Database;
15
16
class AddKindAttributeMap 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
     * {@inheritdoc}
35
     */
36
    public function start(): bool
37
    {
38
        foreach ($this->db->workflows->find() as $workflow) {
39
            $attributes = $workflow['data']['map'];
40
            foreach ($attributes as $key => &$attribute) {
41
                $attribute = $this->convertToKind($attribute);
42
43
                while (isset($attribute['unwind']) && is_array($attribute['unwind'])) {
44
                    $attribute = &$attribute['unwind'];
45
                    $attribute = $this->convertToKind($attribute);
46
                }
47
            }
48
49
            $this->db->workflows->updateOne(['_id' => $workflow['_id']], [
50
                '$set' => ['data.map' => $attributes],
51
            ]);
52
        }
53
54
        return true;
55
    }
56
57
    /**
58
     * Covnert old style declartion to kind/value.
59
     */
60
    protected function convertToKind(array $attribute): array
61
    {
62
        if (isset($attribute['script'])) {
63
            $attribute['kind'] = 'script';
64
            $attribute['value'] = $attribute['script'];
65
        } elseif (isset($attribute['from'])) {
66
            $attribute['kind'] = 'map';
67
            $attribute['value'] = $attribute['from'];
68
        } elseif (isset($attribute['value'])) {
69
            $attribute['kind'] = 'static';
70
            $attribute['value'] = $attribute['value'];
71
        } else {
72
            $attribute['kind'] = 'static';
73
            $attribute['value'] = null;
74
        }
75
76
        unset($attribute['script'], $attribute['from']);
77
78
        return $attribute;
79
    }
80
}
81