Skip to content

Commit

Permalink
Merge pull request #22 from smartbooster/entity_trait_mapping_annotat…
Browse files Browse the repository at this point in the history
…ions

Entity trait mapping annotations + ApiCallMonitor
  • Loading branch information
mathieu-ducrot authored Apr 2, 2024
2 parents 7ea084b + 0c2ba95 commit 67a4db5
Show file tree
Hide file tree
Showing 14 changed files with 161 additions and 2 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
CHANGELOG for 1.x
===================
## v1.2.2 - (2024-04-02)
### Added
- Add mapping comment on EntityTrait to be compatible with doctrine annotations type
- `ApiCallMonitor` to manage start/end ApiCallInterface
- `ProcessInterface::addExceptionTraceData` function to store the exception trace

### Fixed
- `ProcessTrait::addLog` Switch adding log at the end instead of the beginning

## v1.2.1 - (2024-03-27)
### Fix
### Fixed

- `ArrayUtils::checkIssetKeys` : Modify method with not strict comparaison

Expand Down
3 changes: 3 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ services:
- setEntityManager: [ '@Doctrine\ORM\EntityManagerInterface' ]
tags: [ 'controller.service_arguments' ]
# Monitoring
Smart\CoreBundle\Monitoring\ApiCallMonitor:
arguments:
- '@Smart\CoreBundle\Monitoring\ProcessMonitor'
Smart\CoreBundle\Monitoring\ProcessMonitor:
arguments:
- '@Doctrine\ORM\EntityManagerInterface'
Expand Down
16 changes: 16 additions & 0 deletions src/Entity/AddressTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,34 @@

