Credential   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 214
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 23
eloc 72
dl 0
loc 214
rs 10
c 1
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getPriority() 0 3 1
A setVersion() 0 3 1
A setFactor() 0 3 1
A getFactor() 0 3 1
A setTimeout() 0 7 2
A setData() 0 3 1
B save() 0 59 5
A setType() 0 3 1
A getData() 0 3 1
A setPriority() 0 3 1
A setUserId() 0 3 1
A getUserId() 0 3 1
A getDisabled() 0 3 1
A getTimeout() 0 7 2
A getType() 0 3 1
A setDisabled() 0 3 1
A getVersion() 0 3 1
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\DataObjects;
10
11
use DateTimeImmutable;
12
use Exception;
13
use Waca\DataObject;
14
use Waca\Exceptions\OptimisticLockFailedException;
15
16
class Credential extends DataObject
17
{
18
    /** @var int */
19
    private $user;
20
    /** @var int */
21
    private $factor;
22
    /** @var string */
23
    private $type;
24
    /** @var string */
25
    private $data;
26
    /** @var int */
27
    private $version;
28
    private $timeout;
29
    /** @var int */
30
    private $disabled = 0;
31
    /** @var int */
32
    private $priority;
33
34
    /**
35
     * @return int
36
     */
37
    public function getUserId()
38
    {
39
        return $this->user;
40
    }
41
42
    /**
43
     * @param int $user
44
     */
45
    public function setUserId($user)
46
    {
47
        $this->user = $user;
48
    }
49
50
    /**
51
     * @return int
52
     */
53
    public function getFactor()
54
    {
55
        return $this->factor;
56
    }
57
58
    /**
59
     * @param int $factor
60
     */
61
    public function setFactor($factor)
62
    {
63
        $this->factor = $factor;
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    public function getType()
70
    {
71
        return $this->type;
72
    }
73
74
    /**
75
     * @param string $type
76
     */
77
    public function setType($type)
78
    {
79
        $this->type = $type;
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    public function getData()
86
    {
87
        return $this->data;
88
    }
89
90
    /**
91
     * @param string $data
92
     */
93
    public function setData($data)
94
    {
95
        $this->data = $data;
96
    }
97
98
    /**
99
     * @return int
100
     */
101
    public function getVersion()
102
    {
103
        return $this->version;
104
    }
105
106
    /**
107
     * @param int $version
108
     */
109
    public function setVersion($version)
110
    {
111
        $this->version = $version;
112
    }
113
114
    /**
115
     * @return mixed
116
     */
117
    public function getTimeout()
118
    {
119
        if ($this->timeout === null) {
120
            return null;
121
        }
122
123
        return new DateTimeImmutable($this->timeout);
124
    }
125
126
    /**
127
     * @param mixed $timeout
128
     */
129
    public function setTimeout(DateTimeImmutable $timeout = null)
130
    {
131
        if ($timeout === null) {
132
            $this->timeout = null;
133
        }
134
        else {
135
            $this->timeout = $timeout->format('Y-m-d H:i:s');
136
        }
137
    }
138
139
    /**
140
     * @return int
141
     */
142
    public function getDisabled()
143
    {
144
        return $this->disabled;
145
    }
146
147
    /**
148
     * @param int $disabled
149
     */
150
    public function setDisabled($disabled)
151
    {
152
        $this->disabled = $disabled;
153
    }
154
155
    /**
156
     * @return int
157
     */
158
    public function getPriority()
159
    {
160
        return $this->priority;
161
    }
162
163
    /**
164
     * @param int $priority
165
     */
166
    public function setPriority($priority)
167
    {
168
        $this->priority = $priority;
169
    }
170
171
    public function save()
172
    {
173
        if ($this->isNew()) {
174
            // insert
175
            $statement = $this->dbObject->prepare(<<<SQL
176
INSERT INTO credential ( updateversion, user, factor, type, data, version, timeout, disabled, priority )
177
VALUES ( 0, :user, :factor, :type, :data, :version, :timeout, :disabled, :priority );
178
SQL
179
            );
180
            $statement->bindValue(":user", $this->user);
181
            $statement->bindValue(":factor", $this->factor);
182
            $statement->bindValue(":type", $this->type);
183
            $statement->bindValue(":data", $this->data);
184
            $statement->bindValue(":version", $this->version);
185
            $statement->bindValue(":timeout", $this->timeout);
186
            $statement->bindValue(":disabled", $this->disabled);
187
            $statement->bindValue(":priority", $this->priority);
188
189
            if ($statement->execute()) {
190
                $this->id = (int)$this->dbObject->lastInsertId();
191
            }
192
            else {
193
                throw new Exception($statement->errorInfo());
0 ignored issues
show
Bug introduced by
$statement->errorInfo() of type array is incompatible with the type string expected by parameter $message of Exception::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

193
                throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
194
            }
195
        }
196
        else {
197
            // update
198
            $statement = $this->dbObject->prepare(<<<SQL
199
                UPDATE credential
200
                SET   factor = :factor
201
                    , data = :data
202
                    , version = :version
203
                    , timeout = :timeout
204
                    , disabled = :disabled
205
                    , priority = :priority
206
                    , updateversion = updateversion + 1
207
                WHERE id = :id AND updateversion = :updateversion;
208
SQL
209
            );
210
211
            $statement->bindValue(':id', $this->id);
212
            $statement->bindValue(':updateversion', $this->updateversion);
213
214
            $statement->bindValue(":factor", $this->factor);
215
            $statement->bindValue(":data", $this->data);
216
            $statement->bindValue(":version", $this->version);
217
            $statement->bindValue(":timeout", $this->timeout);
218
            $statement->bindValue(":disabled", $this->disabled);
219
            $statement->bindValue(":priority", $this->priority);
220
221
            if (!$statement->execute()) {
222
                throw new Exception($statement->errorInfo());
223
            }
224
225
            if ($statement->rowCount() !== 1) {
226
                throw new OptimisticLockFailedException();
227
            }
228
229
            $this->updateversion++;
230
        }
231
    }
232
}