Completed
Pull Request — master (#3)
by Dmitry
30:18 queued 26:17
created

RequestAction::loadDepositRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
3
namespace hiqdev\yii2\merchant\actions;
4
5
use hiqdev\yii2\merchant\events\TransactionInsertEvent;
6
use hiqdev\yii2\merchant\models\DepositRequest;
7
use hiqdev\yii2\merchant\models\PurchaseRequest;
8
use hiqdev\yii2\merchant\Module;
9
use Yii;
10
use yii\base\Action;
11
use yii\base\Event;
12
use yii\web\BadRequestHttpException;
13
14
class RequestAction extends Action
15
{
16
    const EVENT_BEFORE_TRANSACTION_INSERT = 'before_transaction_insert';
17
    const EVENT_AFTER_TRANSACTION_INSERT = 'after_transaction_insert';
18
19
    /**
20
     * @return Module
21
     */
22
    protected function getMerchantModule()
23
    {
24
        return $this->controller->getMerchantModule();
25
    }
26
27
    public function run()
28
    {
29
        $depositRequest = $this->loadDepositRequest();
30
        $this->registerTransaction($depositRequest);
31
        $request = $this->createPurchaseRequest($depositRequest);
32
33
        return $this->handlePurchaseRequest($request);
34
    }
35
36
    /**
37
     * @return DepositRequest
38
     * @throws BadRequestHttpException
39
     */
40
    protected function loadDepositRequest()
41
    {
42
        $depositRequest = new DepositRequest();
43
        $depositRequest->load(Yii::$app->request->post());
44
        if (!$depositRequest->validate()) {
45
            throw new BadRequestHttpException('Deposit request is not loaded');
46
        }
47
48
        $this->getMerchantModule()->prepareRequestData($depositRequest);
49
50
        return $depositRequest;
51
    }
52
53
    /**
54
     * @param DepositRequest $depositRequest
55
     * @return \hiqdev\yii2\merchant\models\PurchaseRequest
56
     */
57
    protected function createPurchaseRequest($depositRequest)
58
    {
59
        $request = $this->getMerchantModule()->getPurchaseRequest($depositRequest->merchant, $depositRequest);
60
61
        return $request;
62
    }
63
64
    /**
65
     * @param DepositRequest $depositRequest
66
     */
67
    protected function registerTransaction($depositRequest)
68
    {
69
        $this->trigger(self::EVENT_BEFORE_TRANSACTION_INSERT, new TransactionInsertEvent(['depositRequest' => $depositRequest]));
70
71
        $transaction = $this->getMerchantModule()->insertTransaction($depositRequest->id, $depositRequest->merchant, array_merge([
72
            'username' => $depositRequest->username,
73
        ], $depositRequest->toArray()));
74
75
        $this->trigger(self::EVENT_AFTER_TRANSACTION_INSERT, new TransactionInsertEvent([
76
            'depositRequest' => $depositRequest,
77
            'transaction' => $transaction
78
        ]));
79
80
    }
81
82
    /**
83
     * @param PurchaseRequest $response
84
     * @return \yii\web\Response
85
     * @throws BadRequestHttpException
86
     */
87
    protected function handlePurchaseRequest($response)
88
    {
89
        if ('GET' === $response->getFormMethod()) {
90
            return $this->controller->redirect($response->getFormAction());
0 ignored issues
show
Bug introduced by
The method redirect does only exist in yii\web\Controller, but not in yii\base\Controller and yii\console\Controller.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
91
        } elseif ('POST' === $response->getFormMethod()) {
92
            $hiddenFields = '';
93
            foreach ($response->getFormInputs() as $key => $value) {
94
                $hiddenFields .= sprintf(
95
                        '<input type="hidden" name="%1$s" value="%2$s" />',
96
                        htmlentities($key, ENT_QUOTES, 'UTF-8', false),
97
                        htmlentities($value, ENT_QUOTES, 'UTF-8', false)
98
                    )."\n";
99
            }
100
101
            $output = '<!DOCTYPE html>
102
<html>
103
    <head>
104
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
105
        <title>Redirecting...</title>
106
    </head>
107
    <body onload="document.forms[0].submit();">
108
        <form action="%1$s" method="post">
109
            <p>Redirecting to payment page...</p>
110
            <p>
111
                %2$s
112
                <input type="submit" value="Continue" />
113
            </p>
114
        </form>
115
    </body>
116
</html>';
117
            $output = sprintf(
118
                $output,
119
                htmlentities($response->getFormAction(), ENT_QUOTES, 'UTF-8', false),
120
                $hiddenFields
121
            );
122
123
            echo $output;
124
            Yii::$app->end();
125
        }
126
127
        throw new BadRequestHttpException();
128
    }
129
}
130