Completed
Pull Request — master (#1912)
by chihiro
33:37 queued 11:36
created

Version20161108095350::up()   B

Complexity

Conditions 7
Paths 24

Size

Total Lines 66
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 45
c 2
b 0
f 0
nc 24
nop 1
dl 0
loc 66
rs 7.0832

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
10
/**
11
 * Auto-generated Migration: Please modify to your needs!
12
 */
13
class Version20161108095350 extends AbstractMigration
14
{
15
    /**
16
     * @param Schema $schema
17
     */
18
    public function up(Schema $schema)
19
    {
20
        $app = Application::getInstance();
21
        $repository = $app['orm.em']->getRepository('Eccube\Entity\Master\ProductListOrderBy');
22
23
        $isDefault = true;
24
        $default = array(
25
            array(
26
                'id' => 1,
27
                'name' => '価格順',
28
                'rank' => 0,
29
            ),
30
            array(
31
                'id' => 2,
32
                'name' => '新着順',
33
                'rank' => 1,
34
            ),
35
        );
36
37
        // mtb_product_list_orderbyが初期状態から変更がないかどうかの判定
38
        $entities = $repository->findBy(array(), array('id' => 'ASC'));
39
        foreach ($entities as $entity) {
40
            if (!in_array($entity->toArray(), $default, true)) {
41
                $isDefault = false;
42
            }
43
        }
44
45
        // 価格が高い順ソートの追加
46
        $ProductListOrderBy = $repository->find(100);
47
        if (is_null($ProductListOrderBy)) {
48
            $rank = $repository->createQueryBuilder('pl')
49
                ->select('MAX(pl.rank)')
50
                ->getQuery()
51
                ->getSingleScalarResult();
52
            $ProductListOrderBy = new ProductListOrderBy();
53
            $ProductListOrderBy->setId(100);
54
            $ProductListOrderBy->setName('価格が高い順');
55
            $ProductListOrderBy->setRank($rank + 1);
56
            $app['orm.em']->persist($ProductListOrderBy);
57
            $app['orm.em']->flush($ProductListOrderBy);
58
        }
59
60
        // "価格順"の名称を"価格が低い順"へ変更
61
        $ProductListOrderBy = $repository->find(1);
62
        if (!is_null($ProductListOrderBy) && $ProductListOrderBy->getName() === '価格順') {
63
            $ProductListOrderBy->setName('価格が低い順');
64
            $app['orm.em']->persist($ProductListOrderBy);
65
            $app['orm.em']->flush($ProductListOrderBy);
66
        }
67
68
        // mtb_product_list_orderbyが初期状態から変更がなければ、価格が低い順->価格が高い順->新着順の順にrankを振り直す
69
        if ($isDefault) {
70
            $entity = $repository->find(1);
71
            $entity->setRank(0);
72
            $app['orm.em']->flush($entity);
73
74
            $entity = $repository->find(2);
75
            $entity->setRank(2);
76
            $app['orm.em']->flush($entity);
77
78
            $entity = $repository->find(100);
79
            dump($entity);
80
            $entity->setRank(1);
81
            $app['orm.em']->flush($entity);
82
        }
83
    }
84
85
    /**
86
     * @param Schema $schema
87
     */
88
    public function down(Schema $schema)
89
    {
90
        // this down() migration is auto-generated, please modify it to your needs
91
92
    }
93
}
94