Completed
Push — master ( 49a1ac...de016e )
by Kamil
05:40
created

Version20171003103916   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 89
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setContainer() 0 4 1
B up() 0 32 5
B down() 0 34 6
1
<?php
2
3
namespace Sylius\Migrations;
4
5
use Doctrine\DBAL\Migrations\AbstractMigration;
6
use Doctrine\DBAL\Schema\Schema;
7
use Sylius\Component\Attribute\AttributeType\SelectAttributeType;
8
use Sylius\Component\Product\Model\ProductAttributeInterface;
9
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
12
/**
13
 * Auto-generated Migration: Please modify to your needs!
14
 */
15
class Version20171003103916 extends AbstractMigration implements ContainerAwareInterface
16
{
17
    /**
18
     * @var ContainerInterface
19
     */
20
    private $container;
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function setContainer(?ContainerInterface $container = null): void
26
    {
27
        $this->container = $container;
28
    }
29
30
    /**
31
     * @param Schema $schema
32
     */
33
    public function up(Schema $schema): void
34
    {
35
        // this up() migration is auto-generated, please modify it to your needs
36
        $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');
37
38
        $defaultLocale = $this->container->getParameter('locale');
39
        $productAttributeRepository = $this->container->get('sylius.repository.product_attribute');
40
41
        $productAttributes = $productAttributeRepository->findBy(['type' => SelectAttributeType::TYPE]);
42
        /** @var ProductAttributeInterface $productAttribute */
43
        foreach ($productAttributes as $productAttribute) {
44
            $configuration = $productAttribute->getConfiguration();
45
            $upgradedConfiguration = [];
46
47
            foreach ($configuration as $configurationKey => $value) {
48
                if ('choices' === $configurationKey) {
49
                    foreach ($value as $key => $choice) {
50
                        $upgradedConfiguration[$configurationKey][$key][$defaultLocale] = $choice;
51
                    }
52
53
                    continue;
54
                }
55
56
                $upgradedConfiguration[$configurationKey] = $value;
57
            }
58
59
            $this->addSql('UPDATE sylius_product_attribute SET configuration = :configuration WHERE id = :id', [
60
                'id' => $productAttribute->getId(),
61
                'configuration' => serialize($upgradedConfiguration),
62
            ]);
63
        }
64
    }
65
66
    /**
67
     * @param Schema $schema
68
     */
69
    public function down(Schema $schema): void
70
    {
71
        // this down() migration is auto-generated, please modify it to your needs
72
        $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');
73
74
        $defaultLocale = $this->container->getParameter('locale');
75
        $productAttributeRepository = $this->container->get('sylius.repository.product_attribute');
76
77
        $productAttributes = $productAttributeRepository->findBy(['type' => SelectAttributeType::TYPE]);
78
        /** @var ProductAttributeInterface $productAttribute */
79
        foreach ($productAttributes as $productAttribute) {
80
            $configuration = $productAttribute->getConfiguration();
81
            $downgradedConfiguration = [];
82
83
            foreach ($configuration as $configurationKey => $value) {
84
                if ('choices' === $configurationKey) {
85
                    foreach ($value as $key => $choice) {
86
                        if (array_key_exists($defaultLocale, $choice)) {
87
                            $downgradedConfiguration[$configurationKey][$key] = $choice[$defaultLocale];
88
                        }
89
                    }
90
91
                    continue;
92
                }
93
94
                $downgradedConfiguration[$configurationKey] = $value;
95
            }
96
97
            $this->addSql('UPDATE sylius_product_attribute SET configuration = :configuration WHERE id = :id', [
98
                'id' => $productAttribute->getId(),
99
                'configuration' => serialize($downgradedConfiguration),
100
            ]);
101
        }
102
    }
103
}
104