84 lines
2.6 KiB
PHP
84 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Service;
|
|
|
|
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();
|
|
$diffInSeconds = $now->getTimestamp() - $activityTime->getTimestamp();
|
|
|
|
if ($diffInSeconds < 15 * 60) { // less than 15 minutes
|
|
return '#086572';
|
|
}
|
|
|
|
if ($diffInSeconds < 60 * 60) { // less than 1 hour
|
|
return '#247208';
|
|
}
|
|
return '#cc664c';
|
|
}
|
|
|
|
/**
|
|
* Format activities to include the computed color.
|
|
*
|
|
* @param Actions[] $activities
|
|
* @return array<int, array<string, mixed>>
|
|
*/
|
|
public function formatActivities(array $activities): array
|
|
{
|
|
return array_map(function (Actions $activity) {
|
|
return [
|
|
'date' => $activity->getDate()->format('d/m/Y H:i'),
|
|
'actionType' => $activity->getActionType(),
|
|
'userName' => $activity->getUsers()->getName(),
|
|
// 'organization' => $activity->getOrganization(),
|
|
// 'description' => $activity->getDescription(),
|
|
'color' => $this->getActivityColor($activity->getDate())
|
|
];
|
|
}, $activities);
|
|
}
|
|
|
|
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');
|
|
}
|
|
}
|
|
}
|