Completed
Push — 1.4 ( a3fc6f...89c66f )
by Paweł
14s
created

Version20180924122522::up()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 8.0921
c 0
b 0
f 0
cc 7
nc 16
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace SWP\Migrations;
6
7
use Behat\Transliterator\Transliterator;
8
use Doctrine\DBAL\Schema\Schema;
9
use Doctrine\Migrations\AbstractMigration;
10
use Doctrine\ORM\Query\ResultSetMapping;
11
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
14
/**
15
 * Auto-generated Migration: Please modify to your needs!
16
 */
17
final class Version20180924122522 extends AbstractMigration implements ContainerAwareInterface
18
{
19
    /**
20
     * @var ContainerInterface
21
     */
22
    private $container;
23
24
    /**
25
     * @param ContainerInterface|null $container
26
     */
27
    public function setContainer(ContainerInterface $container = null)
28
    {
29
        $this->container = $container;
30
    }
31
32
    public function up(Schema $schema): void
33
    {
34
        $this->abortIf('postgresql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'postgresql\'.');
35
36
        $entityManager = $this->container->get('doctrine.orm.default_entity_manager');
37
38
        $rsm = new ResultSetMapping();
39
        $rsm->addScalarResult('id', 'id', 'integer');
40
        $rsm->addScalarResult('keywords', 'keywords', 'array');
41
42
        $query = $entityManager->createNativeQuery('SELECT id, keywords FROM swp_article', $rsm);
43
        $articles = $query->getResult();
44
45
        $dbConnection = $entityManager->getConnection();
46
        $nextvalQuery = $dbConnection->getDatabasePlatform()->getSequenceNextValSQL('swp_keyword_id_seq');
47
        $newId = (int) $dbConnection->fetchColumn($nextvalQuery);
48
49
        $keywords = [];
50
        foreach ($articles as $article) {
51
            foreach ($article['keywords'] as $articleKeyword) {
52
                if (!\array_key_exists($articleKeyword, $keywords)) {
53
                    $keywords[$articleKeyword] = [
54
                        'name' => $articleKeyword,
55
                        'id' => $newId,
56
                    ];
57
58
                    $this->addSql('INSERT INTO swp_keyword (id, slug, name) VALUES (:id, :slug, :name)', [
59
                        'id' => $newId,
60
                        'slug' => Transliterator::urlize($articleKeyword),
61
                        'name' => $articleKeyword,
62
                    ]);
63
                    ++$newId;
64
                }
65
            }
66
        }
67
68
        foreach ($articles as $article) {
69
            foreach ($article['keywords'] as $articleKeyword) {
70
                if (array_key_exists($articleKeyword, $keywords)) {
71
                    $this->addSql(
72
                        'INSERT INTO swp_article_keyword (article_id, keyword_id) VALUES (:article_id, :keyword_id)',
73
                        [
74
                            'article_id' => $article['id'],
75
                            'keyword_id' => $keywords[$articleKeyword]['id'],
76
                        ]
77
                    );
78
                    ++$newId;
79
                }
80
            }
81
        }
82
83
        $this->addSql('ALTER TABLE swp_article DROP keywords');
84
    }
85
86
    public function down(Schema $schema): void
87
    {
88
        $this->abortIf('postgresql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'postgresql\'.');
89
90
        $this->addSql('ALTER TABLE swp_article ADD keywords TEXT NOT NULL');
91
    }
92
}
93