_getUpdateCondition()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * DataWriter for pending message requests of Threema 2FA.
4
 *
5
 * @package ThreemaGateway
6
 * @author rugk
7
 * @copyright Copyright (c) 2016 rugk
8
 * @license MIT
9
 */
10
11
class ThreemaGateway_DataWriter_TfaPendingMessagesConfirmation extends XenForo_DataWriter
12
{
13
    /**
14
     * Gets the fields that are defined for the table. See parent for explanation.
15
     *
16
     * @see XenForo_DataWriter::_getFields()
17
     * @return array
18
     */
19
    protected function _getFields()
20
    {
21
        return [
22
            ThreemaGateway_Model_TfaPendingMessagesConfirmation::DB_TABLE => [
23
                'request_id' => [
24
                    'type' => self::TYPE_UINT,
25
                    'autoIncrement' => true
26
                ],
27
                'threema_id' => [
28
                    'type' => self::TYPE_STRING,
29
                    'required' => true,
30
                    'maxLength' => 8
31
                ],
32
                'provider_id' => [
33
                    'type' => self::TYPE_STRING,
34
                    'required' => true
35
                ],
36
                'pending_type' => [
37
                    'type' => self::TYPE_UINT,
38
                    'required' => true
39
                ],
40
                'user_id' => [
41
                    'type' => self::TYPE_UINT,
42
                    'required' => true
43
                ],
44
                'session_id' => [
45
                    'type' => self::TYPE_STRING,
46
                    'required' => true
47
                ],
48
                'extra_data' => [
49
                    'type' => self::TYPE_BINARY,
50
                    'required' => false
51
                ],
52
                'expiry_date' => [
53
                    'type' => self::TYPE_UINT,
54
                    'required' => true
55
                ],
56
            ]
57
        ];
58
    }
59
60
    /**
61
     * Gets the actual existing data out of data that was passed in. See parent for explanation.
62
     *
63
     * @param mixed $data
64
     * @see XenForo_DataWriter::_getExistingData()
65
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be null|array?

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...
66
     */
67
    protected function _getExistingData($data)
68
    {
69
        // try primary key first
70
        /** @var string $requestId */
71
        if ($requestId = $this->_getExistingPrimaryKey($data, 'request_id')) {
72
            return [
73
                ThreemaGateway_Model_TfaPendingMessagesConfirmation::DB_TABLE =>
74
                    $this->_getPendingConfirmMsgModel()->getPendingById($requestId)
75
            ];
76
        }
77
78
        // or use other keys
79
        if (isset($data[ThreemaGateway_Model_TfaPendingMessagesConfirmation::DB_TABLE]['threema_id'])) {
80
            /** @var string|null $pendingType */
81
            $pendingType = null;
82
            /** @var string|null $providerId */
83
            $providerId = null;
84
85
            if (isset($data[ThreemaGateway_Model_TfaPendingMessagesConfirmation::DB_TABLE]['provider_id'])) {
86
                $pendingType = $data[ThreemaGateway_Model_TfaPendingMessagesConfirmation::DB_TABLE]['provider_id'];
87
            }
88
89
            if (isset($data[ThreemaGateway_Model_TfaPendingMessagesConfirmation::DB_TABLE]['pending_type'])) {
90
                $pendingType = $data[ThreemaGateway_Model_TfaPendingMessagesConfirmation::DB_TABLE]['pending_type'];
91
            }
92
93
            /** @var array|null $result database query result */
94
            $result = $this->_getPendingConfirmMsgModel()->getPending(
95
                $data[ThreemaGateway_Model_TfaPendingMessagesConfirmation::DB_TABLE]['threema_id'],
96
                $providerId,
97
                $pendingType
98
            );
99
100
            if (!$result) {
101
                return null;
102
            }
103
104
            // as result is keyed we just use the first value here (usually there should
105
            // only be one value anyway)
106
            return [
107
                ThreemaGateway_Model_TfaPendingMessagesConfirmation::DB_TABLE =>
108
                    current($result)
109
            ];
110
        }
111
112
        return [];
113
    }
114
115
    /**
116
     * Gets SQL condition to update the existing record.
117
     *
118
     * @param string $tableName
119
     * @see XenForo_DataWriter::_getUpdateCondition()
120
     * @return string
121
     */
122
    protected function _getUpdateCondition($tableName)
123
    {
124
        return 'request_id = ' . $this->_db->quote($this->getExisting('request_id'));
125
    }
126
127
128
    /**
129
     * Get the pending confirmation messages model.
130
     *
131
     * @return ThreemaGateway_Model_TfaPendingMessagesConfirmation
132
     */
133
    protected function _getPendingConfirmMsgModel()
134
    {
135
        return $this->getModelFromCache('ThreemaGateway_Model_TfaPendingMessagesConfirmation');
136
    }
137
}
138