trait AddressTrait
{
/**
* @ORM\Column(length=255, nullable=true)
* @Assert\Length(max=255)
*/
#[ORM\Column(length: 255, nullable: true)]
#[Assert\Length(max: 255)]
private ?string $address = null;

/**
* @ORM\Column(length=255, nullable=true)
* @Assert\Length(max=255)
*/
#[ORM\Column(length: 255, nullable: true)]
#[Assert\Length(max: 255)]
private ?string $additionalAddress = null;

/**
* @ORM\Column(length=10, nullable=true)
* @Assert\Length(max=10)
*/
#[ORM\Column(length: 10, nullable: true)]
#[Assert\Length(max: 10)]
private ?string $postalCode = null;

/**
* @ORM\Column(length=255, nullable=true)
* @Assert\Length(max=255)
*/
#[ORM\Column(length: 255, nullable: true)]
#[Assert\Length(max: 255)]
private ?string $city = null;
Expand Down
17 changes: 17 additions & 0 deletions src/Entity/ApiCallTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,43 @@ trait ApiCallTrait
{
use ProcessTrait;

/**
* @ORM\Column(length=20)
*/
#[ORM\Column(length: 20)]
private ?string $origin = null;

/**
* @ORM\Column
*/
#[ORM\Column]
private ?int $statusCode = null;

/**
* @ORM\Column(length=10)
*/
#[ORM\Column(length: 10)]
private ?string $method = null;

/**
* The routeUrl property contains the full url used to call the API
* On other hand, the route alias will be stored in the type property from the ProcessTrait. Storing the route alias this way can help you build
* api stats call on top this trait/interface.
*
* @ORM\Column(type=Types::TEXT)
*/
#[ORM\Column(type: Types::TEXT)]
private ?string $routeUrl = null;

/**
* @ORM\Column(nullable=true)
*/
#[ORM\Column(nullable: true)]
private ?array $inputData = null;

/**
* @ORM\Column(type=Types::JSON, nullable=true)
*/
#[ORM\Column(type: Types::JSON, nullable: true)]
private array|string|null $outputResponse = null;

Expand Down
5 changes: 5 additions & 0 deletions src/Entity/EmailTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

trait EmailTrait
{
/**
* @ORM\Column(length=255, nullable=true)
* @Assert\Length(max=255)
* @Assert\Email
*/
#[ORM\Column(length: 255, nullable: true)]
#[Assert\Length(max: 255)]
#[Assert\Email]
Expand Down
5 changes: 5 additions & 0 deletions src/Entity/NameableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

trait NameableTrait
{
/**
* @ORM\Column(length=255)
* @Assert\Length(max=255)
* @Assert\NotBlank
*/
#[ORM\Column(length: 255)]
#[Assert\Length(max: 255)]
#[Assert\NotBlank]
Expand Down
9 changes: 9 additions & 0 deletions src/Entity/OrganizationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,20 @@ trait OrganizationTrait
use PhoneTrait;
use WebsiteTrait;

/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column
*/
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;

/**
* @ORM\Column(length=255, nullable=true)
* @Assert\Length(max=255)
*/
#[ORM\Column(length: 255, nullable: true)]
#[Assert\Length(max: 255)]
private ?string $organizationEmail = null;
Expand Down
8 changes: 8 additions & 0 deletions src/Entity/PersonNameableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@

trait PersonNameableTrait
{
/**
* @ORM\Column(length=100, nullable=true)
* @Assert\Length(max=100)
*/
#[ORM\Column(length: 100, nullable: true)]
#[Assert\Length(max: 100)]
protected ?string $firstName = null;

/**
* @ORM\Column(length=100, nullable=true)
* @Assert\Length(max=100)
*/
#[ORM\Column(length: 100, nullable: true)]
#[Assert\Length(max: 100)]
protected ?string $lastName = null;
Expand Down
5 changes: 5 additions & 0 deletions src/Entity/PhoneTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

trait PhoneTrait
{
/**
* @ORM\Column(length=20, nullable=true)
* @Assert\Length(max=20)
* @Assert\Regex(pattern=RegexUtils::PHONE_PATTERN, message=RegexUtils::PHONE_MESSAGE)
*/
#[ORM\Column(length: 20, nullable: true)]
#[Assert\Length(max: 20)]
#[Assert\Regex(pattern: RegexUtils::PHONE_PATTERN, message: RegexUtils::PHONE_MESSAGE)]
Expand Down
2 changes: 2 additions & 0 deletions src/Entity/ProcessInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@ public function getData(): ?array;
public function setData(?array $data): static;

public function addData(string $key, mixed $value): void;

public function addExceptionTraceData(\Exception $e): void;
}
38 changes: 37 additions & 1 deletion src/Entity/ProcessTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,63 @@

trait ProcessTrait
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column
*/
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;

/**
* @ORM\Column(length=50)
*/
#[ORM\Column(length: 50)]
private ?string $type = null;

/**
* @ORM\Column(type=Types::DATETIME_MUTABLE)
*/
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
private ?\DateTimeInterface $startedAt = null;

/**
* @ORM\Column(type=Types::DATETIME_MUTABLE, nullable=true)
*/
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $endedAt = null;

/**
* Duration in seconds between the start and end times
*
* @ORM\Column(nullable=true)
*/
#[ORM\Column(nullable: true)]
private ?int $duration = null;

/**
* @ORM\Column(length=15, enumType=ProcessStatusEnum::class)
*/
#[ORM\Column(length: 15, enumType: ProcessStatusEnum::class)]
private ?ProcessStatusEnum $status = null;

/**
* @ORM\Column(type=Types::TEXT, nullable=true)
*/
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $summary = null;

/**
* @ORM\Column(nullable=true)
*/
#[ORM\Column(nullable: true)]
private ?array $logs = null;

/**
* @ORM\Column(nullable=true)
*/
#[ORM\Column(nullable: true)]
private ?array $data = null;

Expand Down Expand Up @@ -170,7 +198,7 @@ public function addLog(mixed $log): self
if ($this->logs == null) {
$this->logs = [];
}
array_unshift($this->logs, $log);
$this->logs[] = $log;

return $this;
}
Expand All @@ -191,4 +219,12 @@ public function addData(string $key, mixed $value): void
{
$this->data[$key] = $value;
}

/**
* Use this if you wish to store the exception trace on the internal data of the process
*/
public function addExceptionTraceData(\Exception $e): void
{
$this->data['exception_trace'] = $e->getTrace();
}
}
5 changes: 5 additions & 0 deletions src/Entity/SirenTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

trait SirenTrait
{
/**
* @ORM\Column(length=20, nullable=true)
* @Assert\Length(max=20)
* @Assert\Regex(pattern=RegexUtils::SIREN_PATTERN, message=RegexUtils::SIREN_MESSAGE)
*/
#[ORM\Column(length: 20, nullable: true)]
#[Assert\Length(max: 20)]
#[Assert\Regex(pattern: RegexUtils::SIREN_PATTERN, message: RegexUtils::SIREN_MESSAGE)]
Expand Down
5 changes: 5 additions & 0 deletions src/Entity/WebsiteTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

trait WebsiteTrait
{
/**
* @ORM\Column(length=300, nullable=true)
* @Assert\Length(max=300)
* @Assert\Url
*/
#[ORM\Column(length: 300, nullable: true)]
#[Assert\Length(max: 300)]
#[Assert\Url]
Expand Down
34 changes: 34 additions & 0 deletions src/Monitoring/ApiCallMonitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Smart\CoreBundle\Monitoring;

use Smart\CoreBundle\Entity\ApiCallInterface;
use Smart\CoreBundle\Entity\ProcessInterface;
use Symfony\Component\HttpFoundation\Request;

/**
* @author Mathieu Ducrot <[email protected]>
*/
class ApiCallMonitor
{
public function __construct(private readonly ProcessMonitor $processMonitor)
{
}

public function start(ApiCallInterface $apiCall, Request $request): ApiCallInterface
{
$this->processMonitor->start($apiCall);
$apiCall->setMethod($request->getMethod());
$apiCall->setRouteUrl($request->getUri());
$apiCall->setType($request->attributes->get('_route'));
$apiCall->setInputData($request->request->all());

return $apiCall;
}

public function end(ApiCallInterface $apiCall, int $statusCode, bool $flush = true): void
{
$apiCall->setStatusCode($statusCode);
$this->processMonitor->end($apiCall, $statusCode >= 200 && $statusCode < 300, $flush);
}
}

0 comments on commit 67a4db5

Please sign in to comment.