59 lines
1.9 KiB
PHP
59 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Notifications\Application\UseCases;
|
|
|
|
use App\Modules\Machines\Application\Contracts\MaintenanceQueries;
|
|
use App\Modules\Notifications\Domain\Contracts\NotificationRepository;
|
|
use App\Modules\Notifications\Domain\ValueObjects\NewNotification;
|
|
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
|
use App\Modules\Users\Application\Contracts\Clock;
|
|
|
|
/**
|
|
* The daily run of maintenance requests (spec 19.5): tells each machine's
|
|
* responsible person about maintenance coming up and late. The same need
|
|
* (item, last maintenance and level) is told once, so daily maintenance
|
|
* is told again only after it has been done.
|
|
*/
|
|
final class NotifyMaintenanceDue
|
|
{
|
|
public function __construct(
|
|
private MaintenanceQueries $maintenance,
|
|
private NotificationRepository $notifications,
|
|
private TransactionManager $transactions,
|
|
private Clock $clock,
|
|
) {}
|
|
|
|
/**
|
|
* @return int How many notifications were added.
|
|
*/
|
|
public function execute(): int
|
|
{
|
|
$now = $this->clock->now();
|
|
|
|
return $this->transactions->run(function () use ($now): int {
|
|
$added = 0;
|
|
|
|
foreach ($this->maintenance->due() as $item) {
|
|
if ($item->responsibleUserId === null) {
|
|
continue;
|
|
}
|
|
|
|
$notification = NewNotification::maintenanceDue($item->responsibleUserId, $item->id, $item->lastRecordId, $item->level->value, [
|
|
'machine_id' => $item->machineId,
|
|
'machine' => $item->machineName,
|
|
'item' => $item->name,
|
|
'level' => $item->level->value,
|
|
], $now);
|
|
|
|
if ($this->notifications->addIfNew($notification)) {
|
|
$added++;
|
|
}
|
|
}
|
|
|
|
return $added;
|
|
});
|
|
}
|
|
}
|