Completed
Push — master ( 6351cc...76446a )
by Narcotic
23:40 queued 16:16
created

Version20161122111410::up()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 22
ccs 0
cts 18
cp 0
rs 8.9197
cc 4
eloc 13
nc 4
nop 1
crap 20
1
<?php
2
/**
3
 * Index migration
4
 */
5
6
namespace Graviton\I18nBundle\Migrations\MongoDB;
7
8
use AntiMattr\MongoDB\Migrations\AbstractMigration;
9
use Doctrine\MongoDB\Database;
10
use Graviton\I18nBundle\Document\Translatable;
11
use MongoDB;
12
use MongoCollection;
13
14
/**
15
 * Migrate domain_1_locale_1_original_1 index
16
 *
17
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
18
 * @license  http://opensource.org/licenses/MIT MIT License
19
 * @link     http://swisscom.ch
20
 */
21
class Version20161122111410 extends AbstractMigration
22
{
23
    /**
24
     * @var string
25
     */
26
    private $collection = 'Translatable';
27
28
    /**
29
     * @return string
30
     */
31
    public function getDescription()
32
    {
33
        return 'Build id for the new sha1 implementation';
34
    }
35
36
    /**
37
     * recreate index without unique flag
38
     *
39
     * @param Database $db database to migrate
40
     *
41
     * @return void
42
     */
43
    public function up(Database $db)
44
    {
45
        $collection = $db->createCollection($this->collection);
46
47
        // Remove index
48
        $collection->deleteIndex(['domain_1_locale_1_original_1']);
49
50
        /** @var array $translatable */
51
        foreach ($collection->find() as $translatable) {
52
            $id = $translatable['_id'];
53
            if (!ctype_xdigit($id)) {
54
                $newId = sha1($id);
55
                if ($collection->findOne(['_id' => $newId], ['id'])) {
56
                    $collection->remove(['_id' => $id]);
57
                } else {
58
                    $translatable['_id'] = $newId;
59
                    $collection->insert($translatable);
60
                    $collection->remove(['_id' => $id]);
61
                }
62
            }
63
        }
64
    }
65
66
    /**
67
     * re-add unique flag to index
68
     *
69
     * @param Database $db database to migrate
70
     *
71
     * @return void
72
     */
73
    public function down(Database $db)
74
    {
75
    }
76
}
77