Completed
Push — fm-support ( 0de470...22a3ee )
by Konstantinos
04:53
created

ApiKey   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 2
cbo 2
dl 0
loc 91
ccs 21
cts 21
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A assignResult() 0 7 1
A getKeyByOwner() 0 10 2
A getKey() 0 4 1
A getName() 0 4 1
A getOwner() 0 4 1
A getStatus() 0 4 1
A generateKey() 0 10 1
A createKey() 0 11 1
A getKeys() 0 10 2
1
<?php
2
3
class ApiKey extends Model
4
{
5
    protected $name;
6
7
    protected $owner;
8
9
    protected $key;
10
11
    protected $status;
12
13
    /**
14
     * The name of the database table used for queries
15
     */
16
    const TABLE = "api_keys";
17
18
    /**
19
     * {@inheritdoc}
20
     */
21 1
    protected function assignResult($key)
22
    {
23 1
        $this->name   = $key['name'];
24 1
        $this->owner  = Player::get($key['owner']);
25 1
        $this->key    = $key['key'];
26 1
        $this->status = $key['status'];
27 1
    }
28
29 1
    public static function createKey($name, $owner)
30
    {
31 1
        $key = self::create(array(
32 1
            'name'   => $name,
33 1
            'owner'  => $owner,
34 1
            'key'    => self::generateKey(),
35 1
            'status' => "active"
36
        ));
37
38 1
        return $key;
39
    }
40
41
    public static function getKeyByOwner($owner)
42
    {
43
        $key = self::fetchIdFrom($owner, "owner");
44
45
        if ($key == null) {
46
            return self::createKey("Automatically generated key", $owner);
47
        }
48
49
        return self::get($key);
50
    }
51
52
    public function getKey()
53
    {
54
        return $this->key;
55
    }
56
57
    public function getName()
58
    {
59
        return $this->name;
60
    }
61
62 1
    public function getOwner()
63
    {
64 1
        return $this->owner;
65
    }
66
67
    public function getStatus()
68
    {
69
        return $this->status;
70
    }
71
72
    public static function getKeys($owner = -1)
73
    {
74
        if ($owner > 0) {
75
            $ids = self::fetchIdsFrom("owner", array($owner), false, "WHERE status = 'active'");
76
            return self::arrayIdToModel($ids);
77
        }
78
79
        $ids = self::fetchIdsFrom("status", array("active"));
80
        return self::arrayIdToModel($ids);
81
    }
82
83 1
    protected static function generateKey()
84
    {
85 1
        list($usec, $sec) = explode(' ', microtime());
86 1
        $seed = (float) $sec + ((float) $usec * 100000);
87 1
        srand($seed);
88
89 1
        $key = rand();
90
91 1
        return substr(sha1($key), 24);
92
    }
93
}
94