Skip to content

Commit

Permalink
Merge pull request #20 from smartbooster/common_entity_trait_interface
Browse files Browse the repository at this point in the history
Common entity trait interface + new ProcessMonitor feature
  • Loading branch information
mathieu-ducrot authored Mar 27, 2024
2 parents 1c772b9 + 6dca966 commit b16383c
Show file tree
Hide file tree
Showing 30 changed files with 1,009 additions and 3 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
CHANGELOG for 1.x
===================
## v1.2.0 - (2024-03-27)
### Added
- Common Entity Interface and Trait such as the `ProcessInterface` which we will use to monitor cron, api and file generation.
- `ProcessMonitor` to centralize process code management
- `CommandPoolHelper` service to fetch data about the project symfony commands (like getting all cron choices)
- `DateUtils::secondsToString` helper to convert seconds into a small summary string
- `IniOverrideConfig::initDefaultTimezoneForCli` helper to properly set the timezone when using date with PHP CLI on CleverCloud
- `ApiCallInterface` and trait to ease monitoring API calls

### Fixed
- `RegexUtils::PHONE_PATTERN` remove wrong extra digit needed on foreign number

## v1.1.0 - (2024-03-25)
### Added
- new `ArrayUtils` methods : `checkIssetKeys`, `trimExplode`, `removeEmpty`, `filterByPattern`, `flatToMap`
Expand Down
9 changes: 9 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
services:
# Command
Smart\CoreBundle\Command\CommandPoolHelper:
arguments:
- '@Symfony\Component\HttpKernel\KernelInterface'
- '@translator'
# Config
Smart\CoreBundle\Config\IniOverrideConfig:
arguments:
Expand All @@ -9,6 +14,10 @@ services:
- setContainer: [ '@service_container' ]
- setEntityManager: [ '@Doctrine\ORM\EntityManagerInterface' ]
tags: [ 'controller.service_arguments' ]
# Monitoring
Smart\CoreBundle\Monitoring\ProcessMonitor:
arguments:
- '@Doctrine\ORM\EntityManagerInterface'
# Route
Smart\CoreBundle\Route\RouteLoader:
tags:
Expand Down
57 changes: 57 additions & 0 deletions src/Command/CommandPoolHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Smart\CoreBundle\Command;

use Smart\CoreBundle\Utils\ArrayUtils;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\HttpKernel\KernelInterface;

/**
* @author Mathieu Ducrot <[email protected]>
*/
class CommandPoolHelper
{
public function __construct(private readonly KernelInterface $kernel)
{
}

/**
* Return all commands indexed by a given type
* @return Command[]
*/
public function getCommands(string $type): array
{
$application = new Application($this->kernel);

return array_filter($application->all(), function ($key) use ($type) {
return str_starts_with($key, "$type:");
}, ARRAY_FILTER_USE_KEY);
}

/**
* Parse commands from a given type and return an associative array that can be used in form choice type
* @return array
* [
* 'app.my_command.label' => 'my-command',
* 'app.my_other_command.label' => 'my-other-command',
* ]
*/
public function getCommandsChoices(string $type): array
{
return ArrayUtils::flatToMap(
array_keys($this->getCommands($type)),
function ($key) {
return str_replace([':', '-'], ['.', '_'], $key) . '.label';
},
function ($value) use ($type) {
return str_replace("$type:", '', $value);
}
);
}

public function getCronChoices(): array
{
return $this->getCommandsChoices('cron');
}
}
11 changes: 11 additions & 0 deletions src/Config/IniOverrideConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,15 @@ public function resetMemoryLimit(): void
{
ini_set('memory_limit', $this->getDefaultMemoryLimit());
}

/**
* Call this function for all PHP script running from the CLI (ex: all symfony cron commands) when you are on CleverCloud to ensure every date
* creation or comparaison are using the right timezone.
* Alternativaly you can also set it directly when invoking the PHP command using the -d option like so :
* php -d date.timezone="Europe/Paris" bin/console app:my-command
*/
public function initDefaultTimezoneForCli(string $timezone = 'Europe/Paris'): void
{
date_default_timezone_set($timezone);
}
}
14 changes: 14 additions & 0 deletions src/Entity/AddressInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Smart\CoreBundle\Entity;

