Issues (195)

includes/DataObjects/UserRole.php (1 issue)

Labels
Severity
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 PDO;
13
use Waca\DataObject;
14
use Waca\PdoDatabase;
15
16
class UserRole extends DataObject
17
{
18
    /** @var int */
19
    private $user;
20
    /** @var string */
21
    private $role;
22
23
    /**
24
     * @param int         $userId
25
     * @param PdoDatabase $database
26
     *
27
     * @return UserRole[]
28
     */
29
    public static function getForUser($userId, PdoDatabase $database)
30
    {
31
        $sql = 'SELECT * FROM userrole WHERE user = :user';
32
        $statement = $database->prepare($sql);
33
        $statement->bindValue(':user', $userId);
34
35
        $statement->execute();
36
37
        $result = array();
38
39
        /** @var Ban $v */
40
        foreach ($statement->fetchAll(PDO::FETCH_CLASS, get_called_class()) as $v) {
41
            $v->setDatabase($database);
42
            $result[] = $v;
43
        }
44
45
        return $result;
46
    }
47
48
    /**
49
     * Saves a data object to the database, either updating or inserting a record.
50
     *
51
     * @throws Exception
52
     */
53
    public function save()
54
    {
55
        if ($this->isNew()) {
56
            // insert
57
            $statement = $this->dbObject->prepare('INSERT INTO `userrole` (user, role) VALUES (:user, :role);'
58
            );
59
            $statement->bindValue(":user", $this->user);
60
            $statement->bindValue(":role", $this->role);
61
62
            if ($statement->execute()) {
63
                $this->id = (int)$this->dbObject->lastInsertId();
64
            }
65
            else {
66
                throw new Exception($statement->errorInfo());
0 ignored issues
show
$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

66
                throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
67
            }
68
        }
69
        else {
70
            // update
71
            throw new Exception('Updating roles is not available');
72
        }
73
    }
74
75
    #region Properties
76
77
    /**
78
     * @return int
79
     */
80
    public function getUser()
81
    {
82
        return $this->user;
83
    }
84
85
    /**
86
     * @param int $user
87
     */
88
    public function setUser($user)
89
    {
90
        $this->user = $user;
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function getRole()
97
    {
98
        return $this->role;
99
    }
100
101
    /**
102
     * @param string $role
103
     */
104
    public function setRole($role)
105
    {
106
        $this->role = $role;
107
    }
108
    #endregion
109
}
110