RequestAction   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 6
dl 0
loc 116
ccs 0
cts 72
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getMerchantModule() 0 4 1
A run() 0 8 1
A loadDepositRequest() 0 13 2
A createPurchaseRequest() 0 6 1
A registerTransaction() 0 14 1
A handlePurchaseRequest() 0 41 4
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
            $errors = $depositRequest->getFirstErrors();
46
            throw new BadRequestHttpException('Deposit request is not loaded: ' . reset($errors));
47
        }
48
49
        $this->getMerchantModule()->prepareRequestData($depositRequest);
50
51
        return $depositRequest;
52
    }
53
54
    /**
55
     * @param DepositRequest $depositRequest
56
     * @return \hiqdev\yii2\merchant\models\PurchaseRequest
57
     */
58
    protected function createPurchaseRequest($depositRequest)
59
    {
60
        $request = $this->getMerchantModule()->getPurchaseRequest($depositRequest->merchant, $depositRequest);
61
62
        return $request;
63
    }
64
65
    /**
66
     * @param DepositRequest $depositRequest
67
     */
68
    protected function registerTransaction($depositRequest)
69
    {
70
        $this->trigger(self::EVENT_BEFORE_TRANSACTION_INSERT, new TransactionInsertEvent(['depositRequest' => $depositRequest]));
71
72
        $transaction = $this->getMerchantModule()->insertTransaction($depositRequest->id, $depositRequest->merchant, array_merge([
73
            'username' => $depositRequest->username,
74
            'currency' => $depositRequest->currency,
75
        ], $depositRequest->toArray()));
76
77
        $this->trigger(self::EVENT_AFTER_TRANSACTION_INSERT, new TransactionInsertEvent([
78
            'depositRequest' => $depositRequest,
79
            'transaction' => $transaction
80
        ]));
81
    }
82
83
    /**
84
     * @param PurchaseRequest $response
85
     * @return \yii\web\Response
86
     * @throws BadRequestHttpException
87
     */
88
    protected function handlePurchaseRequest($response)
89
    {
90
        if ('GET' === $response->getFormMethod()) {
91
            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...
92
        } elseif ('POST' === $response->getFormMethod()) {
93
            $hiddenFields = '';
94
            foreach ($response->getFormInputs() as $key => $value) {
95
                $hiddenFields .= sprintf(
96
                        '<input type="hidden" name="%1$s" value="%2$s" />',
97
                        htmlentities($key, ENT_QUOTES, 'UTF-8', false),
98
                        htmlentities($value, ENT_QUOTES, 'UTF-8', false)
99
                    )."\n";
100
            }
101
102
            $output = '<!DOCTYPE html>
103
<html>
104
    <head>
105
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
106
        <title>Redirecting...</title>
107
    </head>
108
    <body onload="document.forms[0].submit();">
109
        <form action="%1$s" method="post">
110
            <p>Redirecting to payment page...</p>
111
            <p>
112
                %2$s
113
                <input type="submit" value="Continue" />
114
            </p>
115
        </form>
116
    </body>
117
</html>';
118
            $output = sprintf(
119
                $output,
120
                htmlentities($response->getFormAction(), ENT_QUOTES, 'UTF-8', false),
121
                $hiddenFields
122
            );
123
124
            return $output;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $output; (string) is incompatible with the return type documented by hiqdev\yii2\merchant\act...::handlePurchaseRequest of type yii\web\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
125
        }
126
127
        throw new BadRequestHttpException();
128
    }
129
}
130