Version20140408113030_AddItemGenres::up()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
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\Genre;
18
19
/**
20
 * Auto-generated Migration: Please modify to your needs!
21
 */
22
class Version20140408113030_AddItemGenres extends AbstractMigration implements ContainerAwareInterface
23
{
24
    /**
25
     * Entity manager.
26
     *
27
     * @var EntityManagerInterface
28
     */
29
    protected $em;
30
31
    /**
32
     * Add genres.
33
     *
34
     * @var array
35
     */
36
    protected $genres = [
37
        'Cars' => 'Машины',
38
        'Demons' => 'Демоны',
39
        'Game' => 'Игры',
40
        'Magic' => 'Магия',
41
        'Space' => 'Космос',
42
        'Super Power' => 'Супер сила',
43
        'Harem' => 'Гарем',
44
        'Supernatural' => 'Сверхъестественное',
45
        'Gender Bender' => 'Смена пола',
46
    ];
47
48
    /**
49
     * Set container.
50
     *
51
     * @param ContainerInterface $container
52
     */
53
    public function setContainer(ContainerInterface $container = null)
54
    {
55
        $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...
56
    }
57
58
    /**
59
     * @param Schema $schema
60
     */
61
    public function up(Schema $schema)
62
    {
63
        $rep = $this->em->getRepository('Gedmo\\Translatable\\Entity\\Translation');
64
        foreach ($this->genres as $en => $ru) {
65
            $genre = new Genre();
66
67
            $genre->setName($en)->setTranslatableLocale('en');
68
            $this->em->persist($genre);
69
            $this->em->flush($genre);
0 ignored issues
show
Unused Code introduced by
The call to EntityManagerInterface::flush() has too many arguments starting with $genre.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
70
71
            $rep->translate($genre, 'name', 'ru', $ru);
72
        }
73
        $this->em->flush();
74
    }
75
76
    /**
77
     * @param Schema $schema
78
     */
79
    public function down(Schema $schema)
80
    {
81
        $rep = $this->em->getRepository('AnimeDbCatalogBundle:Genre');
82
        foreach (array_keys($this->genres) as $en) {
83
            $this->em->remove($rep->findOneBy(['name' => $en]));
84
        }
85
    }
86
}
87