Completed
Push — master ( 15775c...8cc55e )
by Dmitry
10:34
created

PayController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 52
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getMerchantModule() 0 4 1
A render() 0 4 1
A checkNotify() 0 22 3
A completeTransaction() 0 11 2
1
<?php
2
/**
3
 * Finance module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-finance
6
 * @package   hipanel-module-finance
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\controllers;
12
13
use hipanel\modules\finance\models\Merchant;
14
use hiqdev\hiart\ResponseErrorException;
15
use hiqdev\yii2\merchant\transactions\Transaction;
16
use Yii;
17
18
/**
19
 * Class PayController.
20
 * @property \hipanel\modules\finance\Module $module
21
 */
22
class PayController extends \hiqdev\yii2\merchant\controllers\PayController
23
{
24
    public function getMerchantModule()
25
    {
26
        return $this->module->getMerchant();
27
    }
28
29
    public function render($view, $params = [])
30
    {
31
        return $this->getMerchantModule()->getPayController()->render($view, $params);
32
    }
33
34
    public function checkNotify()
0 ignored issues
show
Coding Style introduced by
checkNotify uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
35
    {
36
        $id = Yii::$app->request->get('transactionId') ?: Yii::$app->request->post('transactionId');
37
        $transaction = $this->getMerchantModule()->findTransaction($id);
38
        $data = array_merge([
39
            'transactionId' => $transaction->getId(),
40
            'username'      => $transaction->getParameter('username'),
41
            'merchant'      => $transaction->getParameter('merchant'),
42
        ], $_REQUEST);
43
        Yii::info(http_build_query($data), 'merchant');
44
45
        try {
46
            Yii::$app->get('hiart')->disableAuth();
47
            $result = Merchant::perform('pay', $data);
48
49
            return $this->completeTransaction($transaction, $result);
0 ignored issues
show
Bug introduced by
It seems like $transaction defined by $this->getMerchantModule()->findTransaction($id) on line 37 can be null; however, hipanel\modules\finance\...::completeTransaction() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
50
        } catch (ResponseErrorException $e) {
51
            return false;
52
        } finally {
53
            Yii::$app->get('hiart')->enableAuth();
54
        }
55
    }
56
57
    /**
58
     * @param Transaction $transaction
59
     * @param $response
60
     * @return mixed
61
     */
62
    protected function completeTransaction($transaction, $response)
63
    {
64
        if ($transaction->isCompleted()) {
65
            return $transaction;
66
        }
67
68
        $transaction->confirm();
69
        $transaction->addParameter('bill_id', $response['id']);
70
71
        return $transaction;
72
    }
73
}
74