Passed
Push — master ( 6760ee...9e3b1c )
by Ion
02:46
created

TaskNotificationsCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Constants\TranslationCode;
6
use App\Models\UserTask;
7
use App\Services\LogService;
8
use App\Services\NotificationService;
9
use Carbon\Carbon;
10
use Exception;
11
use Illuminate\Console\Command;
12
use Illuminate\Database\Eloquent\Collection;
13
use Illuminate\Support\Facades\DB;
14
use Illuminate\Support\Facades\Log;
15
16
/**
17
 * Class TaskNotificationsCommand
18
 *
19
 * Send notifications to assigned user when an uncompleted task has deadline today.
20
 * Send notifications to users added the task when the task is uncompleted and deadline passed by a day.
21
 * Should be running once a day.
22
 *
23
 * @package App\Console\Commands
24
 */
25
class TaskNotificationsCommand extends Command
26
{
27
    /** @var string */
28
    protected $signature = 'send:taskNotifications';
29
30
    /** @var string */
31
    protected $description = 'Send notifications when a task is expiring or is expired.';
32
33
    /** @var NotificationService */
34
    protected $notificationService;
35
36
    /**
37
     * TaskNotificationsCommand constructor.
38
     */
39
    public function __construct()
40
    {
41
        parent::__construct();
42
43
        $this->notificationService = new NotificationService();
44
    }
45
46
    /**
47
     * Command handle
48
     */
49
    public function handle()
50
    {
51
        try {
52
            $this->info('[' . Carbon::now()->format('Y-m-d H:i:s') . ']: Command [send:taskNotifications] started.');
53
54
            DB::beginTransaction();
55
56
            $this->checkExpiringTasks();
57
58
            $this->checkExpiredTasks();
59
60
            DB::commit();
61
62
            $this->info('[' . Carbon::now()->format('Y-m-d H:i:s') . ']: Command [send:taskNotifications] ended.');
63
        } catch (Exception $e) {
64
            Log::error(LogService::getExceptionTraceAsString($e));
65
66
            $this->error($e->getMessage());
67
        }
68
    }
69
70
    /**
71
     * Identify uncompleted tasks that have deadline today and send notifications.
72
     */
73
    private function checkExpiringTasks()
74
    {
75
        /** @var Collection $expiringUserTasks */
76
        $expiringUserTasks = UserTask::where('status', UserTask::STATUS_ASSIGNED)
77
            ->where('deadline', Carbon::now()->format('Y-m-d'))
78
            ->get();
79
80
        $this->info('[' . Carbon::now()->format('Y-m-d H:i:s') . ']: Found ' . $expiringUserTasks->count() . ' expiring tasks.');
81
82
        foreach ($expiringUserTasks as $expiringUserTask) {
83
            $userNotification = $this->notificationService->addNotification(
84
                $expiringUserTask->assigned_user_id,
85
                TranslationCode::USER_TASK_EXPIRING,
86
                'userTask',
87
                $expiringUserTask->id
88
            );
89
90
            $this->notificationService->sendNotification($userNotification);
91
        }
92
    }
93
94
    /**
95
     * Identify uncompleted tasks that have deadline passed by a day and send notifications.
96
     */
97
    private function checkExpiredTasks()
98
    {
99
        /** @var Collection $expiredUserTasks */
100
        $expiredUserTasks = UserTask::where('status', UserTask::STATUS_ASSIGNED)
101
            ->where('deadline', Carbon::now()->subDay()->format('Y-m-d'))
102
            ->get();
103
104
        $this->info('[' . Carbon::now()->format('Y-m-d H:i:s') . ']: Found ' . $expiredUserTasks->count() . ' expired tasks.');
105
106
        foreach ($expiredUserTasks as $expiredUserTask) {
107
            $userNotification = $this->notificationService->addNotification(
108
                $expiredUserTask->assigned_user_id,
109
                TranslationCode::USER_TASK_EXPIRED,
110
                'userTask',
111
                $expiredUserTask->id
112
            );
113
114
            $this->notificationService->sendNotification($userNotification);
115
        }
116
    }
117
}
118