Test Failed
Push — master ( 74331d...6be589 )
by Alexey
04:30
created

MerchantsController   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 121
Duplicated Lines 11.57 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 14
loc 121
rs 10
c 0
b 0
f 0
wmc 27
lcom 1
cbo 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testPayAction() 0 14 1
C reciverAction() 14 34 11
B payAction() 0 25 4
A cancelPayAction() 0 9 2
D goAction() 0 31 9

How to fix   Duplicated Code   

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:

1
<?php
2
3
/**
4
 * Money merchants app 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 MerchantsController extends Controller {
12
13
    public function testPayAction() {
14
15
        $this->module->goToMerchant([
16
            'data' => 'test',
17
            'user_id' => \Users\User::$cur->id,
18
            'sum' => '0.01',
19
            'callback_module' => '',
20
            'callback_method' => ''
21
                ], null, [
22
            'description' => 'Тестовый платеж',
23
            'success' => 'http://' . INJI_DOMAIN_NAME . '/',
24
            'false' => 'http://' . INJI_DOMAIN_NAME . '/'
25
        ]);
26
    }
27
28
    public function reciverAction($system = '', $status = '') {
29
        $postData = [];
30 View Code Duplication
        foreach ($_POST as $key => $text) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
            if (!is_array($text) && !mb_detect_encoding($text, array('UTF-8'), true)) {
32
                $postData[$key] = iconv('Windows-1251', 'UTF-8', $text);
33
            } else {
34
                $postData[$key] = $text;
35
            }
36
        }
37 View Code Duplication
        foreach ($_GET as $key => $text) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
            if (!is_array($text) && !mb_detect_encoding($text, array('UTF-8'), true)) {
39
                $postData[$key] = iconv('Windows-1251', 'UTF-8', $text);
40
            } else {
41
                $postData[$key] = $text;
42
            }
43
        }
44
        $merchant = false;
45
46
        if ($system) {
47
            $merchant = \Money\Merchant::get($system, 'object_name');
48
        }
49
        if (!$system || !$merchant) {
50
            $merchant = \Money\Merchant::get($this->module->currentMerchant, 'object_name');
51
        }
52
        $request = new Money\Merchant\Request([
53
            'get' => json_encode($_GET),
54
            'post' => json_encode($_POST),
55
            'status' => $status,
56
            'system' => $system,
57
            'merchant_id' => $merchant ? $merchant->id : 0,
58
        ]);
59
        $request->save();
60
        $this->module->reciver($postData, $system, $status, $request);
61
    }
62
63
    public function payAction($pay_id = 0) {
64
        $pay = Money\Pay::get([['id', $pay_id], ['user_id', Users\User::$cur->id]]);
65
        if (!$pay) {
66
            $this->view->setTitle('Выбор счета для оплаты');
67
            $bread = [];
68
            $bread = ['text' => 'Просмотр счетов'];
69
            $where = [['user_id', \Users\User::$cur->id], ['pay_status_id', 1]];
70
            if (!empty($_GET['data'])) {
71
                $where[] = ['data', $_GET['data']];
72
            }
73
            $pays = Money\Pay::getList(['where' => $where, 'order' => ['date_create', 'DESC']]);
74
            if(count($pays)===1){
75
                Tools::redirect('/money/merchants/pay/'.current($pays)->id);
76
            }
77
            $this->view->page(['content' => 'pays', 'data' => compact('bread', 'pays')]);
78
        } else {
79
            $where = [['active', 1]];
80
            $where[] = [$pay->type, 1];
81
            $merchants = Money\Merchant::getList(['where' => $where]);
82
            $bread = [];
83
            $bread = ['text' => 'Оплата счета'];
84
            $this->view->setTitle('Выбор способа оплаты');
85
            $this->view->page(['data' => compact('bread', 'merchants', 'pay')]);
86
        }
87
    }
88
89
    public function cancelPayAction($pay_id = 0) {
90
        $pay = Money\Pay::get([['id', $pay_id], ['user_id', Users\User::$cur->id]]);
91
        if (!$pay) {
92
            Tools::redirect('/users/cabinet/wallets', 'Такой платеж несуществует');
93
        }
94
        $pay->pay_status_id = 3;
95
        $pay->save();
96
        Tools::redirect('/users/cabinet/wallets', 'Платеж был отменен');
97
    }
98
99
    public function goAction($pay_id, $merchant_id, $currency_id) {
100
        $pay = Money\Pay::get($pay_id);
101
        if (!$pay) {
102
            Tools::redirect('/', 'Такой платеж не найден', 'danger');
103
        }
104
        if ($pay->pay_status_id != 1) {
105
            Tools::redirect('/', 'Счет уже обработан');
106
        }
107
        $merchant = \Money\Merchant::get($merchant_id);
108
        if (!$merchant || !$merchant->active) {
109
            Tools::redirect('/', 'Такой способ оплаты не найден', 'danger');
110
        }
111
        $allowCurrencies = $merchant->allowCurrencies($pay);
112
        $method = [];
113
        foreach ($allowCurrencies as $allowCurrency) {
114
            if ($allowCurrency['currency']->id == $currency_id) {
115
                $method = $allowCurrency;
116
                break;
117
            }
118
        }
119
        if (!$method) {
120
            Tools::redirect('/', 'Валюта для этого способа оплаты не найдена', 'danger');
121
        }
122
        $merchantOptions = [
123
            'description' => $pay->description ? '#' . $pay->id . ' ' . $pay->description : 'Оплата счета №' . $pay->id . ' на сайте: ' . idn_to_utf8(INJI_DOMAIN_NAME),
124
            'success' => 'http://' . INJI_DOMAIN_NAME . '/',
125
            'false' => 'http://' . INJI_DOMAIN_NAME . '/'
126
        ];
127
128
        $this->Money->goToMerchant($pay, $merchant, $method, $merchantOptions);
129
    }
130
131
}
132