Completed
Branch develop-3.0 (48060b)
by Mohamed
02:47
created

Queue::updater()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Model\Message;
13
14
use Illuminate\Database\Eloquent\Model;
15
use Tinyissue\Model\ModelAbstract;
16
use Tinyissue\Model\Project;
17
use Tinyissue\Model\Project\Issue;
18
use Tinyissue\Model\Project\Issue\Comment;
19
use Tinyissue\Model\Project\Note;
20
use Tinyissue\Model\User;
21
22
/**
23
 * Queue is model class for message queue.
24
 *
25
 * @author Mohamed Alsharaf <[email protected]>
26
 *
27
 * @property int $id
28
 * @property string $event
29
 * @property id $model_id
30
 * @property string $model_type
31
 * @property array $payload
32
 * @property id $change_by_id
33
 * @property User $changeBy
34
 * @property int $created_at
35
 * @property Issue|Comment|Note|Project $model
36
 *
37
 * @method Collection getLatestMessages()
38
 * @method $this latest()
39
 */
40
class Queue extends ModelAbstract
41
{
42
    use QueueRelations, QueueScopes;
43
44
    // List of available events
45
    const ADD_ISSUE             = 'add_issue';
46
    const CHANGE_TAG_ISSUE      = 'change_tag_issue';
47
    const UPDATE_ISSUE          = 'update_issue';
48
    const ASSIGN_ISSUE          = 'assign_issue';
49
    const CLOSE_ISSUE           = 'close_issue';
50
    const REOPEN_ISSUE          = 'reopen_issue';
51
    const ADD_COMMENT           = 'add_comment';
52
    const UPDATE_COMMENT        = 'update_comment';
53
    const DELETE_COMMENT        = 'delete_comment';
54
    const ADD_NOTE              = 'add_note';
55
    const UPDATE_NOTE           = 'update_note';
56
    const DELETE_NOTE           = 'delete_note';
57
    const MESSAGE_IN_ALL_ISSUES = 'in_all_issues';
58
59
    protected static $ADD_EVENTS = [
60
        Issue::class   => self::ADD_ISSUE,
61
        Comment::class => self::ADD_COMMENT,
62
        Note::class    => self::ADD_NOTE,
63
    ];
64
65
    /**
66
     * Timestamp disabled.
67
     *
68
     * @var bool
69
     */
70
    public $timestamps = true;
71
72
    /**
73
     * List of allowed columns to be used in $this->fill().
74
     *
75
     * @var array
76
     */
77
    public $fillable = ['event', 'payload', 'model_id', 'model_type', 'change_by_id'];
78
79
    /**
80
     * Name of database table.
81
     *
82
     * @var string
83
     */
84
    protected $table = 'messages_queue';
85
86
    /**
87
     * List of columns and their cast data-type.
88
     *
89
     * @var array
90
     */
91
    protected $casts = [
92
        'payload' => 'array',
93
    ];
94
95
    /**
96
     * @param User|null $user
97
     *
98
     * @return \Tinyissue\Repository\Message\Queue\Updater
99
     */
100
    public function updater(User $user = null)
101
    {
102
        return parent::updater($user);
103
    }
104
105
    /**
106
     * Get an item from a payload using "dot" notation.
107
     *
108
     * @param string|array $key
109
     *
110
     * @return mixed
111
     */
112
    public function getDataFromPayload($key)
113
    {
114
        return data_get($this->payload, $key, '');
115
    }
116
117
    /**
118
     * Get add event from a model object.
119
     *
120
     * @param Model $model
121
     *
122
     * @return mixed
123
     */
124
    public static function getAddEventNameFromModel(Model $model)
125
    {
126
        return self::$ADD_EVENTS[get_class($model)];
127
    }
128
129
    /**
130
     * Get an array of all of the available events.
131
     *
132
     * @return array
133
     */
134
    public static function getEventsNames()
135
    {
136
        return [
137
            self::ADD_ISSUE,
138
            self::CHANGE_TAG_ISSUE,
139
            self::UPDATE_ISSUE,
140
            self::ASSIGN_ISSUE,
141
            self::CLOSE_ISSUE,
142
            self::REOPEN_ISSUE,
143
            self::ADD_COMMENT,
144
            self::UPDATE_COMMENT,
145
            self::DELETE_COMMENT,
146
            self::ADD_NOTE,
147
            self::UPDATE_NOTE,
148
            self::DELETE_NOTE,
149
        ];
150
    }
151
}
152