Completed
Push — to-be-a-hat-or-not-to-be-kuhwa ( b72886...e84c48 )
by Kamil
19:11
created

Version20161102145826::down()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Sylius\Migrations;
4
5
use Doctrine\DBAL\Migrations\AbstractMigration;
6
use Doctrine\DBAL\Schema\Schema;
7
use Sylius\Component\Core\Model\TaxonInterface;
8
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
11
/**
12
 * Auto-generated Migration: Please modify to your needs!
13
 */
14
class Version20161102145826 extends AbstractMigration implements ContainerAwareInterface
15
{
16
    /**
17
     * @var ContainerInterface
18
     */
19
    private $container;
20
21
    /**
22
     * {@inheritDoc}
23
     */
24
    public function setContainer(ContainerInterface $container = null)
25
    {
26
        $this->container = $container;
27
    }
28
29
    /**
30
     * @param Schema $schema
31
     */
32
    public function postUp(Schema $schema)
33
    {
34
        $rootNodes = $this->container->get('sylius.repository.taxon')->findRootNodes();
35
        $this->updatePosition($rootNodes);
36
        $this->container->get('sylius.manager.taxon')->flush();
37
    }
38
39
    /**
40
     * @param Schema $schema
41
     */
42
    public function up(Schema $schema)
43
    {
44
        $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');
45
46
        $this->addSql('ALTER TABLE sylius_taxon ADD position INT NOT NULL');
47
    }
48
49
    /**
50
     * @param Schema $schema
51
     */
52
    public function down(Schema $schema)
53
    {
54
        $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');
55
56
        $this->addSql('ALTER TABLE sylius_taxon DROP position');
57
    }
58
59
    /**
60
     * @param mixed $rootNodes
61
     */
62
    private function updatePosition($rootNodes)
63
    {
64
        /** @var TaxonInterface $rootNode */
65
        foreach ($rootNodes as $key => $rootNode) {
66
            $rootNode->setPosition($key);
67
            if (!$rootNode->getChildren()->isEmpty()) {
68
                $this->updatePosition($rootNode->getChildren());
69
            }
70
        }
71
    }
72
}
73