death-notifier/src/main/php/com/fwdekker/deathnotifier/tracking/RemoveTrackingAction.php

59 lines
1.7 KiB
PHP

<?php
namespace com\fwdekker\deathnotifier\tracking;
use com\fwdekker\deathnotifier\Action;
use com\fwdekker\deathnotifier\validator\IsValidCsrfTokenRule;
use com\fwdekker\deathnotifier\validator\InvalidInputException;
use com\fwdekker\deathnotifier\validator\IsStringRule;
use com\fwdekker\deathnotifier\validator\RuleSet;
use com\fwdekker\deathnotifier\validator\SessionRuleSet;
/**
* Removes a tracking.
*/
class RemoveTrackingAction extends Action
{
/**
* @var TrackingList the list to remove the tracking from
*/
private readonly TrackingList $tracking_list;
/**
* Constructs a new `RemoveTrackingAction`.
*
* @param TrackingList $tracking_list the list to remove the tracking from
*/
public function __construct(TrackingList $tracking_list)
{
$this->tracking_list = $tracking_list;
}
/**
* Removes the tracking by the current user of the specified person.
*
* Requires that the user is logged in and that a valid CSRF token is present.
*
* @param array<int|string, mixed> $inputs `"token": string`: a valid CSRF token, `"person_name": string`: the name
* of the person to stop tracking
* @return null
* @throws InvalidInputException if the user is not logged in, if no valid CSRF token is present, or if the article
* title is not a string
*/
public function handle(array $inputs): mixed
{
(new SessionRuleSet(validate_logged_in: true))->check($_SESSION);
(new RuleSet([
"token" => [new IsValidCsrfTokenRule()],
"person_name" => [new IsStringRule()],
]))->check($inputs);
$this->tracking_list->remove_tracking($_SESSION["uuid"], $inputs["person_name"]);
return null;
}
}