TfaPendingMessagesConfirmation   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 99
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getPendingById() 0 15 2
B getPending() 0 33 4
A deleteExpired() 0 8 1
1
<?php
2
/**
3
 * Model for querying pending confirmation requests.
4
 *
5
 * @package ThreemaGateway
6
 * @author rugk
7
 * @copyright Copyright (c) 2015-2016 rugk
8
 * @license MIT
9
 */
10
11
class ThreemaGateway_Model_TfaPendingMessagesConfirmation extends XenForo_Model
12
{
13
    /**
14
     * @var string database table name
15
     */
16
    const DB_TABLE = 'xf_threemagw_tfa_pending_msgs_confirm';
17
18
    /**
19
     * @var int Pending type: a 6 digit code is requested
20
     */
21
    const PENDING_REQUEST_CODE = 1;
22
23
    /**
24
     * @var int Pending type: a delivery receipt is requested
25
     */
26
    const PENDING_REQUEST_DELIVERY_RECEIPT = 2;
27
28
    /**
29
     * Returns the pending confirmations by a given request ID.
30
     *
31
     * @param string $requestId
32
     *
33
     * @return null|array
34
     */
35
    public function getPendingById($requestId)
36
    {
37
        /** @var mixed $result result of SQL query */
38
        $result = $this->_getDb()->fetchRow(
39
            $this->_getDb()->select()
40
                ->from(self::DB_TABLE)
41
                ->where('request_id = ?', $requestId)
42
        );
43
44
        if (!$result) {
45
            return null;
46
        }
47
48
        return $result;
49
    }
50
51
    /**
52
     * Returns the pending confirmations by Threema ID and optionally also by
53
     * the pending type.
54
     *
55
     * @param string $threemaId
56
     * @param string $providerId  Provider ID of 2FA method
57
     * @param int    $pendingType use the PENDING_* constants
58
     *
59
     * @return null|array
60
     */
61
    public function getPending($threemaId, $providerId = null, $pendingType = null)
62
    {
63
        /** @var array $conditionsArray */
64
        $conditionsArray = [
65
            '`threema_id` = ?'
66
        ];
67
        $paramsArray = [
68
            $threemaId
69
        ];
70
71
        if ($providerId !== null) {
72
            $conditionsArray[] = '`provider_id` = ?';
73
            $paramsArray[]     = $providerId;
74
        }
75
76
        if ($pendingType !== null) {
77
            $conditionsArray[] = '`pending_type` = ?';
78
            $paramsArray[]     = $pendingType;
79
        }
80
81
        /** @var mixed $result result of SQL query */
82
        $result = $this->fetchAllKeyed(
83
            $this->_getDb()->select()
84
                ->from(self::DB_TABLE)
85
                ->where($this->getConditionsForClause($conditionsArray)),
86
            'request_id', $paramsArray);
87
88
        if (!$result) {
89
            return null;
90
        }
91
92
        return $result;
93
    }
94
95
    /**
96
     * Removes all expired pending requets.
97
     *
98
     * This should be executed regularely as otherwise the database gets filled
99
     * up with unconfirmed/never handled pending requests.
100
     */
101
    public function deleteExpired()
102
    {
103
        $this->_getDb()->delete(self::DB_TABLE,
104
            [
105
                '? > expiry_date' => XenForo_Application::$time
106
            ]
107
        );
108
    }
109
}
110