-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAssignmentNotificationJobHandler.php
More file actions
71 lines (59 loc) · 1.99 KB
/
AssignmentNotificationJobHandler.php
File metadata and controls
71 lines (59 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
namespace App\Async\Handler;
use App\Model\Entity\AsyncJob;
use App\Model\Entity\User;
use App\Model\Entity\Assignment;
use App\Async\IAsyncJobHandler;
use App\Async\Dispatcher;
use App\Helpers\Notifications\AssignmentEmailsSender;
/**
* Scheduled job that sends email notifications when the assignment becomes visible.
*/
class AssignmentNotificationJobHandler implements IAsyncJobHandler
{
public const ID = 'assignmentNotification';
/** @var bool */
private $canceled = false;
/** @var AssignmentEmailsSender */
private $assignmentEmailsSender;
public function __construct(AssignmentEmailsSender $assignmentEmailsSender)
{
$this->assignmentEmailsSender = $assignmentEmailsSender;
}
public function getId(): string
{
return self::ID;
}
public function checkArgs(array $args): bool
{
return !$args; // no arguments expected (assignment is attached to the job itself)
}
public function execute(AsyncJob $job)
{
if ($this->canceled) {
return;
}
$assignment = $job->getAssociatedAssignment();
if ($assignment && !$assignment->isDeleted()) {
$this->assignmentEmailsSender->assignmentCreated($assignment);
}
}
/**
* Factory method for async job entity that will be handled by this handler.
* The job is scheduled at time when the assignment gets visible.
* @param Dispatcher $dispatcher used to schedule the job
* @param User $user creator of the job
* @param Assignment $assignment about which the notification will be sent
* @return AsyncJob that was just scheduled
*/
public static function scheduleAsyncJob(Dispatcher $dispatcher, User $user, Assignment $assignment): AsyncJob
{
$job = new AsyncJob($user, self::ID, [], $assignment);
$dispatcher->schedule($job, $assignment->getVisibleFrom());
return $job;
}
public function cancel(): void
{
$this->canceled = true;
}
}