Files
UNIT3D-Community-Edition/app/Repositories/ChatRepository.php
Laravel Shift 5c1c56d2ca Adopt PSR-2 coding style
The Laravel framework adopts the PSR-2 coding style in version 5.1.
Laravel apps *should* adopt this coding style as well. Read the
[PSR-2 coding style guide][1] for more details and check out [PHPCS][2]
to use as a code formatting tool.

[1]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md
[2]: https://github.com/squizlabs/PHP_CodeSniffer
2018-07-18 20:13:14 +00:00

156 lines
3.4 KiB
PHP

<?php
namespace App\Repositories;
use App\Chatroom;
use App\ChatStatus;
use App\Events\MessageDeleted;
use App\Events\MessageSent;
use App\Message;
use App\User;
class ChatRepository
{
/**
* @var Message
*/
private $message;
/**
* @var Chatroom
*/
private $room;
/**
* @var ChatStatus
*/
private $status;
public function __construct(Message $message, Chatroom $room, ChatStatus $status)
{
$this->message = $message;
$this->room = $room;
$this->status = $status;
}
public function config()
{
return config('chat');
}
public function rooms()
{
return $this->room->all();
}
public function roomFindOrFail($id)
{
return $this->room->findOrFail($id);
}
public function message($user_id, $room_id, $message)
{
$message = $this->message->create([
'user_id' => $user_id,
'chatroom_id' => $room_id,
'message' => $message
]);
$this->checkMessageLimits($room_id);
broadcast(new MessageSent($message));
return $message;
}
public function deleteMessage($id)
{
$message = $this->message->find($id);
broadcast(new MessageDeleted($message));
return $message->delete();
}
public function messages($room_id)
{
return $this->message->with(['user.group', 'user.chatStatus'])
->where('chatroom_id', $room_id)
->latest()
->limit(config('chat.message_limit'))
->get();
}
public function checkMessageLimits($room_id)
{
$messages = $this->messages($room_id)->toArray();
$limit = config('chat.message_limit');
$count = count($messages);
// Lets purge all old messages and keep the database to the limit settings
if ($count > $limit) {
for ($x = 1; $x <= $count - $limit; $x++) {
$message = array_pop($messages);
echo $message['id'] . "\n";
$this->message->find($message['id'])->delete();
}
}
}
public function systemMessage($message)
{
$this->message(1, $this->systemChatroom(), $message);
return $this;
}
public function systemChatroom($room = null)
{
$config = config('chat.system_chatroom');
if ($room !== null) {
if ($room instanceof Chatroom) {
$room = $room->id;
} elseif (is_int($room)) {
$room = $this->room->findOrFail($room)->id;
} else {
$room = $this->room->whereName($room)->first()->id;
}
} elseif (is_int($config)) {
$room = $this->room->findOrFail($config)->id;
} elseif ($config instanceof Chatroom) {
$room = $config->id;
} else {
$room = $this->room->whereName($config)->first()->id;
}
return $room;
}
public function statuses()
{
return $this->status->all();
}
public function status($user)
{
if ($user instanceof User) {
$status = $this->status->where('user_id', $user->id)->first();
}
if (is_int($user)) {
$status = $this->status->where('user_id', $user)->first();
}
return $status;
}
public function statusFindOrFail($id)
{
return $this->status->findOrFail($id);
}
}