Create Action function

This commit is contained in:
Charles 2025-08-07 09:06:58 +02:00
parent c757a841c5
commit 1f7d844d6f
1 changed files with 42 additions and 1 deletions

View File

@ -2,9 +2,19 @@
namespace App\Service;
class ActionService
use App\Entity\Actions;
use App\Entity\Organizations;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\User\UserInterface;
readonly class ActionService
{
public function __construct(private EntityManagerInterface $entityManager)
{
}
public function getActivityColor(\DateTimeImmutable $activityTime): string
{
$now = new \DateTimeImmutable();
@ -19,4 +29,35 @@ class ActionService
}
return '#C76633';
}
public function createAction(
string $actionType,
User $user,
Organizations $organizations = null,
string $target = null): void
{
$action = new Actions();
$action->setActionType($actionType);
$action->setUsers($user);
if ($organizations !== null) {
$action->setOrganization($organizations);
}
if ($target !== null) {
$this->descriptionAction($action, $target);
}
$this->entityManager->persist($action);
$this->entityManager->flush();
}
public function descriptionAction(&$action, string $target): void{
if ($action instanceof Actions) {
if(!$action->getUsers()){
throw new \InvalidArgumentException('Action must have a user set');
}
$description = "{$action->getActionType()} by {$action->getUsers()->getUserIdentifier()} onto {$target}";
$action->setDescription($description);
} else {
throw new \InvalidArgumentException('Action must be an instance of Actions');
}
}
}