Completed
Push — newinternal ( 65a0f5...5b021c )
by Simon
08:29
created

UserRole::getForUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 18
loc 18
rs 9.6666
c 0
b 0
f 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 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 View Code Duplication
    public static function getForUser($userId, PdoDatabase $database)
0 ignored issues
show
Duplication introduced by
This method 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...
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 View Code Duplication
    public function save()
0 ignored issues
show
Duplication introduced by
This method 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...
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());
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