Conditions | 28 |
Paths | > 20000 |
Total Lines | 145 |
Code Lines | 86 |
Lines | 0 |
Ratio | 0 % |
Tests | 47 |
CRAP Score | 51.086 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
72 | 182 | public function getByRule($Product = null, $ProductClass = null, $Pref = null, $Country = null) |
|
73 | { |
||
74 | 2 | if (!$this->app) { |
|
75 | throw new \LogicException(); |
||
76 | } |
||
77 | |||
78 | // Pref Country 設定 |
||
79 | if (!$Pref && !$Country && $this->app['security']->getToken() && $this->app['security']->isGranted('ROLE_USER')) { |
||
80 | /* @var $Customer \Eccube\Entity\Customer */ |
||
81 | $Customer = $this->app['security']->getToken()->getUser(); |
||
82 | $Pref = $Customer->getPref(); |
||
83 | $Country = $Customer->getCountry(); |
||
84 | } |
||
85 | |||
86 | // 商品単位税率設定がOFFの場合 |
||
87 | /** @var $BaseInfo \Eccube\Entity\BaseInfo */ |
||
88 | $BaseInfo = $this->app['eccube.repository.base_info']->get(); |
||
89 | if ($BaseInfo->getOptionProductTaxRule() !== Constant::ENABLED) { |
||
90 | 2 | $Product = null; |
|
91 | 2 | $ProductClass = null; |
|
92 | } |
||
93 | |||
94 | // Cache Key 設定 |
||
95 | 2 | if ($Product instanceof \Eccube\Entity\Product) { |
|
96 | $productId = $Product->getId(); |
||
97 | 2 | } elseif ($Product) { |
|
98 | $productId = $Product; |
||
99 | } else { |
||
100 | 2 | $productId = '0'; |
|
101 | 2 | } |
|
102 | 2 | if ($ProductClass instanceof \Eccube\Entity\ProductClass) { |
|
103 | $productClassId = $ProductClass->getId(); |
||
104 | 2 | } else if ($ProductClass instanceof \Eccube\Entity\ShipmentItem) { |
|
105 | // XXX https://github.com/EC-CUBE/ec-cube/issues/1029 |
||
106 | // 注文処理時、TaxRuleEventSubscriber::prePersistからの呼び出しで、 |
||
107 | // $ProductClassにShipmentItemがsetされて呼び出されるのに対応 |
||
108 | $productClassId = ''; |
||
109 | 2 | } elseif ($ProductClass) { |
|
110 | $productClassId = $ProductClass; |
||
111 | } else { |
||
112 | 2 | $productClassId = '0'; |
|
113 | 1 | } |
|
114 | 2 | if ($Pref instanceof \Eccube\Entity\Master\Pref) { |
|
115 | $prefId = $Pref->getId(); |
||
116 | 2 | } elseif ($Pref) { |
|
117 | $prefId = $Pref; |
||
118 | } else { |
||
119 | 2 | $prefId = '0'; |
|
120 | 1 | } |
|
121 | 2 | if ($Country instanceof \Eccube\Entity\Master\Country) { |
|
122 | $countryId = $Country->getId(); |
||
123 | 2 | } elseif ($Country) { |
|
124 | $countryId = $Country; |
||
125 | } else { |
||
126 | 2 | $countryId = '0'; |
|
127 | 2 | } |
|
128 | 2 | $cacheKey = $productId.':'.$productClassId.':'.$prefId.':'.$countryId; |
|
129 | |||
130 | // すでに取得している場合はキャッシュから |
||
131 | 2 | if (isset($this->rules[$cacheKey])) { |
|
132 | 10 | return $this->rules[$cacheKey]; |
|
133 | } |
||
134 | |||
135 | 182 | $parameters = array(); |
|
136 | 182 | $qb = $this->createQueryBuilder('t') |
|
137 | ->where('t.apply_date < CURRENT_TIMESTAMP()'); |
||
138 | |||
139 | // Pref |
||
140 | 182 | if ($Pref) { |
|
141 | $qb->andWhere('t.Pref IS NULL OR t.Pref = :Pref'); |
||
142 | 26 | $parameters['Pref'] = $Pref; |
|
143 | } else { |
||
144 | $qb->andWhere('t.Pref IS NULL'); |
||
145 | 26 | } |
|
146 | |||
147 | // Country |
||
148 | 182 | if ($Country) { |
|
149 | $qb->andWhere('t.Country IS NULL OR t.Country = :Country'); |
||
150 | 2 | $parameters['Country'] = $Country; |
|
151 | } else { |
||
152 | $qb->andWhere('t.Country IS NULL'); |
||
153 | 2 | } |
|
154 | |||
155 | /* |
||
156 | * Product, ProductClass が persist される前に TaxRuleEventSubscriber によってアクセスされる |
||
157 | * 場合があるため、ID の存在もチェックする. |
||
158 | * https://github.com/EC-CUBE/ec-cube/issues/677 |
||
159 | */ |
||
160 | |||
161 | // Product |
||
162 | 182 | if ($Product && $productId > 0) { |
|
163 | $qb->andWhere('t.Product IS NULL OR t.Product = :Product'); |
||
164 | 2 | $parameters['Product'] = $Product; |
|
165 | } else { |
||
166 | $qb->andWhere('t.Product IS NULL'); |
||
167 | 2 | } |
|
168 | |||
169 | // ProductClass |
||
170 | 182 | if ($ProductClass && $productClassId > 0) { |
|
171 | $qb->andWhere('t.ProductClass IS NULL OR t.ProductClass = :ProductClass'); |
||
172 | 1 | $parameters['ProductClass'] = $ProductClass; |
|
173 | } else { |
||
174 | $qb->andWhere('t.ProductClass IS NULL'); |
||
175 | 1 | } |
|
176 | |||
177 | $TaxRules = $qb |
||
178 | 182 | ->setParameters($parameters) |
|
179 | 182 | ->orderBy('t.apply_date', 'DESC') // 実際は usort() でソートする |
|
180 | 182 | ->getQuery() |
|
181 | ->getResult(); |
||
182 | |||
183 | // 地域設定を優先するが、システムパラメーターなどに設定を持っていくか |
||
184 | // 後に書いてあるほど優先される |
||
185 | $priorityKeys = explode(',', $this->app['config']['tax_rule_priority']); |
||
186 | 182 | $priorityKeys = array(); |
|
187 | foreach (explode(',', $this->app['config']['tax_rule_priority']) as $key) { |
||
188 | $priorityKeys[] = str_replace('_', '', preg_replace('/_id\z/', '', $key)); |
||
189 | } |
||
190 | |||
191 | foreach ($TaxRules as $TaxRule) { |
||
192 | 182 | $rank = 0; |
|
193 | foreach ($priorityKeys as $index => $key) { |
||
194 | $arrayProperties = array_change_key_case($TaxRule->toArray()); |
||
195 | if ($arrayProperties[$key]) { |
||
196 | |||
197 | // 配列の数値添字を重みとして利用する |
||
198 | 4 | $rank += 1 << ($index + 1); |
|
199 | } |
||
200 | } |
||
201 | $TaxRule->setRank($rank); |
||
202 | } |
||
203 | |||
204 | // 適用日降順, rank 降順にソートする |
||
205 | usort($TaxRules, function($a, $b) { |
||
206 | return $a->compareTo($b); |
||
207 | }); |
||
208 | |||
209 | 182 | if (!empty($TaxRules)) { |
|
210 | 182 | $this->rules[$cacheKey] = $TaxRules[0]; |
|
211 | |||
212 | 182 | return $TaxRules[0]; |
|
213 | } else { |
||
214 | throw new NoResultException(); |
||
215 | } |
||
216 | } |
||
217 | |||
297 |