<?php
namespace App\Extension\defaultExt\modules\Cases\Service\Fields;
use ApiPlatform\Core\Exception\InvalidArgumentException;
use App\Process\Entity\Process;
use App\Process\Service\ProcessHandlerInterface;
class CaseCalculatePriority implements ProcessHandlerInterface
{
protected const MSG_OPTIONS_NOT_FOUND = 'Process options are not defined';
public const PROCESS_TYPE = 'case-calculate-priority';
/**
* CaseCalculatePriority constructor.
*/
public function __construct()
{
}
/**
* @inheritDoc
*/
public function getProcessType(): string
{
return self::PROCESS_TYPE;
}
/**
* @inheritDoc
*/
public function requiredAuthRole(): string
{
return 'ROLE_USER';
}
/**
* @inheritDoc
*/
public function getRequiredACLs(Process $process): array
{
$options = $process->getOptions();
$module = $options['module'] ?? '';
$id = $options['id'] ?? '';
$editACLCheck = [
'action' => 'edit',
];
if ($id !== '') {
$editACLCheck['record'] = $id;
}
return [
$module => [
$editACLCheck
],
];
}
/**
* @inheritDoc
*/
public function configure(Process $process): void
{
//This process is synchronous
//We aren't going to store a record on db
//thus we will use process type as the id
$process->setId(self::PROCESS_TYPE);
$process->setAsync(false);
}
/**
* @inheritDoc
*/
public function validate(Process $process): void
{
$options = $process->getOptions();
$type = $options['record']['attributes']['type'] ?? '';
if (empty($type)) {
throw new InvalidArgumentException(self::MSG_OPTIONS_NOT_FOUND);
}
}
/**
* @inheritDoc
*/
public function run(Process $process)
{
$options = $process->getOptions();
$type = $options['record']['attributes']['type'] ?? '';
if ($type !== 'User') {
$priority = $options['record']['attributes']['priority'] ?? '';
$responseData = [
'value' => $priority
];
$process->setStatus('success');
$process->setMessages([]);
$process->setData($responseData);
return;
}
$name = $options['record']['attributes']['name'] ?? '';
$value = 'P3';
if (strpos(strtolower($name), 'warning') !== false) {
$value = 'P2';
}
if (strpos(strtolower($name), 'error') !== false) {
$value = 'P1';
}
$responseData = [
'value' => $value
];
$process->setStatus('success');
$process->setMessages([]);
$process->setData($responseData);
}
}