Passed
Push — v5 ( e348bf...8a8f01 )
by Alexey
06:35
created

MoneyController   C

Complexity

Total Complexity 56

Size/Duplication

Total Lines 43
Duplicated Lines 60.47 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 26
loc 43
rs 6.5957
c 0
b 0
f 0
wmc 56
lcom 1
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A completeTransferAction() 0 10 1
B manualClosePayAction() 0 15 5

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like MoneyController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use MoneyController, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * Money admin Controller
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
class MoneyController extends adminController {
12
13
    public function manualClosePayAction($payId) {
14
        $pay = \Money\Pay::get((int) $payId);
15
        $result = new Server\Result();
16
        if ($pay && $pay->pay_status_id == 1) {
17
            $pay->pay_status_id = 2;
18
            $pay->save();
19
            if ($pay->callback_module && $pay->callback_method) {
20
                \App::$primary->{$pay->callback_module}->{$pay->callback_method}(['status' => 'success', 'payId' => $pay->id, 'pay' => $pay]);
21
            }
22
            $result->successMsg = 'Счет был проведен';
23
            $result->send();
24
        }
25
        $result->success = false;
26
        $result->content = 'Такой счет не найден';
27
        $result->send();
28
    }
29
30
    public function cancelTransferAction($transferId) {
31
        $transfer = Money\Transfer::get($transferId);
32
        $result = new Server\Result();
33
34
        $result->success = $transfer->cancel();
35
36
        $result->successMsg = 'Перевод был отменен';
37
        $result->content = 'Не удалось отменить перевод';
38
        $result->send();
39
    }
40
41
    public function completeTransferAction($transferId) {
42
        $transfer = Money\Transfer::get($transferId);
43
44
        $result = new Server\Result();
45
46
        $result->success = $transfer->complete();
47
48
        $result->successMsg = 'Перевод был завершен';
49
        $result->content = 'Не удалось завершить перевод';
50
        $result->send();
51
    }
52
53
}
54