Completed
Push — rbac ( f06361...8f6a08 )
by Michael
05:58 queued 16s
created

Comment::save()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 46
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 29
c 0
b 0
f 0
dl 0
loc 46
rs 9.1448
cc 5
nc 5
nop 0
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 PDO;
14
use Waca\DataObject;
15
use Waca\Exceptions\OptimisticLockFailedException;
16
use Waca\PdoDatabase;
17
18
/**
19
 * Comment data object
20
 */
21
class Comment extends DataObject
22
{
23
    private $time;
24
    private $user;
25
    private $comment;
26
    private $visibility = "user";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal user does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
27
    private $request;
28
29
    /**
30
     * Retrieves all comments for a request, optionally filtered
31
     *
32
     * @param integer     $id      Request ID to search by
33
     * @param PdoDatabase $database
34
     * @param bool        $showAll True to show all comments, False to show only unprotected comments, and protected
35
     *                             comments visible to $userId
36
     * @param null|int    $userId  User to filter by
37
     *
38
     * @return Comment[]
39
     */
40
    public static function getForRequest($id, PdoDatabase $database, $showAll = false, $userId = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$showAll" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$showAll"; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$userId" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$userId"; expected 0 but found 1
Loading history...
41
    {
42
        if ($showAll) {
43
            $statement = $database->prepare('SELECT * FROM comment WHERE request = :target;');
44
        }
45
        else {
46
            $statement = $database->prepare(<<<SQL
47
SELECT * FROM comment
48
WHERE request = :target AND (visibility = 'user' OR user = :userid);
49
SQL
50
            );
51
            $statement->bindValue(':userid', $userId);
52
        }
53
54
        $statement->bindValue(':target', $id);
55
56
        $statement->execute();
57
58
        $result = array();
59
        /** @var Comment $v */
60
        foreach ($statement->fetchAll(PDO::FETCH_CLASS, get_called_class()) as $v) {
61
            $v->setDatabase($database);
62
            $result[] = $v;
63
        }
64
65
        return $result;
66
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getForRequest()
Loading history...
67
68
    /**
69
     * @throws Exception
70
     */
0 ignored issues
show
Coding Style introduced by
Expected 2 @throws tag(s) in function comment; 1 found
Loading history...
71
    public function save()
72
    {
73
        if ($this->isNew()) {
74
            // insert
75
            $statement = $this->dbObject->prepare(<<<SQL
76
INSERT INTO comment ( time, user, comment, visibility, request )
77
VALUES ( CURRENT_TIMESTAMP(), :user, :comment, :visibility, :request );
78
SQL
79
            );
80
            $statement->bindValue(":user", $this->user);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal :user does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
81
            $statement->bindValue(":comment", $this->comment);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal :comment does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
82
            $statement->bindValue(":visibility", $this->visibility);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal :visibility does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
83
            $statement->bindValue(":request", $this->request);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal :request does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
84
85
            if ($statement->execute()) {
86
                $this->id = (int)$this->dbObject->lastInsertId();
87
            }
88
            else {
89
                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

89
                throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
90
            }
91
        }
92
        else {
93
            // update
94
            $statement = $this->dbObject->prepare(<<<SQL
95
UPDATE comment
96
SET comment = :comment, visibility = :visibility, updateversion = updateversion + 1
97
WHERE id = :id AND updateversion = :updateversion
98
LIMIT 1;
99
SQL
100
            );
101
102
            $statement->bindValue(':id', $this->id);
103
            $statement->bindValue(':updateversion', $this->updateversion);
104
105
            $statement->bindValue(':comment', $this->comment);
106
            $statement->bindValue(':visibility', $this->visibility);
107
108
            if (!$statement->execute()) {
109
                throw new Exception($statement->errorInfo());
110
            }
111
112
            if ($statement->rowCount() !== 1) {
113
                throw new OptimisticLockFailedException();
114
            }
115
116
            $this->updateversion++;
117
        }
118
    }
0 ignored issues
show
Coding Style introduced by
Expected //end save()
Loading history...
119
120
    /**
121
     * @return DateTimeImmutable
122
     */
123
    public function getTime()
124
    {
125
        return new DateTimeImmutable($this->time);
126
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getTime()
Loading history...
127
128
    /**
129
     * @return int
130
     */
131
    public function getUser()
132
    {
133
        return $this->user;
134
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getUser()
Loading history...
135
136
    /**
137
     * @param int $user
138
     */
139
    public function setUser($user)
140
    {
141
        $this->user = $user;
142
    }
0 ignored issues
show
Coding Style introduced by
Expected //end setUser()
Loading history...
143
144
    /**
145
     * @return string
146
     */
147
    public function getComment()
148
    {
149
        return $this->comment;
150
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getComment()
Loading history...
151
152
    /**
153
     * @param string $comment
154
     */
155
    public function setComment($comment)
156
    {
157
        $this->comment = $comment;
158
    }
0 ignored issues
show
Coding Style introduced by
Expected //end setComment()
Loading history...
159
160
    /**
161
     * @return string
162
     */
163
    public function getVisibility()
164
    {
165
        return $this->visibility;
166
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getVisibility()
Loading history...
167
168
    /**
169
     * @param string $visibility
170
     */
171
    public function setVisibility($visibility)
172
    {
173
        $this->visibility = $visibility;
174
    }
0 ignored issues
show
Coding Style introduced by
Expected //end setVisibility()
Loading history...
175
176
    /**
177
     * @return int
178
     */
179
    public function getRequest()
180
    {
181
        return $this->request;
182
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getRequest()
Loading history...
183
184
    /**
185
     * @param int $request
186
     */
187
    public function setRequest($request)
188
    {
189
        $this->request = $request;
190
    }
0 ignored issues
show
Coding Style introduced by
Expected //end setRequest()
Loading history...
191
}
0 ignored issues
show
Coding Style introduced by
Expected //end class
Loading history...
192