Skip to content

Commit 473ded6

Browse files
Fix between scope (#359)
* - Added scope betweenOnly() to get private conversations between only passed participants. - Added test case for betweenOnly() scope. - Code refactor. - Fix typos. * - Fix CS. * - Rewrite the betweenOnly() scope sql statement to work on `PostgreSQL` as well. * Fix styling * - Fix namespace. * - Fix factories. * Fix styling * - Added `betweenOnly()` scope test cases. - Added extra test case for `between()` scope. Co-authored-by: AbdullahFaqeir <AbdullahFaqeir@users.noreply.github.com>
1 parent c27c457 commit 473ded6

File tree

7 files changed

+162
-65
lines changed

7 files changed

+162
-65
lines changed

src/MessengerServiceProvider.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class MessengerServiceProvider extends ServiceProvider
1515
*
1616
* @return void
1717
*/
18-
public function boot()
18+
public function boot(): void
1919
{
2020
$this->offerPublishing();
2121
$this->setMessengerModels();
@@ -27,7 +27,7 @@ public function boot()
2727
*
2828
* @return void
2929
*/
30-
public function register()
30+
public function register(): void
3131
{
3232
$this->configure();
3333
}
@@ -37,7 +37,7 @@ public function register()
3737
*
3838
* @return void
3939
*/
40-
protected function configure()
40+
protected function configure(): void
4141
{
4242
$this->mergeConfigFrom(
4343
__DIR__ . '/../config/config.php',
@@ -50,7 +50,7 @@ protected function configure()
5050
*
5151
* @return void
5252
*/
53-
protected function offerPublishing()
53+
protected function offerPublishing(): void
5454
{
5555
if ($this->app->runningInConsole()) {
5656
$this->publishes([
@@ -67,8 +67,9 @@ protected function offerPublishing()
6767
* Define Messenger's models in registry.
6868
*
6969
* @return void
70+
* @throws \Illuminate\Contracts\Container\BindingResolutionException
7071
*/
71-
protected function setMessengerModels()
72+
protected function setMessengerModels(): void
7273
{
7374
$config = $this->app->make('config');
7475

@@ -87,8 +88,9 @@ protected function setMessengerModels()
8788
* Define User model in Messenger's model registry.
8889
*
8990
* @return void
91+
* @throws \Illuminate\Contracts\Container\BindingResolutionException
9092
*/
91-
protected function setUserModel()
93+
protected function setUserModel(): void
9294
{
9395
$config = $this->app->make('config');
9496

src/Models/Message.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@
44

55
use Illuminate\Database\Eloquent\Builder;
66
use Illuminate\Database\Eloquent\Model as Eloquent;
7+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
8+
use Illuminate\Database\Eloquent\Relations\HasMany;
79
use Illuminate\Database\Eloquent\SoftDeletes;
810

11+
/**
12+
* Class Message.
13+
*
14+
* @method static Builder|self unreadForUser(int $userId)
15+
*/
916
class Message extends Eloquent
1017
{
1118
use SoftDeletes;
@@ -32,7 +39,7 @@ class Message extends Eloquent
3239
protected $fillable = ['thread_id', 'user_id', 'body'];
3340

3441
/**
35-
* The attributes that should be mutated to dates.
42+
* The attributes that should be mutated to date's.
3643
*
3744
* @var array
3845
*/
@@ -55,7 +62,7 @@ public function __construct(array $attributes = [])
5562
*
5663
* @codeCoverageIgnore
5764
*/
58-
public function thread()
65+
public function thread(): BelongsTo
5966
{
6067
return $this->belongsTo(Models::classname(Thread::class), 'thread_id', 'id');
6168
}
@@ -67,7 +74,7 @@ public function thread()
6774
*
6875
* @codeCoverageIgnore
6976
*/
70-
public function user()
77+
public function user(): BelongsTo
7178
{
7279
return $this->belongsTo(Models::user(), 'user_id');
7380
}
@@ -79,7 +86,7 @@ public function user()
7986
*
8087
* @codeCoverageIgnore
8188
*/
82-
public function participants()
89+
public function participants(): HasMany
8390
{
8491
return $this->hasMany(Models::classname(Participant::class), 'thread_id', 'thread_id');
8592
}
@@ -89,7 +96,7 @@ public function participants()
8996
*
9097
* @return \Illuminate\Database\Eloquent\Relations\HasMany
9198
*/
92-
public function recipients()
99+
public function recipients(): HasMany
93100
{
94101
return $this->participants()->where('user_id', '!=', $this->user_id);
95102
}
@@ -101,7 +108,7 @@ public function recipients()
101108
* @param int $userId
102109
* @return \Illuminate\Database\Eloquent\Builder
103110
*/
104-
public function scopeUnreadForUser(Builder $query, $userId)
111+
public function scopeUnreadForUser(Builder $query, int $userId): Builder
105112
{
106113
return $query->has('thread')
107114
->where('user_id', '!=', $userId)

src/Models/Models.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Cmgmyr\Messenger\Models;
44

5+
use Illuminate\Database\Eloquent\Model;
6+
57
class Models
68
{
79
/**
@@ -30,7 +32,7 @@ class Models
3032
*
3133
* @param string $model
3234
*/
33-
public static function setMessageModel($model)
35+
public static function setMessageModel(string $model): void
3436
{
3537
static::$models[Message::class] = $model;
3638
}
@@ -40,7 +42,7 @@ public static function setMessageModel($model)
4042
*
4143
* @param string $model
4244
*/
43-
public static function setParticipantModel($model)
45+
public static function setParticipantModel(string $model): void
4446
{
4547
static::$models[Participant::class] = $model;
4648
}
@@ -50,7 +52,7 @@ public static function setParticipantModel($model)
5052
*
5153
* @param string $model
5254
*/
53-
public static function setThreadModel($model)
55+
public static function setThreadModel(string $model): void
5456
{
5557
static::$models[Thread::class] = $model;
5658
}
@@ -60,7 +62,7 @@ public static function setThreadModel($model)
6062
*
6163
* @param string $model
6264
*/
63-
public static function setUserModel($model)
65+
public static function setUserModel(string $model): void
6466
{
6567
static::$models[self::$userModelLookupKey] = $model;
6668
}
@@ -70,7 +72,7 @@ public static function setUserModel($model)
7072
*
7173
* @param array $map
7274
*/
73-
public static function setTables(array $map)
75+
public static function setTables(array $map): void
7476
{
7577
static::$tables = array_merge(static::$tables, $map);
7678
}
@@ -81,7 +83,7 @@ public static function setTables(array $map)
8183
* @param string $table
8284
* @return string
8385
*/
84-
public static function table($table)
86+
public static function table(string $table): string
8587
{
8688
return static::$tables[$table] ?? $table;
8789
}
@@ -92,7 +94,7 @@ public static function table($table)
9294
* @param string $model
9395
* @return string
9496
*/
95-
public static function classname($model)
97+
public static function classname(string $model): string
9698
{
9799
return static::$models[$model] ?? $model;
98100
}
@@ -103,7 +105,7 @@ public static function classname($model)
103105
* @param array $attributes
104106
* @return \Cmgmyr\Messenger\Models\Message
105107
*/
106-
public static function message(array $attributes = [])
108+
public static function message(array $attributes = []): Message
107109
{
108110
return static::make(Message::class, $attributes);
109111
}
@@ -114,7 +116,7 @@ public static function message(array $attributes = [])
114116
* @param array $attributes
115117
* @return \Cmgmyr\Messenger\Models\Participant
116118
*/
117-
public static function participant(array $attributes = [])
119+
public static function participant(array $attributes = []): Participant
118120
{
119121
return static::make(Participant::class, $attributes);
120122
}
@@ -125,7 +127,7 @@ public static function participant(array $attributes = [])
125127
* @param array $attributes
126128
* @return \Cmgmyr\Messenger\Models\Thread
127129
*/
128-
public static function thread(array $attributes = [])
130+
public static function thread(array $attributes = []): Thread
129131
{
130132
return static::make(Thread::class, $attributes);
131133
}
@@ -136,7 +138,7 @@ public static function thread(array $attributes = [])
136138
* @param array $attributes
137139
* @return \Illuminate\Database\Eloquent\Model
138140
*/
139-
public static function user(array $attributes = [])
141+
public static function user(array $attributes = []): Model
140142
{
141143
return static::make(self::$userModelLookupKey, $attributes);
142144
}
@@ -148,7 +150,7 @@ public static function user(array $attributes = [])
148150
* @param array $attributes
149151
* @return \Illuminate\Database\Eloquent\Model
150152
*/
151-
protected static function make($model, array $attributes = [])
153+
protected static function make(string $model, array $attributes = []): Model
152154
{
153155
$model = static::classname($model);
154156

src/Models/Participant.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Cmgmyr\Messenger\Models;
44

55
use Illuminate\Database\Eloquent\Model as Eloquent;
6+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
67
use Illuminate\Database\Eloquent\SoftDeletes;
78

89
class Participant extends Eloquent
@@ -24,7 +25,7 @@ class Participant extends Eloquent
2425
protected $fillable = ['thread_id', 'user_id', 'last_read'];
2526

2627
/**
27-
* The attributes that should be mutated to dates.
28+
* The attributes that should be mutated to date's.
2829
*
2930
* @var array
3031
*/
@@ -47,7 +48,7 @@ public function __construct(array $attributes = [])
4748
*
4849
* @codeCoverageIgnore
4950
*/
50-
public function thread()
51+
public function thread(): BelongsTo
5152
{
5253
return $this->belongsTo(Models::classname(Thread::class), 'thread_id', 'id');
5354
}
@@ -59,7 +60,7 @@ public function thread()
5960
*
6061
* @codeCoverageIgnore
6162
*/
62-
public function user()
63+
public function user(): BelongsTo
6364
{
6465
return $this->belongsTo(Models::user(), 'user_id');
6566
}

0 commit comments

Comments
 (0)