ReleaseDataFixtureMigration   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 90
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A up() 0 11 2
B processFixtures() 0 13 6
A getInsertFixtureSql() 0 11 1
A expr() 0 8 2
1
<?php
2
3
namespace RDV\Bundle\MigrationBundle\Migration;
4
5
use Doctrine\DBAL\Schema\Schema;
6
use Doctrine\ORM\Query\Expr;
7
8
class ReleaseDataFixtureMigration implements Migration
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $fixturesData;
14
15
    /**
16
     * @var array
17
     */
18
    protected $mappingData;
19
20
    /**
21
     * @var Expr
22
     */
23
    protected $expr;
24
25
    /**
26
     * @param array $fixturesData
27
     * @param array $mappingData
28
     */
29
    public function __construct(array $fixturesData, array $mappingData)
30
    {
31
        $this->fixturesData = $fixturesData;
32
        $this->mappingData = $mappingData;
33
    }
34
35
    /**
36
     * {@inheritDoc}
37
     */
38
    public function up(Schema $schema, QueryBag $queries)
39
    {
40
        foreach ($this->fixturesData as $fixtureData) {
41
            $bundleName = $fixtureData['bundle_name'];
42
            $dataVersion = $fixtureData['data_version'];
43
            $demoDataVersion = $fixtureData['demo_data_version'];
44
45
            $this->processFixtures($queries, $bundleName, 'main', $dataVersion);
46
            $this->processFixtures($queries, $bundleName, 'demo', $demoDataVersion);
47
        }
48
    }
49
50
    /**
51
     * @param QueryBag $queries
52
     * @param string $bundle
53
     * @param string $type
54
     * @param string $version
55
     */
56
    protected function processFixtures(QueryBag $queries, $bundle, $type, $version)
57
    {
58
        if ($version && !empty($this->mappingData[$bundle][$type])) {
59
            $fixturesByVersions = $this->mappingData[$bundle][$type];
60
            foreach ($fixturesByVersions as $fixtureVersion => $fixtures) {
61
                if (version_compare($fixtureVersion, $version, '<=')) {
62
                    foreach ($fixtures as $fixture) {
63
                        $queries->addPostQuery($this->getInsertFixtureSql($fixture));
64
                    }
65
                }
66
            }
67
        }
68
    }
69
70
    /**
71
     * @param string $dataFixture
72
     * @return string
73
     */
74
    protected function getInsertFixtureSql($dataFixture)
75
    {
76
        $createdAt = new \DateTime('now', new \DateTimeZone('UTC'));
77
78
        return sprintf(
79
            'INSERT INTO %s SET %s, %s',
80
            Tables::MIGRATION_DATA_TABLE,
81
            $this->expr()->eq('class_name', $this->expr()->literal(addslashes($dataFixture))),
82
            $this->expr()->eq('loaded_at', $this->expr()->literal($createdAt->format('Y-m-d H:i:s')))
83
        );
84
    }
85
86
    /**
87
     * @return Expr
88
     */
89
    protected function expr()
90
    {
91
        if (!$this->expr) {
92
            $this->expr = new Expr();
93
        }
94
95
        return $this->expr;
96
    }
97
}
98