Failed Conditions
Branch experimental/sf (68db07)
by Kentaro
42:17 queued 33:39
created

PluginEventHandlerRepository::calcNewPriority()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
ccs 0
cts 13
cp 0
crap 6
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Repository;
15
16
use Eccube\Entity\PluginEventHandler;
17
use Eccube\Exception\PluginException;
18
use Symfony\Bridge\Doctrine\RegistryInterface;
19
20
/**
21
 * PluginEventHandlerRepository
22
 *
23
 * This class was generated by the Doctrine ORM. Add your own custom
24
 * repository methods below.
25
 */
26
class PluginEventHandlerRepository extends AbstractRepository
27
{
28 1
    public function __construct(RegistryInterface $registry)
29
    {
30 1
        parent::__construct($registry, PluginEventHandler::class);
31
    }
32
33
    public function getHandlers()
34
    {
35
        $qb = $this->createQueryBuilder('e')
36
            ->innerJoin('e.Plugin', 'p')
37
            ->Orderby('e.event', 'ASC')
38
            ->addOrderby('e.priority', 'DESC');
39
40
        return $qb->getQuery()->getResult();
41
    }
42
43
    public function getPriorityRange($type)
44
    {
45
        if (PluginEventHandler::EVENT_HANDLER_TYPE_FIRST == $type) {
46
            $range_start = PluginEventHandler::EVENT_PRIORITY_FIRST_START;
47
            $range_end = PluginEventHandler::EVENT_PRIORITY_FIRST_END;
48
        } elseif (PluginEventHandler::EVENT_HANDLER_TYPE_LAST == $type) {
49
            $range_start = PluginEventHandler::EVENT_PRIORITY_LAST_START;
50
            $range_end = PluginEventHandler::EVENT_PRIORITY_LAST_END;
51
        } else {
52
            $range_start = PluginEventHandler::EVENT_PRIORITY_NORMAL_START;
53
            $range_end = PluginEventHandler::EVENT_PRIORITY_NORMAL_END;
54
        }
55
56
        return [$range_start, $range_end];
57
    }
58
59
    public function calcNewPriority($event, $type)
60
    {
61
        list($range_start, $range_end) = $this->getPriorityRange($type);
62
63
        $qb = $this->createQueryBuilder('e');
64
        $qb->andWhere("e.priority >= $range_end ")
0 ignored issues
show
Coding Style introduced by
Variable "range_end" is not in valid camel caps format
Loading history...
65
           ->andWhere("e.priority <= $range_start ")
0 ignored issues
show
Coding Style introduced by
Variable "range_start" is not in valid camel caps format
Loading history...
66
           ->andWhere('e.event = :event')
67
           ->setParameter('event', $event)
68
           ->setMaxResults(1)
69
           ->orderBy('e.priority', 'ASC');
70
71
        $result = $qb->getQuery()->getResult();
72
        if (count($result)) {
73
            return $result[0]->getPriority() - 1;
74
        } else {
75
            return $range_start;
76
        }
77
    }
78
79
    public function upPriority($pluginEventHandler, $up = true)
80
    {
81
        list($range_start, $range_end) = $this->getPriorityRange($pluginEventHandler->getHandlerType());
82
83
        $qb = $this->createQueryBuilder('e');
84
85
        $qb->andWhere("e.priority >= $range_end ")
0 ignored issues
show
Coding Style introduced by
Variable "range_end" is not in valid camel caps format
Loading history...
86
           ->andWhere("e.priority <= $range_start ")
0 ignored issues
show
Coding Style introduced by
Variable "range_start" is not in valid camel caps format
Loading history...
87
           ->andWhere('e.priority '.($up ? '>' : '<').' :pri')
88
           ->andWhere('e.event = :event')
89
           ->setParameter('event', $pluginEventHandler->getEvent())
90
           ->setParameter('pri', $pluginEventHandler->getPriority())
91
           ->setMaxResults(1)
92
           ->orderBy('e.priority', ($up ? 'ASC' : 'DESC'));
93
94
        $result = $qb->getQuery()->getResult();
95
96
        if (count($result)) {
97
            $em = $this->getEntityManager();
98
            $em->getConnection()->beginTransaction();
99
            // 2個のentityのprioriryを入れ替える
100
            $tmp = $pluginEventHandler->getPriority();
101
            $pluginEventHandler->setPriority($result[0]->getPriority());
102
            $result[0]->setPriority($tmp);
103
            $em->persist($result[0]);
104
            $em->persist($pluginEventHandler);
105
            $em->flush();
106
            $em->getConnection()->commit();
107
        // 移動する
108
        } else {
109
            // 移動しない
110
            throw new PluginException("Can't swap");
111
        }
112
    }
113
}
114