From f7f83a7e4dab73fd2ae8d3068feccdfa8f38a0f7 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 12 Feb 2024 16:13:41 +0100 Subject: [PATCH] wip --- config/packages/lexik_jwt_authentication.yaml | 2 +- src/ApiResource/ContactApi.php | 8 ++++---- src/Entity/User.php | 5 +++++ src/Mapper/ContactEntityToApiMapper.php | 8 +++++++- src/Mapper/UserApiToEntityMapper.php | 3 ++- src/State/EntityClassDtoStateProcessor.php | 7 +++++++ src/State/EntityToDtoStateProvider.php | 6 ++++-- tests/Functional/UserResourceTest.php | 2 +- 8 files changed, 31 insertions(+), 10 deletions(-) diff --git a/config/packages/lexik_jwt_authentication.yaml b/config/packages/lexik_jwt_authentication.yaml index d7b6820..4fc552c 100644 --- a/config/packages/lexik_jwt_authentication.yaml +++ b/config/packages/lexik_jwt_authentication.yaml @@ -2,4 +2,4 @@ lexik_jwt_authentication: secret_key: '%env(resolve:JWT_SECRET_KEY)%' public_key: '%env(resolve:JWT_PUBLIC_KEY)%' pass_phrase: '%env(JWT_PASSPHRASE)%' - token_ttl: 3600 \ No newline at end of file + token_ttl: 36000 \ No newline at end of file diff --git a/src/ApiResource/ContactApi.php b/src/ApiResource/ContactApi.php index e520b74..f52e2fc 100644 --- a/src/ApiResource/ContactApi.php +++ b/src/ApiResource/ContactApi.php @@ -10,9 +10,9 @@ namespace App\ApiResource; use ApiPlatform\Doctrine\Orm\State\Options; use ApiPlatform\Metadata\ApiProperty; use ApiPlatform\Metadata\ApiResource; +use App\Entity\Contact; use App\Entity\MediaObject; use App\Entity\Partner; -use App\Enum\PartnerType; use App\State\EntityClassDtoStateProcessor; use App\State\EntityToDtoStateProvider; use ApiPlatform\Metadata\Delete; @@ -43,7 +43,7 @@ use Symfony\Component\Validator\Constraints\NotBlank; security: 'is_granted("ROLE_USER")', provider: EntityToDtoStateProvider::class, processor: EntityClassDtoStateProcessor::class, - stateOptions: new Options(entityClass: Partner::class), + stateOptions: new Options(entityClass: Contact::class), )] class ContactApi { @@ -56,11 +56,11 @@ class ContactApi #[NotBlank] public ?string $lastName = null; - public ?Partner $partner = null; + public ?PartnerApi $partner = null; public ?\DateTimeInterface $birthday = null; - public ?MediaObject $image = null; + public ?MediaObject $image = null; public ?string $position = null; diff --git a/src/Entity/User.php b/src/Entity/User.php index 8b5aff6..35c4540 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -160,6 +160,11 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface return $this->postings; } + public function setPostings(Collection $postings): void + { + $this->postings = $postings; + } + public function addUserPost(Posting $post): static { if (!$this->postings->contains($post)) { diff --git a/src/Mapper/ContactEntityToApiMapper.php b/src/Mapper/ContactEntityToApiMapper.php index 988c53b..6ff000a 100644 --- a/src/Mapper/ContactEntityToApiMapper.php +++ b/src/Mapper/ContactEntityToApiMapper.php @@ -3,7 +3,9 @@ namespace App\Mapper; use App\ApiResource\ContactApi; +use App\ApiResource\PartnerApi; use App\Entity\Contact; +use App\Entity\Partner; use Symfonycasts\MicroMapper\AsMapper; use Symfonycasts\MicroMapper\MapperInterface; use Symfonycasts\MicroMapper\MicroMapperInterface; @@ -37,7 +39,11 @@ class ContactEntityToApiMapper implements MapperInterface $dto->firstName = $entity->getFirstName(); $dto->lastName = $entity->getLastName(); - $dto->partner = $entity->getPartner(); + $dto->partner = $this->microMapper->map( + $entity->getPartner(), PartnerApi::class, [ + MicroMapperInterface::MAX_DEPTH => 0, + ] + ); $dto->birthday = $entity->getBirthday(); $dto->image = $entity->getImage(); $dto->position = $entity->getPosition(); diff --git a/src/Mapper/UserApiToEntityMapper.php b/src/Mapper/UserApiToEntityMapper.php index e2254d5..4ce3b33 100644 --- a/src/Mapper/UserApiToEntityMapper.php +++ b/src/Mapper/UserApiToEntityMapper.php @@ -6,6 +6,7 @@ use App\ApiResource\UserApi; use App\Entity\User; use App\Entity\Posting; use App\Repository\UserRepository; +use Doctrine\Common\Collections\ArrayCollection; use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; use Symfony\Component\PropertyAccess\PropertyAccessorInterface; use Symfonycasts\MicroMapper\AsMapper; @@ -51,7 +52,7 @@ class UserApiToEntityMapper implements MapperInterface $entity->setPassword($this->userPasswordHasher->hashPassword($entity, $dto->password)); } - $userPostsEntities = []; + $userPostsEntities = new ArrayCollection(); foreach ($dto->postings as $userPostApi) { $userPostsEntities[] = $this->microMapper->map($userPostApi, Posting::class, [ MicroMapperInterface::MAX_DEPTH => 0, diff --git a/src/State/EntityClassDtoStateProcessor.php b/src/State/EntityClassDtoStateProcessor.php index b53846f..4705645 100644 --- a/src/State/EntityClassDtoStateProcessor.php +++ b/src/State/EntityClassDtoStateProcessor.php @@ -22,6 +22,13 @@ class EntityClassDtoStateProcessor implements ProcessorInterface } + /** + * @param mixed $data + * @param Operation $operation + * @param array $uriVariables + * @param array $context + * @return mixed|void|null + */ public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []) { $stateOptions = $operation->getStateOptions(); diff --git a/src/State/EntityToDtoStateProvider.php b/src/State/EntityToDtoStateProvider.php index de489de..044f763 100644 --- a/src/State/EntityToDtoStateProvider.php +++ b/src/State/EntityToDtoStateProvider.php @@ -12,7 +12,7 @@ use ApiPlatform\State\ProviderInterface; use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfonycasts\MicroMapper\MicroMapperInterface; -class EntityToDtoStateProvider implements ProviderInterface +class EntityToDtoStateProvider implements ProviderInterface { public function __construct( #[Autowire(service: CollectionProvider::class)] private ProviderInterface $collectionProvider, @@ -54,6 +54,8 @@ class EntityToDtoStateProvider implements ProviderInterface private function mapEntityToDto(object $entity, string $resourceClass): object { - return $this->microMapper->map($entity, $resourceClass); + $test = $this->microMapper->map($entity, $resourceClass); + return $test; + //return $this->microMapper->map($entity, $resourceClass); } } diff --git a/tests/Functional/UserResourceTest.php b/tests/Functional/UserResourceTest.php index d65e387..d6ef843 100644 --- a/tests/Functional/UserResourceTest.php +++ b/tests/Functional/UserResourceTest.php @@ -36,7 +36,7 @@ class UserResourceTest extends KernelTestCase $json->assertMissing('password'); $json->assertMissing('id'); }) - ->post('/login', [ + ->post('/auth', [ 'json' => [ 'email' => 'draggin_in_the_morning@coffee.com', 'password' => 'password',