Completed
Push — master ( 19732d...076741 )
by Paweł
11:47
created

Version20180223095718::down()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24

Duplication

Lines 24
Ratio 100 %

Importance

Changes 0
Metric Value
dl 24
loc 24
rs 9.536
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SWP\Migrations;
6
7
use Behat\Transliterator\Transliterator;
8
use Doctrine\DBAL\Migrations\AbstractMigration;
9
use Doctrine\DBAL\Schema\Schema;
10
use SWP\Bundle\ContentBundle\Model\RouteInterface;
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
class Version20180223095718 extends AbstractMigration implements ContainerAwareInterface
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\DBAL\Migrations\AbstractMigration has been deprecated with message: Please use Doctrine\Migrations\AbstractMigration

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
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 View Code Duplication
    public function up(Schema $schema)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
    {
34
        // this up() migration is auto-generated, please modify it to your needs
35
        $this->abortIf('postgresql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'postgresql\'.');
36
37
        $entityManager = $this->container->get('doctrine.orm.default_entity_manager');
38
        $query = $entityManager
39
            ->createQuery('SELECT r.id, r.name FROM SWP\Bundle\CoreBundle\Model\Route r  WHERE r.slug IS NULL');
40
        $routes = $query->getArrayResult();
41
42
        foreach ($routes as $route) {
43
            $qb = $entityManager->createQueryBuilder();
44
            $query = $qb->update(RouteInterface::class, 'r')
45
                ->set('r.slug', '?1')
46
                ->where('r.id = ?2')
47
                ->setParameter(1, Transliterator::transliterate($route['name']))
48
                ->setParameter(2, $route['id'])
49
                ->getQuery();
50
51
            $query->execute();
52
        }
53
    }
54
55 View Code Duplication
    public function down(Schema $schema)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        // this down() migration is auto-generated, please modify it to your needs
58
        $this->abortIf('postgresql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'postgresql\'.');
59
60
        $entityManager = $this->container->get('doctrine.orm.default_entity_manager');
61
        $query = $entityManager
62
            ->createQuery('SELECT r.id, r.name, r.slug FROM SWP\Bundle\CoreBundle\Model\Route r');
63
        $routes = $query->getArrayResult();
64
65
        foreach ($routes as $route) {
66
            if ($route['slug'] === Transliterator::transliterate($route['name'])) {
67
                $qb = $entityManager->createQueryBuilder();
68
                $query = $qb->update(RouteInterface::class, 'r')
69
                    ->set('r.slug', '?1')
70
                    ->where('r.id = ?2')
71
                    ->setParameter(1, null)
72
                    ->setParameter(2, $route['id'])
73
                    ->getQuery();
74
75
                $query->execute();
76
            }
77
        }
78
    }
79
}
80