Add entity map provider

This commit is contained in:
2024-12-13 14:30:36 +01:00
parent e3230c6b9e
commit fcbdacffdf
5 changed files with 41 additions and 4 deletions
+3
View File
@@ -9,11 +9,14 @@ use App\Entity\Trait\MigratedEntity;
use App\Entity\Trait\TimestampedCreationEntity;
use App\Entity\Trait\TimestampedUpdationEntity;
use App\Entity\User\User;
use App\Mapping\Provider\EntityMapProvider;
use App\Repository\Project\ProjectRepository;
use AutoMapper\Attribute\MapProvider;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[MapProvider(EntityMapProvider::class)]
#[ORM\Entity(repositoryClass: ProjectRepository::class)]
class Project implements UserOwnedInterface, AccountingOwnerInterface
{
+3
View File
@@ -3,7 +3,9 @@
namespace App\Entity\Project;
use App\Entity\Money;
use App\Mapping\Provider\EntityMapProvider;
use App\Repository\Project\RewardRepository;
use AutoMapper\Attribute\MapProvider;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
@@ -12,6 +14,7 @@ use Doctrine\ORM\Mapping as ORM;
/**
* A ProjectReward is something the Project owner wishes to give in exchange for contributions to their Project.
*/
#[MapProvider(EntityMapProvider::class)]
#[ORM\Entity(repositoryClass: RewardRepository::class)]
class Reward
{
+6 -3
View File
@@ -8,7 +8,9 @@ use App\Entity\Project\Project;
use App\Entity\Trait\MigratedEntity;
use App\Entity\Trait\TimestampedCreationEntity;
use App\Entity\Trait\TimestampedUpdationEntity;
use App\Mapping\Provider\EntityMapProvider;
use App\Repository\User\UserRepository;
use AutoMapper\Attribute\MapProvider;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
@@ -24,10 +26,11 @@ use Symfony\Component\Security\Core\User\UserInterface;
* This allows to keep an User's "wallet", withholding their non-raised fundings into their Accounting.
*/
#[Gedmo\Loggable()]
#[UniqueEntity(fields: ['username'], message: 'This usernames already exists.')]
#[UniqueEntity(fields: ['email'], message: 'This email address is already registered.')]
#[MapProvider(EntityMapProvider::class)]
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Index(fields: ['migratedId'])]
#[UniqueEntity(fields: ['username'], message: 'This usernames already exists.')]
#[UniqueEntity(fields: ['email'], message: 'This email address is already registered.')]
class User implements UserInterface, PasswordAuthenticatedUserInterface, AccountingOwnerInterface
{
use MigratedEntity;
@@ -68,7 +71,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface, Account
/**
* The projects owned by this User.
*/
#[ORM\OneToMany(mappedBy: 'owner', targetEntity: Project::class)]
#[ORM\OneToMany(mappedBy: 'owner', targetEntity: Project::class, cascade: ['persist'])]
private Collection $projects;
/**
+1 -1
View File
@@ -15,7 +15,7 @@ class AutoMapper implements AutoMapperInterface
iterable $mapProviders,
) {
$this->innerMapper = InnerMapper::create(
providers: $mapProviders
providers: \iterator_to_array($mapProviders)
);
}
@@ -0,0 +1,28 @@
<?php
namespace App\Mapping\Provider;
use AutoMapper\Provider\ProviderInterface;
use Doctrine\ORM\EntityManagerInterface;
class EntityMapProvider implements ProviderInterface
{
public function __construct(
private EntityManagerInterface $entityManager
) {}
public function provide(string $targetType, mixed $source, array $context): object|array|null
{
if (!isset($source->id)) {
return null;
}
$repository = $this->entityManager->getRepository($targetType);
if (!$repository) {
throw new \Exception(\sprintf("No repository found for '' class. Is it an Entity?", $targetType));
}
return $repository->find($source->id);
}
}