interface AddressInterface
{
public function getAddress(): ?string;

public function getAdditionalAddress(): ?string;

public function getPostalCode(): ?string;

public function getCity(): ?string;
}
76 changes: 76 additions & 0 deletions src/Entity/AddressTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Smart\CoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

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

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

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

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

public function getAddress(): ?string
{
return $this->address;
}

public function setAddress(?string $address): self
{
$this->address = $address;

return $this;
}

public function getAdditionalAddress(): ?string
{
return $this->additionalAddress;
}

public function setAdditionalAddress(?string $additionalAddress): self
{
$this->additionalAddress = $additionalAddress;

return $this;
}

public function getPostalCode(): ?string
{
return $this->postalCode;
}

public function setPostalCode(?string $postalCode): self
{
if (strlen($postalCode) === 4) {
$postalCode = '0' . $postalCode;
}
$this->postalCode = $postalCode;

return $this;
}

public function getCity(): ?string
{
return $this->city;
}

public function setCity(?string $city): self
{
$this->city = $city;

return $this;
}
}
30 changes: 30 additions & 0 deletions src/Entity/ApiCallInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Smart\CoreBundle\Entity;

interface ApiCallInterface extends ProcessInterface
{
public function getOrigin(): ?string;

public function setOrigin(string $origin): static;

public function getStatusCode(): ?int;

public function setStatusCode(int $statusCode): static;

public function getMethod(): ?string;

public function setMethod(string $method): static;

public function getRouteUrl(): ?string;

public function setRouteUrl(string $routeUrl): static;

public function getInputData(): ?array;

public function setInputData(?array $inputData): static;

public function getOutputResponse(): array|string|null;

public function setOutputResponse(array|string|null $outputResponse): static;
}
116 changes: 116 additions & 0 deletions src/Entity/ApiCallTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace Smart\CoreBundle\Entity;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

trait ApiCallTrait
{
use ProcessTrait;

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

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

#[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)]
private ?string $routeUrl = null;

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

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

public function __toString(): string
{
return $this->statusCode . ' - ' . $this->getRouteUrl();
}

public function getId(): ?int
{
return $this->id;
}

public function getOrigin(): ?string
{
return $this->origin;
}

public function setOrigin(string $origin): static
{
$this->origin = $origin;

return $this;
}

public function getStatusCode(): ?int
{
return $this->statusCode;
}

public function setStatusCode(int $statusCode): static
{
$this->statusCode = $statusCode;

return $this;
}

public function getMethod(): ?string
{
return $this->method;
}

public function setMethod(string $method): static
{
$this->method = $method;

return $this;
}

public function getRouteUrl(): ?string
{
return $this->routeUrl;
}

public function setRouteUrl(string $routeUrl): static
{
$this->routeUrl = $routeUrl;

return $this;
}

public function getInputData(): ?array
{
return $this->inputData;
}

public function setInputData(?array $inputData): static
{
$this->inputData = $inputData;

return $this;
}

public function getOutputResponse(): array|string|null
{
return $this->outputResponse;
}

public function setOutputResponse(array|string|null $outputResponse): static
{
$this->outputResponse = $outputResponse;

return $this;
}
}
10 changes: 10 additions & 0 deletions src/Entity/EmailInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Smart\CoreBundle\Entity;

interface EmailInterface
{
public function getEmail(): ?string;

public function setEmail(?string $email): self;
}
30 changes: 30 additions & 0 deletions src/Entity/EmailTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Smart\CoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

trait EmailTrait
{
#[ORM\Column(length: 255, nullable: true)]
#[Assert\Length(max: 255)]
#[Assert\Email]
private ?string $email = null;

public function getEmail(): ?string
{
return $this->email;
}

public function setEmail(?string $email): self
{
$email = strtolower($email);
if ($email === '') {
$email = null;
}
$this->email = $email;

return $this;
}
}
Loading

0 comments on commit b16383c

Please sign in to comment.