Permissions   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 8
Bugs 4 Features 1
Metric Value
c 8
b 4
f 1
dl 0
loc 49
rs 10
wmc 5
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get() 0 10 3
A set() 0 4 1
1
<?php
2
3
namespace Sovereign\Lib;
4
5
6
/**
7
 * Class Permissions
8
 * @package Sovereign\Lib
9
 */
10
class Permissions
11
{
12
    /**
13
     * @var Db
14
     */
15
    protected $db;
16
    /**
17
     * @var Config
18
     */
19
    protected $config;
20
21
    /**
22
     * Permissions constructor.
23
     * @param Db $db
24
     * @param Config $config
25
     */
26
    public function __construct(Db $db, Config $config)
27
    {
28
        $this->db = $db;
29
        $this->config = $config;
30
    }
31
32
    /**
33
     * @param $userID
34
     * @param $serverID
35
     * @return int|string
36
     */
37
    public function get($userID, $serverID)
38
    {
39
        foreach ($this->config->get("admins", "permissions") as $adminID) {
0 ignored issues
show
Bug introduced by
The expression $this->config->get('admins', 'permissions') of type null|string is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
40
            if ($adminID == $userID) {
41
                return 2;
42
            }
43
        }
44
45
        return $this->db->queryField("SELECT permission FROM permissions WHERE userID = :userID AND serverID = :serverID", "permission", [":userID" => $userID, ":serverID" => $serverID]);
46
    }
47
48
    /**
49
     * @param $userID
50
     * @param $serverID
51
     * @param $permission
52
     */
53
    public function set($userID, $serverID, $permission)
54
    {
55
        $this->db->execute("INSERT INTO permissions (serverID, userID, permission) VALUE (:serverID, :userID, :permission) ON DUPLICATE KEY UPDATE permission = :permission", [":serverID" => $serverID, ":userID" => $userID, ":permission" => $permission]);
56
    }
57
58
}