Completed
Push — feature/player-list ( ee00f1...3478af )
by Vladimir
04:05
created

AbstractMessage   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 72.72%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 7
dl 0
loc 105
ccs 16
cts 22
cp 0.7272
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getQueryBuilder() 0 10 1
A chooseModelFromDatabase() 0 21 4
A assignResult() 0 7 1
isMessage() 0 1 ?
A getActiveStatuses() 0 4 1
A getConversation() 0 4 1
1
<?php
2
3
/**
4
 * This file contains an abstract model class
5
 *
6
 * @package    BZiON\Models
7
 * @license    https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3
8
 */
9
use BZIon\Model\Column\Timestamp;
10
11
/**
12
 * An abstraction for conversation events
13
 * @package    BZiON\Models
14
 */
15
abstract class AbstractMessage extends Model
16
{
17
    use Timestamp;
18
19
    /**
20
     * The ID of the conversation where the event took place
21
     * @var int
22
     */
23
    protected $conversation;
24
25
    /**
26
     * The type of the event, or null if it's a message
27
     *
28
     * @var string|null
29
     */
30
    protected $type;
31
32
    /**
33
     * The status of the event
34
     *
35
     * Can be 'visible', 'hidden', 'deleted' or 'reported'
36
     * @var string
37
     */
38
    protected $status;
39
40
    /**
41
     * The name of the database table used for queries
42
     */
43
    const TABLE = "messages";
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 1
    protected function assignResult($event)
49
    {
50 1
        $this->conversation = $event['conversation_to'];
51 1
        $this->type = $event['event_type'];
52 1
        $this->timestamp = TimeDate::fromMysql($event['timestamp']);
53 1
        $this->status = $event['status'];
54 1
    }
55
56
    /**
57
     * Get the conversation where the event took place
58
     * @return Conversation
59
     */
60
    public function getConversation()
61
    {
62
        return Conversation::get($this->conversation);
63
    }
64
65
    /**
66
     * Find out whether the event is a message and not a generic conversation event
67
     * (such as a rename or member join)
68
     *
69
     * @return bool
70
     */
71
    abstract public function isMessage();
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 1
    public static function getActiveStatuses()
77
    {
78 1
        return array('visible', 'reported');
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 1
    public static function getQueryBuilder()
85
    {
86 1
        return new MessageQueryBuilder('AbstractMessage', array(
87 1
            'columns' => array(
88
                'conversation' => 'conversation_to',
89
                'time'         => 'timestamp',
90
                'status'       => 'status'
91
            )
92
        ));
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 1
    protected static function chooseModelFromDatabase($id)
99
    {
100 1
        $columns = static::fetchColumnValues($id);
101
102
        // Handle a non-existent model
103 1
        if ($columns === null) {
104
            if (get_called_class() === 'AbstractMessage') {
105
                // Default to returning an invalid Message
106
                return new Message($id, null);
107
            } else {
108
                return new static($id, null);
109
            }
110
        }
111
112
        // Determine whether the ID corresponds to a message or another event
113 1
        if ($columns['event_type'] === null) {
114 1
            return new Message($id, $columns);
115
        } else {
116
            return new ConversationEvent($id, $columns);
117
        }
118
    }
119
}
120