Skip to content

Commit

Permalink
refactor: pint
Browse files Browse the repository at this point in the history
  • Loading branch information
blt950 committed Oct 5, 2023
1 parent 3b2e07c commit e7260b8
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 112 deletions.
15 changes: 7 additions & 8 deletions app/Console/Commands/SendTaskNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace App\Console\Commands;

use App\Helpers\TaskStatus;
use App\Models\Task;
use App\Models\User;
use App\Helpers\TaskStatus;
use App\Notifications\TaskNotification;
use Illuminate\Console\Command;
use App\Notifications\TrainingInterestNotification;

class SendTaskNotifications extends Command
{
Expand Down Expand Up @@ -45,22 +44,22 @@ public function handle()
$usersRecipients = $pendingTasks->pluck('recipient_user_id')->merge($completedTasks->pluck('sender_user_id'))->merge($declinedTasks->pluck('sender_user_id'))->unique();
$userModels = User::whereIn('id', $usersRecipients)->get();

foreach($userModels as $user){
foreach ($userModels as $user) {

// If no tasks for this user, skip
if(!$tasks->where('recipient_user_id', $user->id)->count() && ! $tasks->where('sender_user_id', $user->id)->count()){
if (! $tasks->where('recipient_user_id', $user->id)->count() && ! $tasks->where('sender_user_id', $user->id)->count()) {
continue;
}

// If user has disabled task notifications, mark as notified and skip
if(!$user->setting_notify_tasks){
$tasks->where('recipient_user_id', $user->id)->each(function($task){
if (! $user->setting_notify_tasks) {

$tasks->where('recipient_user_id', $user->id)->each(function ($task) {
$task->recipient_notified = true;
$task->save();
});

$tasks->where('sender_user_id', $user->id)->each(function($task){
$tasks->where('sender_user_id', $user->id)->each(function ($task) {
$task->sender_notified = true;
$task->save();
});
Expand Down
56 changes: 25 additions & 31 deletions app/Http/Controllers/TaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,25 @@
namespace App\Http\Controllers;

use App\Helpers\TaskStatus;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use App\Models\Task;
use App\Rules\ValidTaskType;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;

class TaskController extends Controller
{

/**
*
* Show the application task dashboard.
*
* @return \Illuminate\Http\Response
*
*/
public function index($activeFilter = null)
{
$user = auth()->user();

if($activeFilter == 'sent'){
if ($activeFilter == 'sent') {
$tasks = Task::where('sender_user_id', $user->id)->get()->sortBy('created_at');
} elseif($activeFilter == 'archived'){
} elseif ($activeFilter == 'archived') {
$tasks = Task::where('recipient_user_id', $user->id)->whereIn('status', [TaskStatus::COMPLETED->value, TaskStatus::DECLINED->value])->get()->sortBy('created_at');
} else {
$tasks = Task::where('recipient_user_id', $user->id)->where('status', TaskStatus::PENDING->value)->get()->sortBy('created_at');
Expand All @@ -33,12 +31,10 @@ public function index($activeFilter = null)
}

/**
*
* Store a newly created task in storage.
* @param \Illuminate\Http\Request $request
*
*/
public function store(Request $request){
public function store(Request $request)
{

$data = $request->validate([
'type' => ['required', new ValidTaskType],
Expand All @@ -59,15 +55,14 @@ public function store(Request $request){

return redirect()->back()->with('success', 'Task created successfully.');
}

/**
*
* Complete the specified task
*
* @param \Illuminate\Http\Request $request
*
* @param \App\Models\Task $task
*/
public function complete(Request $request, int $task){
public function complete(Request $request, int $task)
{

$task = Task::findOrFail($task);

Expand All @@ -82,13 +77,12 @@ public function complete(Request $request, int $task){
}

/**
*
* Decline the specified task
*
* @param \Illuminate\Http\Request $request
*
* @param \App\Models\Task $task
*/
public function decline(Request $request, int $task){
public function decline(Request $request, int $task)
{

$task = Task::findOrFail($task);

Expand All @@ -102,12 +96,13 @@ public function decline(Request $request, int $task){
return redirect()->back();
}

/**
*
/**
* Return the task type classes
*
* @return array
*/
public static function getTypes(){
public static function getTypes()
{
// Specify the directory where your subclasses are located
$subclassesDirectory = app_path('Tasks/Types');

Expand All @@ -119,7 +114,7 @@ public static function getTypes(){

foreach ($files as $file) {
// Get the class name from the file path
$className = 'App\\Tasks\\Types\\' . pathinfo($file, PATHINFO_FILENAME);;
$className = 'App\\Tasks\\Types\\' . pathinfo($file, PATHINFO_FILENAME);

// Check if the class exists and is a subclass of Types
if (class_exists($className) && is_subclass_of($className, 'App\\Tasks\\Types\\Types')) {
Expand All @@ -131,22 +126,21 @@ public static function getTypes(){
}

/**
*
* Check if a task type is valid
* @param string $type
*
* @param string $type
* @return bool
*
*/
public static function isValidType($type){
public static function isValidType($type)
{
$types = self::getTypes();

foreach($types as $taskType){
if($taskType::class == $type){
foreach ($types as $taskType) {
if ($taskType::class == $type) {
return true;
}
}

return false;
}

}
1 change: 0 additions & 1 deletion app/Http/Controllers/TrainingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\TaskController;

/**
* Controller for all trainings
Expand Down
6 changes: 3 additions & 3 deletions app/Models/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public function sender()
return $this->belongsTo(User::class, 'sender_user_id');
}

public function type(){
if($this->type) {
public function type()
{
if ($this->type) {
return app($this->type);
} else {
throw new \Exception('Invalid task type: ' . $this->type);
}
}

}
2 changes: 1 addition & 1 deletion app/Models/Training.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function updateStatus(int $newStatus, bool $expiredInterest = false)
public function getInlineRatings(bool $vatsimRatingOnly = false)
{

if($vatsimRatingOnly){
if ($vatsimRatingOnly) {
return $this->ratings->where('vatsim_rating', true)->pluck('name')->implode(' + ');
}

Expand Down
23 changes: 10 additions & 13 deletions app/Notifications/TaskNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace App\Notifications;

use App\Helpers\TaskStatus;
use App\Mail\TaskMail;
use App\Models\User;
use App\Helpers\TaskStatus;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class TaskNotification extends Notification
Expand Down Expand Up @@ -53,29 +51,28 @@ public function toMail($notifiable)
{

$textLines = [];
$textLines[] = "There is an update for some of your tasks.";

$textLines[] = 'There is an update for some of your tasks.';

if($this->receivedTasks->count()){
if ($this->receivedTasks->count()) {
$textLines[] = '## New tasks';

foreach($this->receivedTasks as $task){
$textLines[] = "- **" . $task->type()->getName() . "** from " . User::find($task->sender_user_id)->name . " (" . $task->sender_user_id . ")";
foreach ($this->receivedTasks as $task) {
$textLines[] = '- **' . $task->type()->getName() . '** from ' . User::find($task->sender_user_id)->name . ' (' . $task->sender_user_id . ')';
$task->recipient_notified = true;
$task->save();
}

}

if($this->updatedTasks->count()){
if ($this->updatedTasks->count()) {
$textLines[] = '## Updated tasks';

foreach($this->updatedTasks as $task){
$textLines[] = "- **" . $task->type()->getName() . "** for ". User::find($task->reference_user_id)->name ." (". $task->reference_user_id .") is " . strtolower(TaskStatus::from($task->status)->name);
foreach ($this->updatedTasks as $task) {
$textLines[] = '- **' . $task->type()->getName() . '** for ' . User::find($task->reference_user_id)->name . ' (' . $task->reference_user_id . ') is ' . strtolower(TaskStatus::from($task->status)->name);
$task->sender_notified = true;
$task->save();
}

}

// Return the mail message
Expand Down
2 changes: 1 addition & 1 deletion app/Rules/ValidTaskType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use App\Http\Controllers\TaskController;
use Illuminate\Contracts\Validation\Rule;

class ValidTaskType implements Rule
{
Expand Down
28 changes: 17 additions & 11 deletions app/Tasks/Types/Custom.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@

namespace App\Tasks\Types;

use App\Tasks\TaskTypes;
use App\Models\Task;

class Custom extends Types
{
public function getName() {
public function getName()
{
return 'Custom Request';
}

public function getIcon() {
public function getIcon()
{
return 'fa-message';
}

public function getText(Task $model) {
public function getText(Task $model)
{
return $model->message;
}

public function getLink(Task $model){
public function getLink(Task $model)
{
return false;
}

Expand All @@ -28,20 +31,23 @@ public function allowMessage()
return true;
}

public function create(Task $model){
public function create(Task $model)
{
parent::onCreated($model);
}

public function complete(Task $model){
public function complete(Task $model)
{
parent::onCompleted($model);
}

public function decline(Task $model){
public function decline(Task $model)
{
parent::onDeclined($model);
}

public function showConnectedRatings(){
public function showConnectedRatings()
{
return false;
}

}
}
Loading

0 comments on commit e7260b8

Please sign in to comment.