Completed
Pull Request — master (#1912)
by chihiro
99:08 queued 93:00
created

Version20161108095350   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 97
rs 10
wmc 9
lcom 0
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A down() 0 5 1
C up() 0 82 8
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
        $isDefault = true;
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
39
        // mtb_product_list_orderbyが初期状態から変更がないかどうかの判定
40
        $entities = $repository->findBy(array(), array('id' => 'ASC'));
41
        foreach ($entities as $entity) {
42
            if (!in_array($entity->toArray(), $default, true)) {
43
                $isDefault = false;
44
            }
45
        }
46
47
        // 価格が高い順ソートの追加
48
        $id = $repository->createQueryBuilder('pl')
49
            ->select('MAX(pl.id) + 1')
50
            ->getQuery()
51
            ->getSingleScalarResult();
52
53
        $rank = $repository->createQueryBuilder('pl')
54
            ->select('MAX(pl.rank) + 1')
55
            ->getQuery()
56
            ->getSingleScalarResult();
57
58
        $ProductListOrderBy = new ProductListOrderBy();
59
        $ProductListOrderBy->setId($id);
60
        $ProductListOrderBy->setName('価格が高い順');
61
        $ProductListOrderBy->setRank($rank);
62
        $app['orm.em']->persist($ProductListOrderBy);
63
        $app['orm.em']->flush($ProductListOrderBy);
64
65
        // constant.ymlへ価格が高い順のIDを記録.
66
        if ($id !== $app['config']['product_order_price_higher']) {
67
            $file = $app['config']['root_dir'].'/app/config/eccube/constant.yml';
68
            $fs = new Filesystem();
69
70
            $constant = $fs->exists($file)
71
                ? Yaml::parse(file_get_contents($file))
72
                : array();
73
74
            $constant['product_order_price_higher'] = $id;
75
            $yaml = Yaml::dump($constant);
76
            $fs->dumpFile($file, $yaml);
77
        }
78
79
        // "価格順"の名称を"価格が低い順"へ変更
80
        $ProductListOrderBy = $repository->find(1);
81
        if (!is_null($ProductListOrderBy) && $ProductListOrderBy->getName() === '価格順') {
82
            $ProductListOrderBy->setName('価格が低い順');
83
            $app['orm.em']->persist($ProductListOrderBy);
84
            $app['orm.em']->flush($ProductListOrderBy);
85
        }
86
87
        // mtb_product_list_orderbyが初期状態から変更がなければ、価格が低い順->価格が高い順->新着順の順にrankを振り直す
88
        if ($isDefault) {
89
            $entity = $repository->find(1);
90
            $entity->setRank(0);
91
            $app['orm.em']->flush($entity);
92
93
            $entity = $repository->find(2);
94
            $entity->setRank(2);
95
            $app['orm.em']->flush($entity);
96
97
            $entity = $repository->find($id);
98
            $entity->setRank(1);
99
            $app['orm.em']->flush($entity);
100
        }
101
    }
102
103
    /**
104
     * @param Schema $schema
105
     */
106
    public function down(Schema $schema)
107
    {
108
        // this down() migration is auto-generated, please modify it to your needs
109
110
    }
111
}
112