TextMessage::processConfirmRequest()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 2
1
<?php
2
/**
3
 * 2FA callback action for handeling text messages.
4
 *
5
 * @package ThreemaGateway
6
 * @author rugk
7
 * @copyright Copyright (c) 2015-2016 rugk
8
 * @license MIT
9
 */
10
11
class ThreemaGateway_Handler_Action_TfaCallback_TextMessage extends ThreemaGateway_Handler_Action_TfaCallback_Abstract
12
{
13
    /**
14
     * @var int replace chars in the string
15
     */
16
    const FILTER_REPLACE = 1;
17
18
    /**
19
     * @var int regular expression match, can only succeed or fail
20
     */
21
    const FILTER_REGEX_MATCH = 10;
22
23
    /**
24
     * @var string text of Threema message
25
     */
26
    protected $msgText;
27
28
    /**
29
     * Prepare the message handling. Should be called before any other actions.
30
     *
31
     * @return bool
32
     */
33
    public function prepareProcessing()
34
    {
35
        $this->msgText = trim($this->threemaMsg->getText());
36
37
        return true;
38
    }
39
40
    /**
41
     * Filters the passed data/message.
42
     *
43
     * Returns "false" if the process should be canceled. Otherwise "true".
44
     *
45
     * @param int   $filterType  please use the constants FILTER_*
46
     * @param mixed $filterData  any data the filter uses
47
     * @param bool  $failOnError whether the filter should fail on errors (true)
48
     *                           or silently ignore them (false)
49
     *
50
     * @throws XenForo_Exception
51
     * @return bool
52
     */
53
    protected function applyFilter($filterType, $filterData, $failOnError = true)
54
    {
55
        /** @var $success bool */
56
        $success = true;
0 ignored issues
show
Unused Code introduced by
$success is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
57
58
        switch ($filterType) {
59
            case $this::FILTER_REPLACE:
60
                $success = strtr($this->msgText, $filterData);
61
62
                if (is_string($success)) {
63
                    $this->msgText = $success;
64
                    $success       = true;
65
                } else {
66
                    $success = false;
67
                }
68
                break;
69
70
            case $this::FILTER_REGEX_MATCH:
71
                $success = preg_match($filterData, $this->msgText);
72
                break;
73
74
            default:
75
                throw new XenForo_Exception('Unknown filter type: ' . $filterType);
76
        }
77
78
        if ($failOnError && !$success) {
79
            return false;
80
        }
81
82
        return true;
83
    }
84
85
    /**
86
     * Does all steps needed to do before processing data.
87
     *
88
     * Returns "false" if the process should be canceled. Otherwise "true".
89
     *
90
     * @throws XenForo_Exception
91
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be false|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
92
     */
93
    protected function preProcessPending()
94
    {
95
        $this->log('Recognized ' . $this->name . ' (text message).');
96
        $this->log('[...]', 'Message text: ' . $this->msgText);
97
98
        if (!parent::preProcessPending()) {
99
            return false;
100
        };
101
    }
102
103
    /**
104
     * Verifies & saves data for one confirm request.
105
     *
106
     * Returns "false" if the process should be canceled. Otherwise "true".
107
     *
108
     * @param array $processOptions please include 'saveKey'
109
     *
110
     * @return bool
111
     */
112
    protected function processConfirmRequest($confirmRequest, array $processOptions = [])
113
    {
114
        if (!parent::processConfirmRequest($confirmRequest, $processOptions)) {
0 ignored issues
show
Unused Code introduced by
The call to ThreemaGateway_Handler_A...processConfirmRequest() has too many arguments starting with $processOptions.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
115
            return false;
116
        }
117
118
        // save data
119
        try {
120
            $this->setDataForRequest($confirmRequest, [
121
                $processOptions['saveKey'] => $this->msgText
122
            ]);
123
        } catch (Exception $e) {
124
            $this->log('Could not save data for request.', $e->getMessage());
125
        }
126
127
        // whether the code is the same as the requested one is verified in
128
        // the actual 2FA provider (verifyFromInput) later
129
130
        return true;
131
    }
132
}
133