|
|
@@ -3,6 +3,9 @@
|
|
|
namespace App\DataPersister;
|
|
|
|
|
|
use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
|
|
|
+use App\Service\OnChange\Organization\OnChangeContext;
|
|
|
+use App\Service\OnChange\Organization\OnChangeDefault;
|
|
|
+use App\Service\OnChange\Organization\OnChangeInterface;
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
use Symfony\Contracts\Service\Attribute\Required;
|
|
|
|
|
|
@@ -12,16 +15,18 @@ use Symfony\Contracts\Service\Attribute\Required;
|
|
|
*/
|
|
|
class BaseDataPersister implements ContextAwareDataPersisterInterface
|
|
|
{
|
|
|
- protected array $context = [];
|
|
|
-
|
|
|
// <-- dependencies injections
|
|
|
- protected EntityManagerInterface $entityManager;
|
|
|
protected ContextAwareDataPersisterInterface $contextAwareDataPersister;
|
|
|
+ protected OnChangeInterface $onChange;
|
|
|
|
|
|
#[Required]
|
|
|
public function setContextAwareDataPersister(ContextAwareDataPersisterInterface $contextAwareDataPersister): void {$this->contextAwareDataPersister = $contextAwareDataPersister;}
|
|
|
- #[Required]
|
|
|
- public function setEntityManager(EntityManagerInterface $entityManager): void {$this->entityManager = $entityManager;}
|
|
|
+
|
|
|
+ public function __construct(
|
|
|
+ OnChangeDefault $onChange
|
|
|
+ ) {
|
|
|
+ $this->onChange = $onChange;
|
|
|
+ }
|
|
|
// dependencies injections -->
|
|
|
|
|
|
public function supports($data, array $context = []): bool
|
|
|
@@ -31,55 +36,21 @@ class BaseDataPersister implements ContextAwareDataPersisterInterface
|
|
|
|
|
|
public function persist($data, array $context = [])
|
|
|
{
|
|
|
- $this->context = $context;
|
|
|
+ $onChangeContext = new OnChangeContext($context);
|
|
|
|
|
|
- $this->validate($data);
|
|
|
+ $this->onChange->validate($data, $onChangeContext);
|
|
|
|
|
|
- $data = $this->preProcess($data);
|
|
|
+ $data = $this->onChange->preProcess($data, $onChangeContext);
|
|
|
|
|
|
- $this->prePersist($data);
|
|
|
+ $this->onChange->beforeChange($data, $onChangeContext);
|
|
|
|
|
|
$result = $this->contextAwareDataPersister->persist($data, $context);
|
|
|
|
|
|
- $this->postPersist($data);
|
|
|
+ $this->onChange->onChange($data, $onChangeContext);
|
|
|
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Validation de la nouvelle donnée
|
|
|
- *
|
|
|
- * @param $data
|
|
|
- */
|
|
|
- protected function validate($data): void {
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Transformation de la donnée en entrée
|
|
|
- *
|
|
|
- * @param $data
|
|
|
- * @return mixed
|
|
|
- */
|
|
|
- protected function preProcess($data) {
|
|
|
- return $data;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Opérations avant mise à jour
|
|
|
- *
|
|
|
- * @param $data
|
|
|
- */
|
|
|
- protected function prePersist($data): void {
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Opérations après mise à jour
|
|
|
- *
|
|
|
- * @param $data
|
|
|
- */
|
|
|
- protected function postPersist($data): void {
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* La fonction native 'remove' n'est pas supportée par défaut, la réimplémenter au besoin
|
|
|
*
|
|
|
@@ -89,31 +60,4 @@ class BaseDataPersister implements ContextAwareDataPersisterInterface
|
|
|
public function remove($data, array $context = []): void {
|
|
|
throw new \RuntimeException('not supported', 500);
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * La requête traitée est de type POST
|
|
|
- *
|
|
|
- * @return bool
|
|
|
- */
|
|
|
- protected function isPostRequest(): bool {
|
|
|
- return $this->context['collection_operation_name'] ?? null === 'post';
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * La requête traitée est de type PUT
|
|
|
- *
|
|
|
- * @return bool
|
|
|
- */
|
|
|
- protected function isPutRequest(): bool {
|
|
|
- return $this->context['item_operation_name'] ?? null === 'put';
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Retourne l'entité existante (avant mise à jour) si elle existe, null sinon
|
|
|
- *
|
|
|
- * @return mixed|null
|
|
|
- */
|
|
|
- protected function previousData() {
|
|
|
- return $this->context['previous_data'] ?? null;
|
|
|
- }
|
|
|
}
|