From d5844564ed12189620787b4b608097bcc36aed7b Mon Sep 17 00:00:00 2001
From: pfleu <fleutotp@gmail.com>
Date: Fri, 6 Jan 2023 18:31:15 +0100
Subject: [PATCH] Ajout type DateTimeMicroseconds. DEV : on utilise plus le
 firewall api pour utilisation plus simple du swagger

---
 config/packages/doctrine.yaml                 |  2 +
 config/packages/security.yaml                 | 14 ++--
 .../Types/DateTimeMicrosecondsType.php        | 71 +++++++++++++++++++
 src/Entity/MetaInfo.php                       |  2 +-
 src/Entity/Trace.php                          | 18 +++++
 5 files changed, 99 insertions(+), 8 deletions(-)
 create mode 100644 src/Doctrine/Types/DateTimeMicrosecondsType.php

diff --git a/config/packages/doctrine.yaml b/config/packages/doctrine.yaml
index 95228da..e1983cc 100644
--- a/config/packages/doctrine.yaml
+++ b/config/packages/doctrine.yaml
@@ -1,5 +1,7 @@
 doctrine:
     dbal:
+        types:
+            datetimemicroseconds: App\Doctrine\Types\DateTimeMicrosecondsType
         url: '%env(resolve:DATABASE_URL)%'
 
         # IMPORTANT: You MUST configure your server version,
diff --git a/config/packages/security.yaml b/config/packages/security.yaml
index 0538c94..c1989d4 100644
--- a/config/packages/security.yaml
+++ b/config/packages/security.yaml
@@ -18,12 +18,12 @@ security:
         api_token:
             pattern: ^/api/token$
             security: false
-        api:
-            pattern: ^/api(?!/doc$) # Accepts routes under /api except /api/doc (pour api/doc, on utilisera donc le firewall "main" ce qui permettra d'accéder au swagger quand on est authentifié via Session PHP avec le role Admin
-            security: true
-            stateless: true # Pas d'authentification, pas de session utilisateur (mais le compte user est vérifié via le token)
-            oauth2: true
-            user_checker: App\Security\EasyUserChecker
+#        api:
+#            pattern: ^/api(?!/doc$) # Accepts routes under /api except /api/doc (pour api/doc, on utilisera donc le firewall "main" ce qui permettra d'accéder au swagger quand on est authentifié via Session PHP avec le role Admin
+#            security: true
+#            stateless: true # Pas d'authentification, pas de session utilisateur (mais le compte user est vérifié via le token)
+#            oauth2: true
+#            user_checker: App\Security\EasyUserChecker
 
         dev:
             pattern: ^/(_(profiler|wdt)|css|images|js)/
@@ -62,7 +62,7 @@ security:
         - { path: ^/token, role: PUBLIC_ACCESS }
         - { path: ^/.well-known, roles: PUBLIC_ACCESS }
         - { path: ^/api/doc, role: ROLE_ADMIN } # swagger Firewall: main
-        - { path: ^/api, roles: [ROLE_OAUTH2_EMAIL] } #   Firewall: Api
+        - { path: ^/api, roles: [ROLE_OAUTH2_EMAIL, ROLE_ADMIN] } #   Firewall: Api
         - { path: ^/, role: ROLE_USER }
 
 when@test:
diff --git a/src/Doctrine/Types/DateTimeMicrosecondsType.php b/src/Doctrine/Types/DateTimeMicrosecondsType.php
new file mode 100644
index 0000000..7b35737
--- /dev/null
+++ b/src/Doctrine/Types/DateTimeMicrosecondsType.php
@@ -0,0 +1,71 @@
+<?php
+
+namespace App\Doctrine\Types;
+
+use DateTime;
+use DateTimeInterface;
+use Doctrine\DBAL\Types\Type;
+use Doctrine\DBAL\Platforms\AbstractPlatform;
+use Doctrine\DBAL\Types\ConversionException;
+
+/**
+ * Override datetime datatype to support microseconds
+ */
+class DateTimeMicrosecondsType extends Type
+{
+    const TYPENAME = 'datetimemicroseconds'; // modify to match your type name
+
+    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
+    {
+        if (isset($fieldDeclaration['version']) && $fieldDeclaration['version'] == true) {
+            return 'TIMESTAMP';
+        }
+
+        return 'DATETIME(6)';
+    }
+
+    public function convertToPHPValue($value, AbstractPlatform $platform)
+    {
+        if ($value === null || $value instanceof DateTimeInterface) {
+            return $value;
+        }
+
+        $val = DateTime::createFromFormat('Y-m-d H:i:s.u', $value);
+
+        if ( ! $val) {
+            $val = date_create($value);
+        }
+
+        if ( ! $val) {
+            throw ConversionException::conversionFailedFormat(
+                $value,
+                $this->getName(),
+                'Y-m-d H:i:s.u'
+            );
+        }
+
+        return $val;
+    }
+
+    public function convertToDatabaseValue($value, AbstractPlatform $platform)
+    {
+        if (null === $value) {
+            return $value;
+        }
+
+        if ($value instanceof DateTimeInterface) {
+            return $value->format('Y-m-d H:i:s.u');
+        }
+
+        throw ConversionException::conversionFailedInvalidType(
+            $value,
+            $this->getName(),
+            ['null', 'DateTime']
+        );
+    }
+
+    public function getName()
+    {
+        return self::TYPENAME;
+    }
+}
\ No newline at end of file
diff --git a/src/Entity/MetaInfo.php b/src/Entity/MetaInfo.php
index beea0ba..76fcb47 100644
--- a/src/Entity/MetaInfo.php
+++ b/src/Entity/MetaInfo.php
@@ -53,7 +53,7 @@ class MetaInfo
      * @ORM\ManyToOne(targetEntity=Graphy::class, inversedBy="metaInfos")
      * @Groups({"metaInfo:read"})
      */
-    private $createdBy;
+    private $graphies;
 
     public function __toString()
     {
diff --git a/src/Entity/Trace.php b/src/Entity/Trace.php
index 84829ff..f813bd8 100644
--- a/src/Entity/Trace.php
+++ b/src/Entity/Trace.php
@@ -27,6 +27,12 @@ class Trace
      */
     private $createdAt;
 
+    /**
+     * @Groups({"trace:read"})
+     * @ORM\Column(type="datetimemicroseconds")
+     */
+    private $timestamp;
+
     /**
      * @ORM\ManyToOne(targetEntity=User::class, inversedBy="traces")
      * @Groups({"trace:read"})
@@ -126,4 +132,16 @@ class Trace
 
         return $this;
     }
+
+    public function getTimestamp()
+    {
+        return $this->timestamp;
+    }
+
+    public function setTimestamp($timestamp): self
+    {
+        $this->timestamp = $timestamp;
+
+        return $this;
+    }
 }
-- 
GitLab