Skip to content

Commit

Permalink
Add entity map provider
Browse files Browse the repository at this point in the history
  • Loading branch information
subiabre committed Dec 13, 2024
1 parent e3230c6 commit fcbdacf
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/Entity/Project/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
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
{
Expand Down
3 changes: 3 additions & 0 deletions src/Entity/Project/Reward.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,6 +14,7 @@
/**
* 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
{
Expand Down
9 changes: 6 additions & 3 deletions src/Entity/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
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;
Expand All @@ -24,10 +26,11 @@
* 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;
Expand Down Expand Up @@ -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;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Mapping/AutoMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __construct(
iterable $mapProviders,
) {
$this->innerMapper = InnerMapper::create(
providers: $mapProviders
providers: \iterator_to_array($mapProviders)
);
}

Expand Down
28 changes: 28 additions & 0 deletions src/Mapping/Provider/EntityMapProvider.php
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit fcbdacf

Please sign in to comment.