Version20140408114519_EditListStudios::down()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 5
Ratio 29.41 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 5
loc 17
rs 9.4285
cc 3
eloc 8
nc 4
nop 1
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
8
 */
9
10
namespace AnimeDb\Bundle\CatalogBundle\DoctrineMigrations;
11
12
use Doctrine\DBAL\Migrations\AbstractMigration;
13
use Doctrine\ORM\EntityManagerInterface;
14
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
use Doctrine\DBAL\Schema\Schema;
17
use AnimeDb\Bundle\CatalogBundle\Entity\Studio;
18
19
/**
20
 * Auto-generated Migration: Please modify to your needs!
21
 */
22
class Version20140408114519_EditListStudios extends AbstractMigration implements ContainerAwareInterface
23
{
24
    /**
25
     * Entity manager.
26
     *
27
     * @var EntityManagerInterface
28
     */
29
    protected $em;
30
31
    /**
32
     * Add studios.
33
     *
34
     * @var array
35
     */
36
    protected $add = [
37
        'Actas',
38
        'OB Planning',
39
        'Studio Rikka',
40
        'Group TAC',
41
        'Brains Base',
42
        'Studio Nue',
43
        'Anima',
44
        'Hoods Entertainment',
45
        'Palm Studio',
46
        'MAPPA',
47
        'Cammot',
48
        'Fifth Avenue',
49
        'NAZ',
50
        'Pierrot Plus',
51
    ];
52
53
    /**
54
     * Rename studios.
55
     *
56
     * @var array
57
     */
58
    protected $rename = [
59
        'Xebec' => 'XEBEC',
60
        'Ufotable' => 'ufotable',
61
        'Shaft' => 'SHAFT',
62
        'A-1 Pictures' => 'A-1 Pictures Inc.',
63
        'Imagin' => 'IMAGIN',
64
        'Feel' => 'feel.',
65
        'Animax Entertainment' => 'Animax',
66
        'A.C.G.T' => 'A.C.G.T.',
67
        'Zexcs' => 'ZEXCS',
68
    ];
69
70
    /**
71
     * Set container.
72
     *
73
     * @param ContainerInterface $container
74
     */
75
    public function setContainer(ContainerInterface $container = null)
76
    {
77
        $this->em = $container->get('doctrine.orm.entity_manager');
0 ignored issues
show
Bug introduced by
It seems like $container is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
78
    }
79
80
    /**
81
     * @param Schema $schema
82
     */
83
    public function up(Schema $schema)
84
    {
85
        $rep = $this->em->getRepository('AnimeDbCatalogBundle:Studio');
86
87
        // rename studios
88
        /* @var $studio Studio */
89 View Code Duplication
        foreach ($this->rename as $from => $to) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
90
            $studio = $rep->findOneBy(['name' => $from]);
91
            $studio->setName($to);
92
            $this->em->persist($studio);
93
        }
94
95
        // add new studios
96
        foreach ($this->add as $name) {
97
            $studio = new Studio();
98
            $studio->setName($name);
99
            $this->em->persist($studio);
100
        }
101
        $this->em->flush();
102
    }
103
104
    /**
105
     * @param Schema $schema
106
     */
107
    public function down(Schema $schema)
108
    {
109
        $rep = $this->em->getRepository('AnimeDbCatalogBundle:Studio');
110
111
        // rename studios
112
        /* @var $studio Studio */
113 View Code Duplication
        foreach ($this->rename as $from => $to) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
114
            $studio = $rep->findOneBy(['name' => $to]);
115
            $studio->setName($from);
116
            $this->em->persist($studio);
117
        }
118
119
        // remove studios
120
        foreach ($this->add as $name) {
121
            $this->em->remove($rep->findOneBy(['name' => $name]));
122
        }
123
    }
124
}
125