ThreemaGateway_Option_ThreemaGatewaySecret   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 0
loc 69
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B renderOption() 0 24 2
A verifyOption() 0 17 4
1
<?php
2
/**
3
 * Threema Gateway ID option.
4
 *
5
 * @package ThreemaGateway
6
 * @author rugk
7
 * @copyright Copyright (c) 2016 rugk
8
 * @license MIT
9
 */
10
11
class ThreemaGateway_Option_ThreemaGatewaySecret
12
{
13
    /**
14
     * @var int Chars not to censor when displaying secret
15
     */
16
    const CHARS_LEAVE_UNCENSORED = 2;
17
18
    /**
19
     * Renders the Threema Gateway Secret text input field.
20
     *
21
     * @param XenForo_View $view           View object
22
     * @param string       $fieldPrefix    Prefix for the HTML form field name
23
     * @param array        $preparedOption Prepared option info
24
     * @param bool         $canEdit        True if an "edit" link should appear
25
     *
26
     * @return XenForo_Template_Abstract Template object
27
     */
28
    public static function renderOption(XenForo_View $view, $fieldPrefix, array $preparedOption, $canEdit)
0 ignored issues
show
Unused Code introduced by
The parameter $fieldPrefix is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
29
    {
30
        if ($preparedOption['option_value']) {
31
            //censor option
32
            $preparedOption['option_value'] = ThreemaGateway_Helper_General::censorString($preparedOption['option_value'], self::CHARS_LEAVE_UNCENSORED);
33
34
            // add note to explain *** in field
35
            $preparedOption['explain']->setParams([
36
                'note' => new XenForo_Phrase('option_threema_gateway_threema_id_secret_explain_note')
37
            ]);
38
        } else {
39
            // set note to empty string
40
            $preparedOption['explain']->setParams([
41
                'note' => ''
42
            ]);
43
        }
44
45
        //modify array to show text box
46
        $preparedOption['edit_format']  = 'textbox';
47
        $preparedOption['formatParams'] = ['placeholder' => ''];
48
49
        //pass this to the default handler
50
        return XenForo_ViewAdmin_Helper_Option::renderPreparedOptionHtml($view, $preparedOption, $canEdit);
51
    }
52
53
    /**
54
     * Verifies the Threema Gateway Secret format.
55
     *
56
     * @param string             $threemagwsecret Input Threema ID secret
57
     * @param XenForo_DataWriter $dataWriter
58
     * @param string             $fieldName       Name of field/option
59
     *
60
     * @return bool
61
     */
62
    public static function verifyOption(&$threemagwsecret, XenForo_DataWriter $dataWriter, $fieldName)
63
    {
64
        //check whether change was really done by user
65
        // https://regex101.com/r/eY2uE3/3
66
        if (preg_match('/\*{' . (16 - self::CHARS_LEAVE_UNCENSORED) . '}[A-Za-z0-9]*/', $threemagwsecret)) {
67
            $threemagwsecret = $dataWriter->getExisting('option_value'); //reset old value
68
            return true;
69
        }
70
71
        //check for formal errors
72
        if ($threemagwsecret != '' && !preg_match('/[A-Za-z0-9]{16}/', $threemagwsecret)) {
73
            $dataWriter->error(new XenForo_Phrase('threemagw_invalid_threema_secret'), $fieldName);
74
            return false;
75
        }
76
77
        return true;
78
    }
79
}
80