Skip to content

Commit

Permalink
fixed deprecated php warning (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
myckhel authored Nov 20, 2022
1 parent 5379e19 commit 8db285e
Show file tree
Hide file tree
Showing 22 changed files with 1,128 additions and 1,005 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,36 @@
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Myckhel\ChatSystem\Traits\Config;
use Myckhel\ChatSystem\Config;

class CreateConversationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('conversations', function (Blueprint $table) {
$userModel = Config::config('models.user');
$user_table = (new $userModel)->getTable();
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('conversations', function (Blueprint $table) {
$userModel = Config::config('models.user');
$user_table = (new $userModel)->getTable();

$table->id();
$table->foreignId('user_id')->constrained($user_table)->onDelete('cascade');
$table->string('name')->nullable();
$table->enum('type', ['private', 'group', 'issue'])->default('private')->index();
$table->timestamps();
});
}
$table->id();
$table->foreignId('user_id')->constrained($user_table)->onDelete('cascade');
$table->string('name')->nullable();
$table->enum('type', ['private', 'group', 'issue'])->default('private')->index();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('conversations');
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('conversations');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Myckhel\ChatSystem\Traits\Config;
use Myckhel\ChatSystem\Config;

class CreateMessagesTable extends Migration
{
Expand Down
29 changes: 16 additions & 13 deletions src/ChatSystem.php
Original file line number Diff line number Diff line change
@@ -1,48 +1,50 @@
<?php

namespace Myckhel\ChatSystem;

use Illuminate\Support\Facades\Gate;
use Myckhel\ChatSystem\Traits\Config;
use Myckhel\ChatSystem\Config;
use Laravel\Octane\Facades\Octane;

class ChatSystem
{
use Config;

/**
* method to register policies ChatSystem provides.
*
*/
static function registerPolicies() {
static function registerPolicies()
{
Gate::guessPolicyNamesUsing(function ($modelClass) {
$spilts = explode('\\', $modelClass);
return 'Myckhel\\ChatSystem\\Policies\\'.array_pop($spilts).'Policy';
return 'Myckhel\\ChatSystem\\Policies\\' . array_pop($spilts) . 'Policy';
});
}

/**
* method to register observers ChatSystem provides.
*
*/
static function registerObservers(array $exclude = []) {
static function registerObservers(array $exclude = [])
{
@[
'chat_event' => $chat_event,
'conversation' => $conversation
] = $exclude;

$chat_event != true && self::config('models.chat_event')
::observe(self::config('observers.models.chat_event'));
$chat_event != true && Config::config('models.chat_event')
::observe(Config::config('observers.models.chat_event'));

$conversation !== true && self::config('models.conversation')
::observe(self::config('observers.models.conversation'));
$conversation !== true && Config::config('models.conversation')
::observe(Config::config('observers.models.conversation'));
}

/**
* method to register broadcast routes ChatSystem provides.
*
*/
static function registerBroadcastRoutes() {
require __DIR__.'/routes/channels.php';
static function registerBroadcastRoutes()
{
require __DIR__ . '/routes/channels.php';
}

/**
Expand All @@ -53,7 +55,8 @@ static function registerBroadcastRoutes() {
* @param Closure ...$calls
* @return array|Collection
*/
static function async(...$calls){
static function async(...$calls)
{
if (config('octane.server') === 'swoole') {
return Octane::concurrently($calls);
} else {
Expand Down
14 changes: 14 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Myckhel\ChatSystem;

/**
*
*/
class Config
{
static function config(String $config = null)
{
return config("chat-system.$config");
}
}
73 changes: 38 additions & 35 deletions src/Database/Factories/MessageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,47 @@
use Myckhel\ChatSystem\Models\Message;
use Myckhel\ChatSystem\Models\ChatEvent;
use Illuminate\Database\Eloquent\Factories\Factory;
use Myckhel\ChatSystem\Traits\Config;
use Myckhel\ChatSystem\Config;

class MessageFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Message::class;
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Message::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'message' => $this->faker->realText(200, 2),
];
}
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'message' => $this->faker->realText(200, 2),
];
}

function configure() {
return $this->afterCreating(function ($message) {
try {
ChatEvent::factory()->count($this->faker->numberBetween(0, 3))->create([
'made_id' => $message->id,
'made_type' => $message::class,
'maker_id' => $this->faker->randomElement([
$message->user_id,
$message->load(['conversation' => fn ($q) =>
$q->whereNotParticipant($message->user_id)->with('participant')
])->conversation->participant->user_id,
]),
'maker_type' => Config::config('models.user'),
]);
} catch (\Exception $e) {}
});
}
function configure()
{
return $this->afterCreating(function ($message) {
try {
ChatEvent::factory()->count($this->faker->numberBetween(0, 3))->create([
'made_id' => $message->id,
'made_type' => $message::class,
'maker_id' => $this->faker->randomElement([
$message->user_id,
$message->load([
'conversation' => fn ($q) =>
$q->whereNotParticipant($message->user_id)->with('participant')
])->conversation->participant->user_id,
]),
'maker_type' => Config::config('models.user'),
]);
} catch (\Exception $e) {
}
});
}
}
38 changes: 21 additions & 17 deletions src/Database/Seeders/ConversationSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,36 @@
use Myckhel\ChatSystem\Models\Conversation;
use Myckhel\ChatSystem\Models\ConversationUser;
use Faker\Factory as Faker;
use Myckhel\ChatSystem\Traits\Config;
use Myckhel\ChatSystem\Config;

class ConversationSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$userModel = Config::config('models.user');
$conversationModel = Config::config('models.conversation');
$user_key = (new $userModel)->getKeyName();
$faker = Faker::create();
$users = $userModel::pluck($user_key)->toArray();
$conversationModel::factory()->count($faker->numberBetween(min(100, count($users)), count($users)))
->hasParticipants($faker->numberBetween(3, 5), fn ($attributes, $conversation) =>
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$userModel = Config::config('models.user');
$conversationModel = Config::config('models.conversation');
$user_key = (new $userModel)->getKeyName();
$faker = Faker::create();
$users = $userModel::pluck($user_key)->toArray();
$conversationModel::factory()->count($faker->numberBetween(min(100, count($users)), count($users)))
->hasParticipants(
$faker->numberBetween(3, 5),
fn ($attributes, $conversation) =>
[
'user_id' => $faker->randomElement(
collect($users)->filter(fn ($id) => $id != $conversation->user_id)
),
'conversation_id' => $conversation->id,
]
)
->hasMessages($faker->numberBetween(1, 5), fn (array $attributes, $conversation) =>
->hasMessages(
$faker->numberBetween(1, 5),
fn (array $attributes, $conversation) =>
[
'conversation_id' => $conversation->id,
'user_id' => $faker->randomElement([
Expand All @@ -47,5 +51,5 @@ public function run()
->create([
'user_id' => $faker->randomElement($users),
]);
}
}
}
81 changes: 42 additions & 39 deletions src/Events/Message/Created.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,50 @@
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Myckhel\ChatSystem\Traits\Config;
use Myckhel\ChatSystem\Config;
use Myckhel\ChatSystem\Contracts\IMessage;

class Created implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $broadcastQueue = 'chat';
public $message;

/**
* Create a new event instance.
*
* @return void
*/
public function __construct(IMessage $message){
$this->message = $message;
$this->broadcastQueue = Config::config("queues.events.message.created");
}

public function broadcastAs() {
return 'App\\Events\\Myckhel\\ChatSystem\\Events\\Message';
}

function broadcastWith() {
return [
'message' => $this->message,
];
}

/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
$ids = $this->message->participants()->pluck('user_id')->toArray();

return array_merge(
[new PrivateChannel("message-created.{$this->message->conversation_id}")],
array_map(fn ($id) => new PrivateChannel("message-new.user.{$id}"), $ids)
);
}
use Dispatchable, InteractsWithSockets, SerializesModels;
public $broadcastQueue = 'chat';
public $message;

/**
* Create a new event instance.
*
* @return void
*/
public function __construct(IMessage $message)
{
$this->message = $message;
$this->broadcastQueue = Config::config("queues.events.message.created");
}

public function broadcastAs()
{
return 'App\\Events\\Myckhel\\ChatSystem\\Events\\Message';
}

function broadcastWith()
{
return [
'message' => $this->message,
];
}

/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
$ids = $this->message->participants()->pluck('user_id')->toArray();

return array_merge(
[new PrivateChannel("message-created.{$this->message->conversation_id}")],
array_map(fn ($id) => new PrivateChannel("message-new.user.{$id}"), $ids)
);
}
}
Loading

0 comments on commit 8db285e

Please sign in to comment.