PluginEventHandlerRepository::getPriorityRange()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.4285
cc 3
eloc 11
nc 3
nop 1
crap 3
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
namespace Eccube\Repository;
25
26
use Doctrine\ORM\EntityRepository;
27
use Eccube\Entity\PluginEventHandler;
28
use Eccube\Exception\PluginException;
29
30
31
/**
32
 * PluginEventHandlerRepository
33
 *
34
 * This class was generated by the Doctrine ORM. Add your own custom
35
 * repository methods below.
36
 */
37
class PluginEventHandlerRepository extends EntityRepository
38
{
39
40 1189
    public function getHandlers()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
41
    {
42 1189
        $qb = $this->createQueryBuilder('e')
43 1189
            ->innerJoin('e.Plugin', 'p')
44 1189
            ->andWhere('e.del_flg = 0 ')  
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
45 1189
            ->Orderby('e.event','ASC') 
0 ignored issues
show
introduced by
Add a single space after each comma delimiter
Loading history...
introduced by
Please trim any trailing whitespace
Loading history...
46 1189
            ->addOrderby('e.priority','DESC');
0 ignored issues
show
introduced by
Add a single space after each comma delimiter
Loading history...
47
            ;
48
49 1189
        return $qb->getQuery()->getResult();
50
    }
51
52 3
    public function getPriorityRange($type)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
53
    {
54
55 3
        if(PluginEventHandler::EVENT_HANDLER_TYPE_FIRST==$type){
0 ignored issues
show
introduced by
Add a single space around binary operators
Loading history...
56 1
            $range_start=PluginEventHandler::EVENT_PRIORITY_FIRST_START;
57 1
            $range_end=PluginEventHandler::EVENT_PRIORITY_FIRST_END;
58 3
        }elseif(PluginEventHandler::EVENT_HANDLER_TYPE_LAST==$type){
0 ignored issues
show
introduced by
Add a single space around binary operators
Loading history...
59 1
            $range_start=PluginEventHandler::EVENT_PRIORITY_LAST_START;
60 1
            $range_end=PluginEventHandler::EVENT_PRIORITY_LAST_END;
61
        }else{
62 3
            $range_start=PluginEventHandler::EVENT_PRIORITY_NORMAL_START;
63 3
            $range_end=PluginEventHandler::EVENT_PRIORITY_NORMAL_END;
64
        }
65 3
        return array($range_start,$range_end);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
introduced by
Add a single space after each comma delimiter
Loading history...
66
67
    }
68
69 3
    public function calcNewPriority($event , $type)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
Coding Style introduced by
Expected 0 spaces between argument "$event" and comma; 1 found
Loading history...
70
    {
71
72 3
        list($range_start,$range_end) = $this->getPriorityRange($type);
0 ignored issues
show
introduced by
Add a single space after each comma delimiter
Loading history...
73
74 3
        $qb = $this->createQueryBuilder('e');
75 3
        $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...
76 3
           ->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...
77 3
           ->andWhere('e.event = :event')
78 3
           ->setParameter('event',$event)
0 ignored issues
show
introduced by
Add a single space after each comma delimiter
Loading history...
79 3
           ->setMaxResults(1)
80 3
           ->orderBy('e.priority','ASC');
0 ignored issues
show
introduced by
Add a single space after each comma delimiter
Loading history...
81
82 3
        $result=$qb->getQuery()->getResult();
83 3
        if(count($result)){
84
             return $result[0]->getPriority() -1;
85
        }else{
86 3
             return $range_start;
87
        }
88
89
    }
90
91
    public function upPriority($pluginEventHandler,$up=true)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
introduced by
Add a single space after each comma delimiter
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$up" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$up"; expected 1 but found 0
Loading history...
Coding Style introduced by
Expected 1 space between comma and argument "$up"; 0 found
Loading history...
92
    {
93
94
        list($range_start,$range_end) = $this->getPriorityRange($pluginEventHandler->getHandlerType());
0 ignored issues
show
introduced by
Add a single space after each comma delimiter
Loading history...
95
96
        $qb = $this->createQueryBuilder('e');
97
98
        $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...
99
           ->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...
100
           ->andWhere("e.del_flg = 0 ") 
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
101
           ->andWhere('e.priority '.($up ?  '>' : '<' ).' :pri')
102
           ->andWhere('e.event = :event')
103
           ->setParameter('event',$pluginEventHandler->getEvent())
0 ignored issues
show
introduced by
Add a single space after each comma delimiter
Loading history...
104
           ->setParameter('pri',  $pluginEventHandler->getPriority()  )
0 ignored issues
show
Coding Style introduced by
Expected 1 space instead of 2 after comma in function call.
Loading history...
105
           ->setMaxResults(1)
106
           ->orderBy('e.priority', ($up ? 'ASC':'DESC' )  );
107
108
        $result=$qb->getQuery()->getResult();
109
110
        if(count($result)){
111
            $em =$this->getEntityManager();
112
            $em->getConnection()->beginTransaction();
113
            // 2個のentityのprioriryを入れ替える
114
            $tmp=$pluginEventHandler->getPriority();
115
            $pluginEventHandler->setPriority($result[0]->getPriority());
116
            $result[0]->setPriority($tmp);
117
            $em->persist($result[0]);
118
            $em->persist($pluginEventHandler);
119
            $em->flush();
120
            $em->getConnection()->commit();
121
            # 移動する
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
122
        }else{
123
            # 移動しない
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
124
            throw new PluginException("Can't swap");
125
        }
126
127
128
    }
129
130
}
131