Skip to content

Commit

Permalink
Use contract for event (#142)
Browse files Browse the repository at this point in the history
* Use contract for event

* Apply fixes from StyleCI (#143)
  • Loading branch information
musonza authored Jun 11, 2019
1 parent a8824c6 commit 8ccbaa0
Show file tree
Hide file tree
Showing 18 changed files with 65 additions and 66 deletions.
6 changes: 3 additions & 3 deletions config/musonza_chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
return [
'user_model' => 'App\User',

/**
/*
* If not set, the package will use getKeyName() on the user_model specified above
*/
'user_model_primary_key' => null,
Expand All @@ -16,13 +16,13 @@
*/
'broadcasts' => false,

/**
/*
* The event to fire when a message is sent
* See Musonza\Chat\Eventing\MessageWasSent if you want to customize.
*/
'sent_message_event' => 'Musonza\Chat\Eventing\MessageWasSent',

/**
/*
* Automatically convert conversations with more than two users to public
*/
'make_three_or_more_users_public' => true,
Expand Down
12 changes: 7 additions & 5 deletions src/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Chat
use SetsParticipants;

/**
* @param MessageService $messageService
* @param MessageService $messageService
* @param ConversationService $conversationService
* @param MessageNotification $messageNotification
*/
Expand All @@ -40,7 +40,7 @@ public function createConversation(array $participants, array $data = [])
/**
* Sets message.
*
* @param string | Musonza\Chat\Models\Message $message
* @param string | Musonza\Chat\Models\Message $message
*
* @return MessageService
*/
Expand All @@ -62,7 +62,8 @@ public function messages()
/**
* Sets Conversation.
*
* @param Conversation $conversation
* @param Conversation $conversation
*
* @return ConversationService
*/
public function conversation(Conversation $conversation)
Expand Down Expand Up @@ -101,7 +102,8 @@ public static function userModel()
}

/**
* Returns primary key for the User model
* Returns primary key for the User model.
*
* @return string
*/
public static function userModelPrimaryKey()
Expand All @@ -112,7 +114,7 @@ public static function userModelPrimaryKey()
/**
* Should the messages be broadcasted.
*
* @return boolean
* @return bool
*/
public static function broadcasts()
{
Expand Down
6 changes: 3 additions & 3 deletions src/ChatServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ private function registerChat()
public function publishMigrations()
{
$timestamp = date('Y_m_d_His', time());
$stub = __DIR__ . '/../database/migrations/create_chat_tables.php';
$target = $this->app->databasePath() . '/migrations/' . $timestamp . '_create_chat_tables.php';
$stub = __DIR__.'/../database/migrations/create_chat_tables.php';
$target = $this->app->databasePath().'/migrations/'.$timestamp.'_create_chat_tables.php';

$this->publishes([$stub => $target], 'chat.migrations');
}
Expand All @@ -68,7 +68,7 @@ public function publishMigrations()
public function publishConfig()
{
$this->publishes([
__DIR__ . '/../config' => config_path(),
__DIR__.'/../config' => config_path(),
], 'chat.config');
}
}
1 change: 1 addition & 0 deletions src/Commanding/CommandBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function __construct(Application $app, CommandTranslator $commandTranslat
public function execute($command)
{
$handler = $this->commandTranslator->toCommandHandler($command);

return $this->app->make($handler)->handle($command);
}
}
2 changes: 1 addition & 1 deletion src/Commanding/CommandTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class CommandTranslator
{
public function toCommandHandler($command)
{
$handler = str_replace('Command', 'CommandHandler', get_class($command));
$handler = str_replace('Command', 'CommandHandler', get_class($command));

if (!class_exists($handler)) {
$message = "Command handler [$handler] does not exist.";
Expand Down
4 changes: 2 additions & 2 deletions src/Eventing/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Musonza\Chat\Eventing;

use Illuminate\Events\Dispatcher;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Musonza\Chat\Chat;

class EventDispatcher
{
protected $event;

public function __construct(Dispatcher $event)
public function __construct(DispatcherContract $event)
{
$this->event = $event;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Eventing/MessageWasSent.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ public function createNotifications()
*/
public function broadcastOn()
{
return new Channel('mc-chat-conversation.' . $this->message->conversation->id);
return new Channel('mc-chat-conversation.'.$this->message->conversation->id);
}
}
6 changes: 3 additions & 3 deletions src/Messages/SendMessageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class SendMessageCommand

/**
* @param Conversation $conversation The conversation
* @param string $body The message body
* @param int $senderId The sender identifier
* @param string $type The message type
* @param string $body The message body
* @param int $senderId The sender identifier
* @param string $type The message type
*/
public function __construct(Conversation $conversation, $body, $senderId, $type = 'text')
{
Expand Down
2 changes: 1 addition & 1 deletion src/Messages/SendMessageCommandHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SendMessageCommandHandler implements CommandHandler

/**
* @param EventDispatcher $dispatcher The dispatcher
* @param Message $message The message
* @param Message $message The message
*/
public function __construct(EventDispatcher $dispatcher, Message $message)
{
Expand Down
8 changes: 3 additions & 5 deletions src/Models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use Musonza\Chat\BaseModel;
use Musonza\Chat\Chat;
use Musonza\Chat\Eventing\EventGenerator;
use Musonza\Chat\Models\Conversation;
use Musonza\Chat\Models\MessageNotification;

class Message extends BaseModel
{
Expand Down Expand Up @@ -60,9 +58,9 @@ public function conversation()
public function send(Conversation $conversation, $body, $userId, $type = 'text')
{
$message = $conversation->messages()->create([
'body' => $body,
'body' => $body,
'user_id' => $userId,
'type' => $type,
'type' => $type,
]);

$messageWasSent = Chat::sentMessageEvent();
Expand Down Expand Up @@ -116,7 +114,7 @@ public function markRead($user)

public function flagged($user)
{
return !!MessageNotification::where('user_id', $user->getKey())
return (bool) MessageNotification::where('user_id', $user->getKey())
->where('message_id', $this->id)
->where('flagged', 1)
->first();
Expand Down
15 changes: 6 additions & 9 deletions src/Models/MessageNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Notification;
use Musonza\Chat\BaseModel;
use Musonza\Chat\Chat;
use Musonza\Chat\Models\Conversation;
use Musonza\Chat\Models\Message;

class MessageNotification extends BaseModel
{
Expand All @@ -30,7 +27,7 @@ public static function make(Message $message, Conversation $conversation)

public function unReadNotifications($user)
{
return MessageNotification::where([
return self::where([
['user_id', '=', $user->getKey()],
['is_seen', '=', 0],
])->get();
Expand All @@ -44,12 +41,12 @@ public static function createCustomNotifications($message, $conversation)
$is_sender = ($message->user_id == $user->getKey()) ? 1 : 0;

$notification[] = [
'user_id' => $user->getKey(),
'message_id' => $message->id,
'user_id' => $user->getKey(),
'message_id' => $message->id,
'conversation_id' => $conversation->id,
'is_seen' => $is_sender,
'is_sender' => $is_sender,
'created_at' => $message->created_at,
'is_seen' => $is_sender,
'is_sender' => $is_sender,
'created_at' => $message->created_at,
];
}

Expand Down
13 changes: 7 additions & 6 deletions src/Services/ConversationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ public function get()
}

return $this->conversation->getUserConversations($this->user, [
'perPage' => $this->perPage,
'page' => $this->page,
'pageName' => 'page',
'isPrivate' => $this->isPrivate
'perPage' => $this->perPage,
'page' => $this->page,
'pageName' => 'page',
'isPrivate' => $this->isPrivate,
]);
}

Expand Down Expand Up @@ -170,8 +170,9 @@ private function getConversationsInCommon($conversation1, $conversation2)
/**
* Sets the conversation type to query for, public or private.
*
* @param boolean $isPrivate
* @return boolean
* @param bool $isPrivate
*
* @return bool
*/
public function isPrivate($isPrivate = true)
{
Expand Down
12 changes: 6 additions & 6 deletions src/Traits/Paginates.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ public function setPaginationParams($params)
public function getPaginationParams()
{
return [
'page' => $this->page,
'perPage' => $this->perPage,
'sorting' => $this->sorting,
'columns' => $this->columns,
'pageName' => $this->pageName
'page' => $this->page,
'perPage' => $this->perPage,
'sorting' => $this->sorting,
'columns' => $this->columns,
'pageName' => $this->pageName,
];
}
}
}
12 changes: 6 additions & 6 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Musonza\Chat\Tests;

require __DIR__ . '/../database/migrations/create_chat_tables.php';
require __DIR__.'/../database/migrations/create_chat_tables.php';

use CreateChatTables;
use Illuminate\Database\Schema\Blueprint;
Expand All @@ -24,7 +24,7 @@ public function setUp()
parent::setUp();

$this->artisan('migrate', ['--database' => 'testbench']);
$this->withFactories(__DIR__ . '/../database/factories');
$this->withFactories(__DIR__.'/../database/factories');
$this->migrate();
$this->users = $this->createUsers(6);
}
Expand All @@ -48,7 +48,7 @@ protected function migrateTestTables()
protected function migrate()
{
$this->migrateTestTables();
(new CreateChatTables)->up();
(new CreateChatTables())->up();
}

/**
Expand All @@ -65,9 +65,9 @@ protected function getEnvironmentSetUp($app)
// Setup default database to use sqlite :memory:
$app['config']->set('database.default', 'testbench');
$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
'prefix' => '',
]);

// $app['config']->set('database.default', 'testbench');
Expand Down Expand Up @@ -108,7 +108,7 @@ public function createUsers($count = 1)

public function tearDown()
{
(new CreateChatTables)->down();
(new CreateChatTables())->down();
$this->rollbackTestTables();
parent::tearDown();
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/ConversationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function it_creates_a_conversation()
{
$conversation = Chat::createConversation([$this->users[0]->getKey(), $this->users[1]->getKey()]);

$this->assertDatabaseHas($this->prefix . 'conversations', ['id' => 1]);
$this->assertDatabaseHas($this->prefix.'conversations', ['id' => 1]);
}

/** @test */
Expand Down Expand Up @@ -220,13 +220,13 @@ public function it_returns_the_correct_order_of_conversations_when_updated_at_is
$auth = $this->users[0];

$conversation = Chat::createConversation([$auth->getKey(), $this->users[1]->getKey()]);
Chat::message('Hello-' . $conversation->id)->from($auth)->to($conversation)->send();
Chat::message('Hello-'.$conversation->id)->from($auth)->to($conversation)->send();

$conversation = Chat::createConversation([$auth->getKey(), $this->users[2]->getKey()]);
Chat::message('Hello-' . $conversation->id)->from($auth)->to($conversation)->send();
Chat::message('Hello-'.$conversation->id)->from($auth)->to($conversation)->send();

$conversation = Chat::createConversation([$auth->getKey(), $this->users[3]->getKey()]);
Chat::message('Hello-' . $conversation->id)->from($auth)->to($conversation)->send();
Chat::message('Hello-'.$conversation->id)->from($auth)->to($conversation)->send();

$conversations = Chat::conversations()->setPaginationParams(['sorting' => 'desc'])->setUser($auth)->limit(1)->page(1)->get();
$this->assertEquals('Hello-3', $conversations->items()[0]->last_message->body);
Expand Down
10 changes: 5 additions & 5 deletions tests/Unit/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ public function it_can_return_paginated_messages_in_a_conversation()
$conversation = Chat::createConversation([$this->users[0]->getKey(), $this->users[1]->getKey()]);

for ($i = 0; $i < 3; $i++) {
Chat::message('Hello ' . $i)->from($this->users[0])->to($conversation)->send();
Chat::message('Hello Man ' . $i)->from($this->users[1])->to($conversation)->send();
Chat::message('Hello '.$i)->from($this->users[0])->to($conversation)->send();
Chat::message('Hello Man '.$i)->from($this->users[1])->to($conversation)->send();
}

Chat::message('Hello Man')->from($this->users[1])->to($conversation)->send();
Expand Down Expand Up @@ -153,10 +153,10 @@ public function it_can_return_recent_user_messsages()
$this->assertCount(3, $recent_messages);

$recent_messages = Chat::conversations()->setUser($this->users[0])->setPaginationParams([
'perPage' => 1,
'page' => 1,
'perPage' => 1,
'page' => 1,
'pageName' => 'test',
'sorting' => 'desc',
'sorting' => 'desc',
])->get();

$this->assertCount(1, $recent_messages);
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/NotificationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function it_gets_all_unread_notifications()
$conversation2 = Chat::createConversation([$this->users[2]->getKey(), $this->users[0]->getKey()]);
Chat::message('Hello 3')->from($this->users[2])->to($conversation2)->send();

$notifications = Chat::for($this->users[0]) ->unReadNotifications();
$notifications = Chat::for($this->users[0])->unReadNotifications();

$this->assertEquals(3, $notifications->count());
}
Expand Down
10 changes: 5 additions & 5 deletions tests/Unit/PaginationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ class PaginationTest extends TestCase
public function it_can_set_pagination_params()
{
$chat = Chat::conversations()->setPaginationParams([
'perPage' => 30,
'page' => 3,
'perPage' => 30,
'page' => 3,
'pageName' => 'test',
'sorting' => 'desc',
'sorting' => 'desc',
]);

$this->assertEquals([
'page' => 3,
'page' => 3,
'perPage' => 30,
'sorting' => "desc",
'sorting' => 'desc',
'columns' => [
0 => '*',
],
Expand Down

0 comments on commit 8ccbaa0

Please sign in to comment.