Completed
Push — master ( 949af3...6525bd )
by Yangsin
143:56 queued 137:59
created

Version20161108095350::up()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 59
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 4
eloc 38
c 5
b 0
f 0
nc 3
nop 1
dl 0
loc 59
rs 8.9846

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
namespace DoctrineMigrations;
4
5
use Doctrine\DBAL\Migrations\AbstractMigration;
6
use Doctrine\DBAL\Schema\Schema;
7
use Eccube\Application;
8
use Eccube\Entity\Master\ProductListOrderBy;
9
use Symfony\Component\Filesystem\Filesystem;
10
use Symfony\Component\Yaml\Yaml;
11
12
/**
13
 * Auto-generated Migration: Please modify to your needs!
14
 */
15
class Version20161108095350 extends AbstractMigration
16
{
17
    /**
18
     * @param Schema $schema
19
     */
20
    public function up(Schema $schema)
21
    {
22
        $app = Application::getInstance();
23
        $repository = $app['orm.em']->getRepository('Eccube\Entity\Master\ProductListOrderBy');
24
25
        // mtb_product_list_orderbyが初期状態から変更がある場合は、マイグレーションを適用しない
26
        $default = array(
27
            array(
28
                'id' => 1,
29
                'name' => '価格順',
30
                'rank' => 0,
31
            ),
32
            array(
33
                'id' => 2,
34
                'name' => '新着順',
35
                'rank' => 1,
36
            ),
37
        );
38
        $entities = $repository->createQueryBuilder('pl')
39
            ->orderBy('pl.id', 'ASC')
40
            ->getQuery()
41
            ->getArrayResult();
42
43
        if ($entities !== $default) {
44
            return;
45
        }
46
47
        // 価格が高い順を追加
48
        $ProductListOrderBy = new ProductListOrderBy();
49
        $ProductListOrderBy->setId(3);
50
        $ProductListOrderBy->setName('価格が高い順');
51
        $ProductListOrderBy->setRank(2);
52
        $app['orm.em']->persist($ProductListOrderBy);
53
        $app['orm.em']->flush($ProductListOrderBy);
54
55
        // "価格順"の名称を"価格が低い順"へ変更
56
        $ProductListOrderBy = $repository->find(1);
57
        if (!is_null($ProductListOrderBy) && $ProductListOrderBy->getName() === '価格順') {
58
            $ProductListOrderBy->setName('価格が低い順');
59
            $app['orm.em']->persist($ProductListOrderBy);
60
            $app['orm.em']->flush($ProductListOrderBy);
61
        }
62
63
        // 価格が低い順->価格が高い順->新着順の順にrankを振り直す
64
        // 価格が低い順
65
        $entity = $repository->find(1);
66
        $entity->setRank(0);
67
        $app['orm.em']->flush($entity);
68
69
        // 価格が高い順
70
        $entity = $repository->find(3);
71
        $entity->setRank(1);
72
        $app['orm.em']->flush($entity);
73
74
        // 新着順
75
        $entity = $repository->find(2);
76
        $entity->setRank(2);
77
        $app['orm.em']->flush($entity);
78
    }
79
80
    /**
81
     * @param Schema $schema
82
     */
83
    public function down(Schema $schema)
84
    {
85
        // this down() migration is auto-generated, please modify it to your needs
86
87
    }
88
}
89