__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
/**
3
 * Allows adding and removing providers.
4
 *
5
 * @package ThreemaGateway
6
 * @author rugk
7
 * @copyright Copyright (c) 2015-2016 rugk
8
 * @license MIT
9
 */
10
11
/**
12
 * Methods for writeing or reading from the DB.
13
 */
14
class ThreemaGateway_Installer_TfaProvider
15
{
16
    /**
17
     * @var string $tfaId The unique ID of the providet
18
     */
19
    protected $tfaId;
20
21
    /**
22
     * @var string $tfaClass The class which handles 2FA requests
23
     */
24
    protected $tfaClass;
25
26
    /**
27
     * @var int $tfaClass A number, which represents the priority of the
28
     *          provider.
29
     */
30
    protected $tfaPriority;
31
32
    /**
33
     * Add a new provider to the database.
34
     *
35
     * @param string $tfaId       The unique ID of the provider
36
     * @param string $tfaClass    The class which handles 2FA requests
37
     * @param int    $tfaPriority
38
     */
39
    public function __construct($tfaId, $tfaClass, $tfaPriority)
40
    {
41
        $this->tfaId       = $tfaId;
42
        $this->tfaClass    = $tfaClass;
43
        $this->tfaPriority = $tfaPriority;
44
    }
45
46
    /**
47
     * Add the new provider to the database.
48
     *
49
     * @param bool|int $enabled (optional) Whether the provider should
50
     *                          be activated or disabled.
51
     */
52
    public function add($enabled = true)
53
    {
54
        $db = XenForo_Application::get('db');
55
        $db->query('INSERT INTO `xf_tfa_provider`
56
                  (`provider_id`, `provider_class`, `priority`, `active`)
57
                  VALUES (?, ?, ?, ?)',
58
                  [$this->tfaId, $this->tfaClass, $this->tfaPriority, (int) $enabled]);
59
    }
60
61
    /**
62
     * Delete the provider from the database.
63
     */
64
    public function delete()
65
    {
66
        $db = XenForo_Application::get('db');
67
        // delete user data
68
        $db->delete('xf_user_tfa', [
69
            'provider_id = ?' => $this->tfaId
70
        ]);
71
72
        // unfortunately I do not want to go through each deleted user data here
73
        // to test whether the user has no 2FA mode anymore as it is done in
74
        // XenForo_Model_Tfa::disableTfaForUser.
75
        // I have not experienced any issues when this is not done and when the
76
        // deleted data is stored there this is not very bad.
77
78
        // delete provider itself
79
        $db->delete('xf_tfa_provider', [
80
            'provider_id = ?' => $this->tfaId
81
        ]);
82
    }
83
}
84