Completed
Push — master ( 97bad7...6351cc )
by Narcotic
241:05 queued 236:33
created

Version20161122111410   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 49
ccs 0
cts 21
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 4 1
A up() 0 15 3
A down() 0 3 1
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
        $qb = $collection->createQueryBuilder();
47
        /** @var Translatable $translatable */
48
        foreach ($qb->find() as $translatable) {
49
            $id = $translatable->getId();
50
            if (!ctype_xdigit($id)) {
51
                $collection->update(
52
                    ['id' => $id],
53
                    ['id' => sha1($id)]
54
                );
55
            }
56
        }
57
    }
58
59
    /**
60
     * re-add unique flag to index
61
     *
62
     * @param Database $db database to migrate
63
     *
64
     * @return void
65
     */
66
    public function down(Database $db)
67
    {
68
    }
69
}
70