Skip to content

Commit

Permalink
Cleaning the package
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanedev-maroc committed Feb 26, 2018
1 parent bafcce7 commit c849958
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 154 deletions.
3 changes: 3 additions & 0 deletions config/laravel-messenger.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

'database' => [
'connection' => env('DB_CONNECTION', 'mysql'),

'prefix' => null,
],

/* -----------------------------------------------------------------
Expand All @@ -19,6 +21,7 @@
'users' => [
'table' => 'users',
'model' => App\User::class,
'morph' => 'participable',
],

'discussions' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct()
parent::__construct();

$this->setTable(
$this->getTableFromConfig('discussions', 'discussions')
config('laravel-messenger.discussions.table', 'discussions')
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct()
parent::__construct();

$this->setTable(
$this->getTableFromConfig('messages', 'messages')
config('laravel-messenger.messages.table', 'messages')
);
}

Expand All @@ -42,7 +42,7 @@ public function up()
$this->createSchema(function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('discussion_id');
$table->morphs('participable');
$table->morphs(config("laravel-messenger.users.morph", 'participable'));
$table->text('body');
$table->timestamps();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct()
parent::__construct();

$this->setTable(
$this->getTableFromConfig('participations', 'participations')
config('laravel-messenger.participations.table', 'participations')
);
}

Expand All @@ -42,7 +42,7 @@ public function up()
$this->createSchema(function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('discussion_id');
$table->morphs('participable');
$table->morphs(config("laravel-messenger.users.morph", 'participable'));
$table->timestamp('last_read')->nullable();
$table->timestamps();
$table->softDeletes();
Expand Down
12 changes: 4 additions & 8 deletions src/Bases/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@
*/
abstract class Migration extends BaseMigration
{
/* -----------------------------------------------------------------
| Traits
| -----------------------------------------------------------------
*/

use ConfigHelper;

/* -----------------------------------------------------------------
| Constructor
| -----------------------------------------------------------------
Expand All @@ -29,7 +22,10 @@ abstract class Migration extends BaseMigration
public function __construct()
{
$this->setConnection(
$this->getFromConfig('database.connection')
config('laravel-messenger.database.connection')
);
$this->setPrefix(
config('laravel-messenger.database.prefix')
);
}
}
75 changes: 42 additions & 33 deletions src/Models/Discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
* @property \Illuminate\Database\Eloquent\Collection participations
* @property \Arcanedev\LaravelMessenger\Models\Message latest_message
*
* @method static \Illuminate\Database\Eloquent\Builder|static subject(string $subject, bool $strict)
* @method static \Illuminate\Database\Eloquent\Builder|static between(\Illuminate\Support\Collection|array $participables)
* @method static \Illuminate\Database\Eloquent\Builder|static forUser(\Illuminate\Database\Eloquent\Model $participable)
* @method static \Illuminate\Database\Eloquent\Builder|static withParticipations()
* @method static \Illuminate\Database\Eloquent\Builder|static forUserWithNewMessages(\Illuminate\Database\Eloquent\Model $participable)
* @method static \Illuminate\Database\Eloquent\Builder|static subject(string $subject, bool $strict)
* @method static \Illuminate\Database\Eloquent\Builder|static between(\Illuminate\Support\Collection|array $participables)
* @method static \Illuminate\Database\Eloquent\Builder|static forUser(\Illuminate\Database\Eloquent\Model $participable)
* @method static \Illuminate\Database\Eloquent\Builder|static withParticipations()
* @method static \Illuminate\Database\Eloquent\Builder|static forUserWithNewMessages(\Illuminate\Database\Eloquent\Model $participable)
*/
class Discussion extends Model implements DiscussionContract
{
Expand Down Expand Up @@ -83,7 +83,7 @@ class Discussion extends Model implements DiscussionContract
public function __construct(array $attributes = [])
{
$this->setTable(
$this->getTableFromConfig('discussions', 'discussions')
config('laravel-messenger.discussions.table', 'discussions')
);

parent::__construct($attributes);
Expand All @@ -102,7 +102,7 @@ public function __construct(array $attributes = [])
public function participations()
{
return $this->hasMany(
$this->getModelFromConfig('participations', Participation::class)
config('laravel-messenger.participations.model', Participation::class)
);
}

Expand All @@ -114,7 +114,7 @@ public function participations()
public function messages()
{
return $this->hasMany(
$this->getModelFromConfig('messages', Participation::class)
config('laravel-messenger.messages.model', Message::class)
);
}

Expand All @@ -139,16 +139,18 @@ public function creator()
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Model $participable
*
* @return \Illuminate\Database\Eloquent\Builder
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public function scopeForUser(Builder $query, EloquentModel $participable)
{
$table = $this->getParticipationsTable();

return $query->join($table, function (JoinClause $join) use ($table, $participable) {
$morph = config('laravel-messenger.users.morph', 'participable');

$join->on($this->getQualifiedKeyName(), '=', "{$table}.discussion_id")
->where("{$table}.participable_type", '=', $participable->getMorphClass())
->where("{$table}.participable_id", '=', $participable->getKey())
->where("{$table}.{$morph}_type", '=', $participable->getMorphClass())
->where("{$table}.{$morph}_id", '=', $participable->getKey())
->whereNull("{$table}.deleted_at");
});
}
Expand All @@ -158,7 +160,7 @@ public function scopeForUser(Builder $query, EloquentModel $participable)
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public function scopeWithParticipations(Builder $query)
{
Expand All @@ -171,17 +173,18 @@ public function scopeWithParticipations(Builder $query)
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Model $participable
*
* @return \Illuminate\Database\Eloquent\Builder
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public function scopeForUserWithNewMessages(Builder $query, EloquentModel $participable)
{
$prefix = $this->getConnection()->getTablePrefix();
$prefix = $this->getConnection()->getTablePrefix();
$participations = $this->getParticipationsTable();
$discussions = $this->getTable();
$discussions = $this->getTable();

return $this->scopeForUser($query, $participable)
->where(function (Builder $query) use ($participations, $discussions, $prefix) {
$expression = $this->getConnection()->raw("{$prefix}{$participations}.last_read");

$query->where("{$discussions}.updated_at", '>', $expression)
->orWhereNull("{$participations}.last_read");
});
Expand All @@ -193,18 +196,19 @@ public function scopeForUserWithNewMessages(Builder $query, EloquentModel $parti
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Support\Collection|array $participables
*
* @return \Illuminate\Database\Eloquent\Builder
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public function scopeBetween(Builder $query, $participables)
{
return $query->whereHas($this->getParticipationsTable(), function (Builder $query) use ($participables) {
$morph = config('laravel-messenger.users.morph', 'participable');
$index = 0;

foreach ($participables as $participable) {
/** @var \Illuminate\Database\Eloquent\Model $participable */
$clause = [
['participable_type', '=', $participable->getMorphClass()],
['participable_id', '=', $participable->getKey()],
["{$morph}_type", '=', $participable->getMorphClass()],
["{$morph}_id", '=', $participable->getKey()],
];

$query->where($clause, null, null, $index === 0 ? 'and' : 'or');
Expand All @@ -224,7 +228,7 @@ public function scopeBetween(Builder $query, $participables)
* @param string $subject
* @param bool $strict
*
* @return \Illuminate\Database\Eloquent\Builder
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public function scopeSubject(Builder $query, $subject, $strict = false)
{
Expand Down Expand Up @@ -253,7 +257,7 @@ public function getLatestMessageAttribute()
*/
protected function getParticipationsTable()
{
return $this->getTableFromConfig('participations', 'participations');
return config('laravel-messenger.participations.table', 'participations');
}

/* -----------------------------------------------------------------
Expand Down Expand Up @@ -311,10 +315,12 @@ public function getParticipables()
*/
public function addParticipant(EloquentModel $participable)
{
$morph = config('laravel-messenger.users.morph', 'participable');

return $this->participations()->firstOrCreate([
'participable_id' => $participable->getKey(),
'participable_type' => $participable->getMorphClass(),
'discussion_id' => $this->id,
"{$morph}_id" => $participable->getKey(),
"{$morph}_type" => $participable->getMorphClass(),
'discussion_id' => $this->id,
]);
}

Expand Down Expand Up @@ -357,14 +363,15 @@ public function removeParticipant(EloquentModel $participable, $reload = true)
*/
public function removeParticipants($participables, $reload = true)
{
$morph = config('laravel-messenger.users.morph', 'participable');
$deleted = 0;

foreach ($participables as $participable) {
/** @var \Illuminate\Database\Eloquent\Model $participable */
$deleted += $this->participations()
->where('participable_type', $participable->getMorphClass())
->where('participable_id', $participable->getKey())
->where('discussion_id', $this->id)
->where("{$morph}_type", '=', $participable->getMorphClass())
->where("{$morph}_id", '=', $participable->getKey())
->where('discussion_id', '=', $this->id)
->delete();
}

Expand All @@ -384,9 +391,7 @@ public function removeParticipants($participables, $reload = true)
public function markAsRead(EloquentModel $participable)
{
if ($participant = $this->getParticipationByParticipable($participable)) {
return $participant->update([
'last_read' => Carbon::now()
]);
return $participant->update(['last_read' => Carbon::now()]);
}

return false;
Expand Down Expand Up @@ -415,9 +420,11 @@ public function isUnread(EloquentModel $participable)
*/
public function getParticipationByParticipable(EloquentModel $participable)
{
$morph = config('laravel-messenger.users.morph', 'participable');

return $this->participations()
->where('participable_type', $participable->getMorphClass())
->where('participable_id', $participable->getKey())
->where("{$morph}_type", '=', $participable->getMorphClass())
->where("{$morph}_id", '=', $participable->getKey())
->first();
}

Expand Down Expand Up @@ -484,9 +491,11 @@ public function participationsString($callback = null, $glue = ', ')
*/
public function hasParticipation(EloquentModel $participable)
{
$morph = config('laravel-messenger.users.morph', 'participable');

return $this->participations()
->where('participable_id', '=', $participable->getKey())
->where('participable_type', '=', $participable->getMorphClass())
->where("{$morph}_id", '=', $participable->getKey())
->where("{$morph}_type", '=', $participable->getMorphClass())
->count() > 0;
}

Expand Down
14 changes: 8 additions & 6 deletions src/Models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Message extends Model implements MessageContract
public function __construct(array $attributes = [])
{
$this->setTable(
$this->getTableFromConfig('messages', 'messages')
config('laravel-messenger.messeges.table', 'messages')
);

parent::__construct($attributes);
Expand All @@ -86,7 +86,7 @@ public function __construct(array $attributes = [])
public function discussion()
{
return $this->belongsTo(
$this->getModelFromConfig('discussions', Discussion::class)
config('laravel-messenger.discussions.model', Discussion::class)
);
}

Expand Down Expand Up @@ -118,7 +118,7 @@ public function participable()
public function participations()
{
return $this->hasMany(
$this->getModelFromConfig('participations', Participation::class),
config('laravel-messenger.participations.model', Participation::class),
'discussion_id',
'discussion_id'
);
Expand All @@ -136,9 +136,11 @@ public function participations()
*/
public function getRecipientsAttribute()
{
return $this->participations->reject(function (Participation $participant) {
return $participant->participable_id === $this->participable_id
&& $participant->participable_type === $this->participable_type;
$morph = config('laravel-messenger.users.morph', 'participable');

return $this->participations->reject(function (Participation $participant) use ($morph) {
return $participant->getAttribute("{$morph}_id") === $this->getAttribute("{$morph}_id")
&& $participant->getAttribute("{$morph}_type") === $this->getAttribute("{$morph}_type");
});
}
}
13 changes: 4 additions & 9 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php namespace Arcanedev\LaravelMessenger\Models;

use Arcanedev\LaravelMessenger\Traits\ConfigHelper;
use Arcanedev\Support\Database\Model as BaseModel;

/**
Expand All @@ -11,13 +10,6 @@
*/
abstract class Model extends BaseModel
{
/* -----------------------------------------------------------------
| Traits
| -----------------------------------------------------------------
*/

use ConfigHelper;

/* -----------------------------------------------------------------
| Constructor
| -----------------------------------------------------------------
Expand All @@ -31,7 +23,10 @@ abstract class Model extends BaseModel
public function __construct(array $attributes = [])
{
$this->setConnection(
$this->getFromConfig('database.connection')
config('laravel-messenger.database.connection')
);
$this->setPrefix(
config('laravel-messenger.database.prefix')
);

parent::__construct($attributes);
Expand Down
Loading

0 comments on commit c849958

Please sign in to comment.