Models::setMessageModel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Transmissor\Models\Messenger;
4
5
class Models
6
{
7
    /**
8
     * Map for the messenger's models.
9
     *
10
     * @var array
11
     */
12
    protected static $models = [];
13
14
    /**
15
     * Map for the messenger's tables.
16
     *
17
     * @var array
18
     */
19
    protected static $tables = [];
20
21
    /**
22
     * Internal pointer name for the app's "user" model.
23
     *
24
     * @var string
25
     */
26
    private static $userModelLookupKey = 'User';
27
28
    /**
29
     * Set the model to be used for threads.
30
     *
31
     * @param string $model
32
     */
33
    public static function setMessageModel($model)
34
    {
35
        static::$models[Message::class] = $model;
36
    }
37
38
    /**
39
     * Set the model to be used for participants.
40
     *
41
     * @param string $model
42
     */
43
    public static function setParticipantModel($model)
44
    {
45
        static::$models[Participant::class] = $model;
46
    }
47
48
    /**
49
     * Set the model to be used for threads.
50
     *
51
     * @param string $model
52
     */
53
    public static function setThreadModel($model)
54
    {
55
        static::$models[Thread::class] = $model;
56
    }
57
58
    /**
59
     * Set the model to be used for users.
60
     *
61
     * @param string $model
62
     */
63
    public static function setUserModel($model)
64
    {
65
        static::$models[self::$userModelLookupKey] = $model;
66
    }
67
68
    /**
69
     * Set custom table names.
70
     *
71
     * @param array $map
72
     */
73
    public static function setTables(array $map)
74
    {
75
        static::$tables = array_merge(static::$tables, $map);
76
    }
77
78
    /**
79
     * Get a custom table name mapping for the given table.
80
     *
81
     * @param  string $table
82
     * @return string
83
     */
84
    public static function table($table)
85
    {
86
        return static::$tables[$table] ?? $table;
87
    }
88
89
    /**
90
     * Get the class name mapping for the given model.
91
     *
92
     * @param  string $model
93
     * @return string
94
     */
95
    public static function classname($model)
96
    {
97
        return static::$models[$model] ?? $model;
98
    }
99
100
    /**
101
     * Get an instance of the messages model.
102
     *
103
     * @param  array $attributes
104
     * @return \Transmissor\Models\Messenger\Message
105
     */
106
    public static function message(array $attributes = [])
107
    {
108
        return static::make(Message::class, $attributes);
109
    }
110
111
    /**
112
     * Get an instance of the participants model.
113
     *
114
     * @param  array $attributes
115
     * @return \Transmissor\Models\Messenger\Participant
116
     */
117
    public static function participant(array $attributes = [])
118
    {
119
        return static::make(Participant::class, $attributes);
120
    }
121
122
    /**
123
     * Get an instance of the threads model.
124
     *
125
     * @param  array $attributes
126
     * @return \Transmissor\Models\Messenger\Thread
127
     */
128
    public static function thread(array $attributes = [])
129
    {
130
        return static::make(Thread::class, $attributes);
131
    }
132
133
    /**
134
     * Get an instance of the user model.
135
     *
136
     * @param  array $attributes
137
     * @return \Illuminate\Database\Eloquent\Model
138
     */
139
    public static function user(array $attributes = [])
140
    {
141
        return static::make(self::$userModelLookupKey, $attributes);
142
    }
143
144
    /**
145
     * Get an instance of the given model.
146
     *
147
     * @param  string $model
148
     * @param  array  $attributes
149
     * @return \Illuminate\Database\Eloquent\Model
150
     */
151
    protected static function make($model, array $attributes = [])
152
    {
153
        $model = static::classname($model);
154
155
        return new $model($attributes);
156
    }
157
}
158