Failed Conditions
Pull Request — newinternal (#527)
by Simon
16:02 queued 05:59
created

Credential::save()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 52

Duplication

Lines 52
Ratio 100 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 0
dl 52
loc 52
rs 8.7361
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Exception;
12
use Waca\DataObject;
13
use Waca\Exceptions\OptimisticLockFailedException;
14
15 View Code Duplication
class Credential extends DataObject
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
16
{
17
    /** @var int */
18
    private $user;
19
    /** @var int */
20
    private $factor;
21
    /** @var string */
22
    private $type;
23
    /** @var string */
24
    private $data;
25
    /** @var int */
26
    private $version;
27
28
    /**
29
     * @return int
30
     */
31
    public function getUserId()
32
    {
33
        return $this->user;
34
    }
35
36
    /**
37
     * @param int $user
38
     */
39
    public function setUserId($user)
40
    {
41
        $this->user = $user;
42
    }
43
44
    /**
45
     * @return int
46
     */
47
    public function getFactor()
48
    {
49
        return $this->factor;
50
    }
51
52
    /**
53
     * @param int $factor
54
     */
55
    public function setFactor($factor)
56
    {
57
        $this->factor = $factor;
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    public function getType()
64
    {
65
        return $this->type;
66
    }
67
68
    /**
69
     * @param string $type
70
     */
71
    public function setType($type)
72
    {
73
        $this->type = $type;
74
    }
75
76
    /**
77
     * @return string
78
     */
79
    public function getData()
80
    {
81
        return $this->data;
82
    }
83
84
    /**
85
     * @param string $data
86
     */
87
    public function setData($data)
88
    {
89
        $this->data = $data;
90
    }
91
92
    /**
93
     * @return int
94
     */
95
    public function getVersion()
96
    {
97
        return $this->version;
98
    }
99
100
    /**
101
     * @param int $version
102
     */
103
    public function setVersion($version)
104
    {
105
        $this->version = $version;
106
    }
107
108
    public function save()
109
    {
110
        if ($this->isNew()) {
111
            // insert
112
            $statement = $this->dbObject->prepare(<<<SQL
113
INSERT INTO credential ( updateversion, user, factor, type, data, version )
114
VALUES ( 0, :user, :factor, :type, :data, :version );
115
SQL
116
            );
117
            $statement->bindValue(":user", $this->user);
118
            $statement->bindValue(":factor", $this->factor);
119
            $statement->bindValue(":type", $this->type);
120
            $statement->bindValue(":data", $this->data);
121
            $statement->bindValue(":version", $this->version);
122
123
            if ($statement->execute()) {
124
                $this->id = (int)$this->dbObject->lastInsertId();
125
            }
126
            else {
127
                throw new Exception($statement->errorInfo());
128
            }
129
        }
130
        else {
131
            // update
132
            $statement = $this->dbObject->prepare(<<<SQL
133
                UPDATE credential
134
                SET   factor = :factor
135
                    , data = :data
136
                    , version = :version                 
137
                    , updateversion = updateversion + 1
138
                WHERE id = :id AND updateversion = :updateversion;
139
SQL
140
            );
141
142
            $statement->bindValue(':id', $this->id);
143
            $statement->bindValue(':updateversion', $this->updateversion);
144
145
            $statement->bindValue(":factor", $this->factor);
146
            $statement->bindValue(":data", $this->data);
147
            $statement->bindValue(":version", $this->version);
148
149
            if (!$statement->execute()) {
150
                throw new Exception($statement->errorInfo());
151
            }
152
153
            if ($statement->rowCount() !== 1) {
154
                throw new OptimisticLockFailedException();
155
            }
156
157
            $this->updateversion++;
158
        }
159
    }
160
}