add loggers actions
This commit is contained in:
parent
59398d14ba
commit
12d4db370f
22 changed files with 517 additions and 218 deletions
|
@ -17,6 +17,7 @@ use function uuid_create;
|
|||
use Symfony\Component\Filesystem\Filesystem;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Twig\Environment;
|
||||
use App\Service\ActionLogger;
|
||||
|
||||
final class AdminController extends AbstractController
|
||||
{
|
||||
|
@ -25,7 +26,8 @@ final class AdminController extends AbstractController
|
|||
private EntityManagerInterface $entityManager,
|
||||
private Motocultrice $motocultrice,
|
||||
private BudgetService $budgetService,
|
||||
private Environment $twig
|
||||
private Environment $twig,
|
||||
private ActionLogger $actionLogger
|
||||
) {
|
||||
}
|
||||
|
||||
|
@ -145,6 +147,7 @@ final class AdminController extends AbstractController
|
|||
$stats->setAvecSite($calculatedStats['counters']['avec_site']);
|
||||
$stats->setAvecAccessibilite($calculatedStats['counters']['avec_accessibilite']);
|
||||
$stats->setAvecNote($calculatedStats['counters']['avec_note']);
|
||||
|
||||
$stats->setCompletionPercent($calculatedStats['completion_percent']);
|
||||
|
||||
// Associer les stats à chaque commerce
|
||||
|
@ -730,15 +733,9 @@ final class AdminController extends AbstractController
|
|||
|
||||
// Afficher le log des objets non trouvés à la fin
|
||||
if (!empty($notFoundOsmKeys)) {
|
||||
return $this->render('admin/labourage_results.html.twig', [
|
||||
'stats' => $stats,
|
||||
'zone' => $insee_code,
|
||||
'new_places_counter' => $processedCount,
|
||||
'commerces' => $commerces,
|
||||
'not_found_osm_keys' => $notFoundOsmKeys
|
||||
]);
|
||||
$this->addFlash('info', count($notFoundOsmKeys).' objets OSM non trouvés lors du labourage.');
|
||||
}
|
||||
// Sinon, rediriger comme avant
|
||||
// Rediriger dans tous les cas vers la page de stats de la ville
|
||||
return $this->redirectToRoute('app_admin_stats', ['insee_code' => $insee_code]);
|
||||
} catch (\Exception $e) {
|
||||
$this->addFlash('error', 'Erreur lors du labourage : ' . $e->getMessage());
|
||||
|
|
|
@ -13,6 +13,7 @@ use GuzzleHttp\Client;
|
|||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Mime\Email;
|
||||
use Symfony\Component\Mailer\MailerInterface;
|
||||
use App\Service\ActionLogger;
|
||||
|
||||
class PublicController extends AbstractController
|
||||
{
|
||||
|
@ -22,19 +23,19 @@ class PublicController extends AbstractController
|
|||
public function __construct(
|
||||
private EntityManagerInterface $entityManager,
|
||||
private Motocultrice $motocultrice,
|
||||
private MailerInterface $mailer
|
||||
) {
|
||||
}
|
||||
private MailerInterface $mailer,
|
||||
private ActionLogger $actionLogger
|
||||
) {}
|
||||
|
||||
#[Route('/propose-email/{email}/{type}/{id}', name: 'app_public_propose_email')]
|
||||
public function proposeEmail(string $email, string $type, int $id): Response
|
||||
{
|
||||
|
||||
|
||||
$data = $this->motocultrice->get_osm_object_data($type, $id);
|
||||
// Récupérer le code postal depuis les tags, sinon mettre -1
|
||||
$zipCode = isset($data['tags_converted']['addr:postcode']) ? (int)$data['tags_converted']['addr:postcode'] : -1;
|
||||
$place_name = $data['tags_converted']['name'];
|
||||
$data = $this->motocultrice->get_osm_object_data($type, $id);
|
||||
// Récupérer le code postal depuis les tags, sinon mettre -1
|
||||
$zipCode = isset($data['tags_converted']['addr:postcode']) ? (int)$data['tags_converted']['addr:postcode'] : -1;
|
||||
$place_name = $data['tags_converted']['name'];
|
||||
|
||||
// Vérifier si une Place existe déjà avec le même osm_kind et osmId
|
||||
$existingPlace = $this->entityManager->getRepository(Place::class)->findOneBy([
|
||||
|
@ -45,10 +46,10 @@ class PublicController extends AbstractController
|
|||
if ($existingPlace) {
|
||||
// Mettre à jour l'email de la Place existante
|
||||
$existingPlace->setEmail($email)->setLastContactAttemptDate(new \DateTime());
|
||||
if($zipCode != -1) {
|
||||
if ($zipCode != -1) {
|
||||
$existingPlace->setZipCode($zipCode);
|
||||
}
|
||||
$this->entityManager->persist($existingPlace);
|
||||
$this->entityManager->persist($existingPlace);
|
||||
$this->entityManager->flush();
|
||||
|
||||
$debug = '';
|
||||
|
@ -59,28 +60,28 @@ class PublicController extends AbstractController
|
|||
'uuid' => $existingPlace->getUuidForUrl()
|
||||
], true);
|
||||
}
|
||||
|
||||
$this->addFlash('success', 'L\'email a été mis à jour. Un email vous sera envoyé avec le lien de modification. '.$debug);
|
||||
|
||||
$this->addFlash('success', 'L\'email a été mis à jour. Un email vous sera envoyé avec le lien de modification. ' . $debug);
|
||||
} else {
|
||||
|
||||
|
||||
// Créer une nouvelle entité Place
|
||||
$place = new Place();
|
||||
$place->setEmail($email)
|
||||
->setOsmId($id)
|
||||
->setOsmKind($type)
|
||||
->setAskedHumainsSupport(false)
|
||||
->setOptedOut(false)
|
||||
->setDead(false)
|
||||
->setNote('')
|
||||
->setModifiedDate(new \DateTime())
|
||||
->setZipCode($zipCode)
|
||||
->setPlaceCount(0)
|
||||
->setMainTag($this->motocultrice->find_main_tag($data['tags_converted']) ?? '')
|
||||
->setStreet($this->motocultrice->find_street($data['tags_converted']) ?? '')
|
||||
->setHousenumber($this->motocultrice->find_housenumber($data['tags_converted']) ?? '')
|
||||
->setLastContactAttemptDate(new \DateTime())
|
||||
->setUuidForUrl(uniqid());
|
||||
->setOsmId($id)
|
||||
->setOsmKind($type)
|
||||
->setAskedHumainsSupport(false)
|
||||
->setOptedOut(false)
|
||||
->setDead(false)
|
||||
->setNote('')
|
||||
->setModifiedDate(new \DateTime())
|
||||
->setZipCode($zipCode)
|
||||
->setPlaceCount(0)
|
||||
->setMainTag($this->motocultrice->find_main_tag($data['tags_converted']) ?? '')
|
||||
->setStreet($this->motocultrice->find_street($data['tags_converted']) ?? '')
|
||||
->setHousenumber($this->motocultrice->find_housenumber($data['tags_converted']) ?? '')
|
||||
->setLastContactAttemptDate(new \DateTime())
|
||||
->setUuidForUrl(uniqid());
|
||||
|
||||
$this->entityManager->persist($place);
|
||||
$this->entityManager->flush();
|
||||
|
@ -93,7 +94,7 @@ class PublicController extends AbstractController
|
|||
'uuid' => $place->getUuidForUrl()
|
||||
], true);
|
||||
}
|
||||
$this->addFlash('success', 'Un email vous sera envoyé avec le lien de modification. '.$debug);
|
||||
$this->addFlash('success', 'Un email vous sera envoyé avec le lien de modification. ' . $debug);
|
||||
}
|
||||
|
||||
// Envoyer l'email
|
||||
|
@ -118,7 +119,7 @@ class PublicController extends AbstractController
|
|||
public function index(): Response
|
||||
{
|
||||
$stats = $this->entityManager->getRepository(Stats::class)->findAll();
|
||||
|
||||
|
||||
return $this->render('public/home.html.twig', [
|
||||
'controller_name' => 'PublicController',
|
||||
'stats' => $stats
|
||||
|
@ -128,9 +129,15 @@ class PublicController extends AbstractController
|
|||
#[Route('/edit/{zipcode}/{name}/{uuid}', name: 'app_public_edit')]
|
||||
public function edit_with_uuid($zipcode, $name, $uuid): Response
|
||||
{
|
||||
$this->actionLogger->log('dashboard', [
|
||||
'zipcode' => $zipcode,
|
||||
|
||||
|
||||
]);
|
||||
|
||||
$place = $this->entityManager->getRepository(Place::class)->findOneBy(['uuid_for_url' => $uuid]);
|
||||
if (!$place) {
|
||||
$this->addFlash('warning', 'Ce lien de modification n\'existe pas.'.$uuid);
|
||||
$this->addFlash('warning', 'Ce lien de modification n\'existe pas.' . $uuid);
|
||||
return $this->redirectToRoute('app_public_index');
|
||||
}
|
||||
|
||||
|
@ -142,19 +149,19 @@ class PublicController extends AbstractController
|
|||
// récupérer les tags de base
|
||||
$base_tags = $this->motocultrice->base_tags;
|
||||
$base_tags = array_fill_keys($base_tags, '');
|
||||
|
||||
|
||||
$commerce_overpass = $this->motocultrice->get_osm_object_data($place->getOsmKind(), $place->getOsmId());
|
||||
// Fusionner les tags de base avec les tags existants
|
||||
|
||||
$commerce_overpass['tags_converted'] = array_merge($base_tags, $commerce_overpass['tags_converted']);
|
||||
|
||||
|
||||
$commerce_overpass['tags_converted'] = array_merge($base_tags, $commerce_overpass['tags_converted']);
|
||||
|
||||
// Trier les tags par ordre alphabétique des clés
|
||||
ksort($commerce_overpass['tags_converted']);
|
||||
|
||||
$place->setDisplayedDate(new \DateTime());
|
||||
$this->entityManager->persist($place);
|
||||
$this->entityManager->flush();
|
||||
|
||||
|
||||
return $this->render('public/edit.html.twig', [
|
||||
'commerce_overpass' => $commerce_overpass,
|
||||
'name' => $name,
|
||||
|
@ -172,7 +179,9 @@ class PublicController extends AbstractController
|
|||
#[Route('/dashboard', name: 'app_public_dashboard')]
|
||||
public function dashboard(): Response
|
||||
{
|
||||
|
||||
|
||||
$this->actionLogger->log('dashboard', []);
|
||||
|
||||
$stats_repo = $this->entityManager->getRepository(Stats::class)->findAll();
|
||||
|
||||
$stats_for_chart = [];
|
||||
|
@ -204,6 +213,12 @@ class PublicController extends AbstractController
|
|||
#[Route('/modify/{osm_object_id}/{version}/{changesetID}', name: 'app_public_submit')]
|
||||
public function submit($osm_object_id, $version, $changesetID): Response
|
||||
{
|
||||
|
||||
$this->actionLogger->log('submit_object', [
|
||||
'osm_id' => $osm_object_id,
|
||||
'version' => $version,
|
||||
'changesetID' => $changesetID
|
||||
]);
|
||||
$place = $this->entityManager->getRepository(Place::class)->findOneBy(['osmId' => $osm_object_id]);
|
||||
if (!$place) {
|
||||
$this->addFlash('warning', 'Ce commerce n\'existe pas.');
|
||||
|
@ -212,19 +227,19 @@ class PublicController extends AbstractController
|
|||
|
||||
// Récupérer les données POST
|
||||
$request = Request::createFromGlobals();
|
||||
|
||||
|
||||
// Vérifier si des données ont été soumises
|
||||
if ($request->isMethod('POST')) {
|
||||
$status = "non modifié";
|
||||
|
||||
|
||||
// Récupérer le type d'objet (node ou way)
|
||||
$osm_kind = $request->request->get('osm_kind', 'node');
|
||||
|
||||
|
||||
// Récupérer tous les tags du formulaire
|
||||
$tags = [];
|
||||
|
||||
$request_post = $request->request->all();
|
||||
|
||||
|
||||
$request_post = $this->motocultrice->map_post_values($request_post);
|
||||
|
||||
foreach ($request_post as $key => $value) {
|
||||
|
@ -258,7 +273,7 @@ class PublicController extends AbstractController
|
|||
$tag = $changeset->addChild('tag');
|
||||
$tag->addAttribute('k', 'created_by');
|
||||
$tag->addAttribute('v', 'OSM Mon Commerce Web Editor');
|
||||
|
||||
|
||||
$tag = $changeset->addChild('tag');
|
||||
$tag->addAttribute('k', 'comment');
|
||||
$tag->addAttribute('v', 'Modification dans #MonCommerceOSM');
|
||||
|
@ -275,14 +290,14 @@ class PublicController extends AbstractController
|
|||
|
||||
// Récupérer les données actuelles de l'objet
|
||||
$currentObjectData = $this->motocultrice->get_osm_object_data($osm_kind, $osm_object_id);
|
||||
|
||||
|
||||
// 2. Modifier l'objet avec le nouveau changeset
|
||||
$xml = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><osm version="0.6"></osm>');
|
||||
$object = $xml->addChild($osm_kind);
|
||||
$object->addAttribute('id', $osm_object_id);
|
||||
$object->addAttribute('version', $version);
|
||||
$object->addAttribute('changeset', $newChangesetId);
|
||||
|
||||
|
||||
// Ajouter les coordonnées pour les nodes
|
||||
if ($osm_kind === 'node') {
|
||||
if (!isset($currentObjectData['@attributes']['lat']) || !isset($currentObjectData['@attributes']['lon'])) {
|
||||
|
@ -291,7 +306,7 @@ class PublicController extends AbstractController
|
|||
$object->addAttribute('lat', $currentObjectData['@attributes']['lat']);
|
||||
$object->addAttribute('lon', $currentObjectData['@attributes']['lon']);
|
||||
}
|
||||
|
||||
|
||||
// Ajouter les tags
|
||||
foreach ($tags as $key => $value) {
|
||||
if (!empty($key) && !empty($value)) {
|
||||
|
@ -323,18 +338,26 @@ class PublicController extends AbstractController
|
|||
$status = "Les tags ont été mis à jour avec succès";
|
||||
} else {
|
||||
$status = "Erreur lors de la mise à jour des tags";
|
||||
$this->actionLogger->log('ERROR_submit_object', [
|
||||
'osm_id' => $osm_object_id,
|
||||
'version' => $version,
|
||||
'changesetID' => $changesetID,
|
||||
'body_sent' => $xmlString,
|
||||
'response' => $response->getBody()->getContents(),
|
||||
]);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$status = "Erreur lors de la communication avec l'API OSM: " . $e->getMessage();
|
||||
$exception = true;
|
||||
$exception_message = $e->getMessage();
|
||||
|
||||
// Debug de la réponse en cas d'erreur
|
||||
if (method_exists($e, 'getResponse')) {
|
||||
var_dump($e->getResponse()->getBody()->getContents());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// après envoi on récupère les données
|
||||
$commerce_overpass = $this->motocultrice->get_osm_object_data($osm_kind, $osm_object_id);
|
||||
|
||||
|
@ -344,14 +367,14 @@ class PublicController extends AbstractController
|
|||
$this->entityManager->clear();
|
||||
|
||||
$stats = $place->getStats();
|
||||
if(!$stats) {
|
||||
if (!$stats) {
|
||||
$stats = $this->entityManager->getRepository(Stats::class)->findOneBy(['zip_code' => $place->getZipCode()]);
|
||||
}
|
||||
if(!$stats) {
|
||||
if (!$stats) {
|
||||
$stats = new Stats();
|
||||
$stats->setZipCode($place->getZipCode());
|
||||
}
|
||||
|
||||
|
||||
$stats->addPlace($place);
|
||||
$place->setStats($stats);
|
||||
$place->setModifiedDate(new \DateTime());
|
||||
|
@ -379,23 +402,31 @@ class PublicController extends AbstractController
|
|||
#[Route('/request_email_to_modify/{osm_object_id}', name: 'app_public_request_email')]
|
||||
public function request_email($osm_object_id): Response
|
||||
{
|
||||
$this->actionLogger->log('request_email_to_modify', [
|
||||
'osm_id' => $osm_object_id,
|
||||
]);
|
||||
|
||||
if ($this->getRequest()->isMethod('POST')) {
|
||||
$email = $this->getRequest()->request->get('email');
|
||||
|
||||
|
||||
try {
|
||||
// TODO: Implémenter l'envoi réel du mail
|
||||
|
||||
|
||||
$this->addFlash(
|
||||
'success',
|
||||
'Un email vous a été envoyé avec les instructions pour modifier ce lieu.'
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
$this->actionLogger->log('ERROR_request_email_to_modify', [
|
||||
'osm_id' => $osm_object_id,
|
||||
'exception_message' => $e->getMessage(),
|
||||
]);
|
||||
$this->addFlash(
|
||||
'error',
|
||||
'error',
|
||||
'Une erreur est survenue lors de l\'envoi de l\'email. Veuillez réessayer plus tard.'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return $this->redirectToRoute('app_public_index');
|
||||
}
|
||||
// TODO envoyer un email
|
||||
|
@ -409,6 +440,9 @@ class PublicController extends AbstractController
|
|||
#[Route('/closed_commerce/{osm_object_id}', name: 'app_public_closed_commerce')]
|
||||
public function closed_commerce($osm_object_id): Response
|
||||
{
|
||||
$this->actionLogger->log('closed_commerce', [
|
||||
'osm_id' => $osm_object_id,
|
||||
]);
|
||||
$place = $this->entityManager->getRepository(Place::class)->findOneBy(['osm_id' => $osm_object_id]);
|
||||
if (!$place) {
|
||||
$this->addFlash('warning', 'Ce commerce n\'existe pas.');
|
||||
|
@ -417,7 +451,7 @@ class PublicController extends AbstractController
|
|||
|
||||
$place->setClosed(true);
|
||||
$this->entityManager->flush();
|
||||
|
||||
|
||||
return $this->render('public/closed_commerce.html.twig', [
|
||||
'controller_name' => 'PublicController',
|
||||
]);
|
||||
|
@ -474,12 +508,33 @@ class PublicController extends AbstractController
|
|||
}
|
||||
|
||||
#[Route('/set_opted_out_place/{uuid}', name: 'app_public_set_opted_out_place')]
|
||||
public function set_opted_out_place($uuid): Response
|
||||
public function set_opted_out_place($uuid)
|
||||
{
|
||||
$place = $this->entityManager->getRepository(Place::class)->findOneBy(['uuid_for_url' => $uuid]);
|
||||
$this->actionLogger->log('set_place_opted_out', [
|
||||
'uuid' => $uuid,
|
||||
]);
|
||||
|
||||
if (!$place) {
|
||||
$this->addFlash('warning', 'Ce commerce n\'existe pas.');
|
||||
return $this->redirectToRoute('app_public_index');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[Route('/ask-for-help', name: 'app_public_ask_for_help')]
|
||||
public function askForHelp(Request $request): Response
|
||||
{
|
||||
|
||||
$this->actionLogger->log('ask_for_help', []);
|
||||
return $this->redirect('https://www.openstreetmap.fr/contact');
|
||||
}
|
||||
|
||||
#[Route('/logs/actions', name: 'app_public_action_logs')]
|
||||
public function listActionLogs(): Response
|
||||
{
|
||||
$logs = $this->actionLogger->getLastLogs(100);
|
||||
return $this->render('public/action_logs.html.twig', [
|
||||
'logs' => $logs
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
91
src/Entity/ActionLog.php
Normal file
91
src/Entity/ActionLog.php
Normal file
|
@ -0,0 +1,91 @@
|
|||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity]
|
||||
class ActionLog
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 100)]
|
||||
private ?string $type = null;
|
||||
|
||||
#[ORM\Column(type: 'json', nullable: true)]
|
||||
private ?array $data = null;
|
||||
|
||||
#[ORM\Column(type: 'datetime')]
|
||||
private ?\DateTimeInterface $createdAt = null;
|
||||
|
||||
#[ORM\Column(length: 500, nullable: true)]
|
||||
private ?string $from_url = null;
|
||||
|
||||
#[ORM\Column(length: 255, nullable: true)]
|
||||
private ?string $who = null;
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getType(): ?string
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
public function setType(string $type): self
|
||||
{
|
||||
$this->type = $type;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getData(): ?array
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
public function setData(?array $data): self
|
||||
{
|
||||
$this->data = $data;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCreatedAt(): ?\DateTimeInterface
|
||||
{
|
||||
return $this->createdAt;
|
||||
}
|
||||
|
||||
public function setCreatedAt(\DateTimeInterface $createdAt): self
|
||||
{
|
||||
$this->createdAt = $createdAt;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getFromUrl(): ?string
|
||||
{
|
||||
return $this->from_url;
|
||||
}
|
||||
|
||||
public function setFromUrl(?string $from_url): static
|
||||
{
|
||||
$this->from_url = $from_url;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getWho(): ?string
|
||||
{
|
||||
return $this->who;
|
||||
}
|
||||
|
||||
public function setWho(?string $who): static
|
||||
{
|
||||
$this->who = $who;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -196,7 +196,9 @@ class Stats
|
|||
$this->avec_site = 0;
|
||||
$this->avec_accessibilite = 0;
|
||||
$this->avec_note = 0;
|
||||
|
||||
$this->avec_siret = 0;
|
||||
$this->avec_name = 0;
|
||||
|
||||
$somme_completions = 0;
|
||||
|
||||
|
||||
|
@ -219,10 +221,13 @@ class Stats
|
|||
$this->avec_horaires++;
|
||||
$place_completions++;
|
||||
}
|
||||
if($place->hasNote()) {
|
||||
$this->avec_note++;
|
||||
if($place->getSiret()) {
|
||||
$this->avec_siret++;
|
||||
}
|
||||
$somme_completions += $place_completions / 5;
|
||||
if($place->getName()) {
|
||||
$this->avec_name++;
|
||||
}
|
||||
$somme_completions += $place_completions / 6;
|
||||
}
|
||||
|
||||
$this->setPlacesCount($places_count);
|
||||
|
|
43
src/Repository/ActionLogRepository.php
Normal file
43
src/Repository/ActionLogRepository.php
Normal file
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Entity\ActionLog;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<ActionLog>
|
||||
*/
|
||||
class ActionLogRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, ActionLog::class);
|
||||
}
|
||||
|
||||
// /**
|
||||
// * @return ActionLog[] Returns an array of ActionLog objects
|
||||
// */
|
||||
// public function findByExampleField($value): array
|
||||
// {
|
||||
// return $this->createQueryBuilder('a')
|
||||
// ->andWhere('a.exampleField = :val')
|
||||
// ->setParameter('val', $value)
|
||||
// ->orderBy('a.id', 'ASC')
|
||||
// ->setMaxResults(10)
|
||||
// ->getQuery()
|
||||
// ->getResult()
|
||||
// ;
|
||||
// }
|
||||
|
||||
// public function findOneBySomeField($value): ?ActionLog
|
||||
// {
|
||||
// return $this->createQueryBuilder('a')
|
||||
// ->andWhere('a.exampleField = :val')
|
||||
// ->setParameter('val', $value)
|
||||
// ->getQuery()
|
||||
// ->getOneOrNullResult()
|
||||
// ;
|
||||
// }
|
||||
}
|
48
src/Service/ActionLogger.php
Normal file
48
src/Service/ActionLogger.php
Normal file
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use App\Entity\ActionLog;
|
||||
|
||||
class ActionLogger
|
||||
{
|
||||
private $entityManager;
|
||||
|
||||
public function __construct(EntityManagerInterface $entityManager)
|
||||
{
|
||||
$this->entityManager = $entityManager;
|
||||
}
|
||||
|
||||
public function log(string $type, array $data = []): void
|
||||
{
|
||||
$log = new ActionLog();
|
||||
$log->setType($type);
|
||||
$log->setData($data);
|
||||
$log->setCreatedAt(new \DateTime());
|
||||
|
||||
if (isset($data['from_url'])) {
|
||||
$log->setFromUrl($data['from_url']);
|
||||
} else if (isset($_SERVER['HTTP_REFERER'])) {
|
||||
$log->setFromUrl($_SERVER['HTTP_REFERER']);
|
||||
}
|
||||
if (isset($data['who'])) {
|
||||
$log->setWho($data['who']);
|
||||
}
|
||||
$this->entityManager->persist($log);
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ActionLog[]
|
||||
*/
|
||||
public function getLastLogs(int $limit = 100): array
|
||||
{
|
||||
return $this->entityManager->getRepository(ActionLog::class)
|
||||
->createQueryBuilder('a')
|
||||
->orderBy('a.createdAt', 'DESC')
|
||||
->setMaxResults($limit)
|
||||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue