Skip to content

Commit

Permalink
code challenge 12 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
jschaedl committed Dec 2, 2023
1 parent 84b9e35 commit a484a85
Show file tree
Hide file tree
Showing 31 changed files with 184 additions and 34 deletions.
10 changes: 5 additions & 5 deletions fixtures/attendees.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
App\Entity\Attendee:
attendee_1:
__construct: [ '02d47053-4034-4818-97d1-299c4cd7168d', 'Ferdinand', 'Hale', '[email protected]' ]
__construct: [ '02d47053-4034-4818-97d1-299c4cd7168d', 'Ferdinand', 'Hale', 'Ferdinand Hale', '[email protected]' ]
attendee_2:
__construct: [ '6047c255-593f-4fa8-888b-f919fafd904f', 'Abraham', 'Noel', '[email protected]' ]
__construct: [ '6047c255-593f-4fa8-888b-f919fafd904f', 'Abraham', 'Noel', 'Abraham Noel', '[email protected]' ]
attendee_3:
__construct: [ '92e06a4b-19ac-4e67-b988-00e7250929c6', 'Bertha', 'Tucker', '[email protected]' ]
__construct: [ '92e06a4b-19ac-4e67-b988-00e7250929c6', 'Bertha', 'Tucker', 'Bertha Tucker', '[email protected]' ]
attendee_4:
__construct: [ 'c153c6c3-c43b-449d-8f5a-2bfb627f9822', 'Melodie', 'Perkins', '[email protected]' ]
__construct: [ 'c153c6c3-c43b-449d-8f5a-2bfb627f9822', 'Melodie', 'Perkins', 'Melodie Perkins', '[email protected]' ]
attendee_5:
__construct: [ 'f5e2c329-f5b8-4e4b-860d-b5a0189ef35f', 'Keegan', 'Mcpherson', '[email protected]' ]
__construct: [ 'f5e2c329-f5b8-4e4b-860d-b5a0189ef35f', 'Keegan', 'Mcpherson', 'Keegan Mcpherson', '[email protected]' ]
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<!-- DATABASE_URL="mysql://app:[email protected]:3306/app?serverVersion=10.11.2-MariaDB&charset=utf8mb4" -->
<env name="DATABASE_URL" value="sqlite:///%kernel.project_dir%/var/data.db"/>
<!-- ###- doctrine/doctrine-bundle ### -->
<env name="SYMFONY_DEPRECATIONS_HELPER" value="disabled"/>
</php>
<testsuites>
<testsuite name="Project Test Suite">
Expand Down
10 changes: 8 additions & 2 deletions src/Controller/Attendee/UpdateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@

#[IsGranted('ROLE_USER')]
#[Route('/attendees/{identifier}', name: 'update_attendee', methods: ['PUT'])]
#[OA\Put(tags: ['Attendee'])]
#[OA\Put(
description: 'This endpoint is deprecated, please don\'t rely on it anymore.',
tags: ['Attendee'],
deprecated: true
)]
final class UpdateController
{
public function __construct(
Expand All @@ -27,6 +31,8 @@ public function __invoke(Request $request, Attendee $attendee, UpdateAttendeeMod
{
$this->attendeeUpdater->update($attendee, $updateAttendeeModel);

return new Response(null, Response::HTTP_NO_CONTENT);
return new Response(null, Response::HTTP_NO_CONTENT, [
'Sunset' => (new \DateTime())->modify('+ 1 year')->format(DATE_RFC7231), // HTTP Date by RFC 7231
]);
}
}
5 changes: 3 additions & 2 deletions src/Domain/AttendeeCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ public function create(CreateAttendeeModel $createAttendeeModel): Attendee
{
$newAttendee = new Attendee(
Uuid::uuid4()->toString(),
$createAttendeeModel->firstname,
$createAttendeeModel->lastname,
$createAttendeeModel->firstname ?? '',
$createAttendeeModel->lastname ?? '',
$createAttendeeModel->name ?? '',
$createAttendeeModel->email,
);

Expand Down
31 changes: 29 additions & 2 deletions src/Domain/Model/CreateAttendeeModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,45 @@

namespace App\Domain\Model;

use OpenApi\Attributes as OA;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

final class CreateAttendeeModel
{
#[NotBlank]
// be aware that deprecating properties means you still need to support both ways of creating attendees
// and that you need to move validation into your domain layer

#[OA\Property(
deprecated: true,
description: 'Property firstname is deprecated, use property name instead.'
)]
#[NotBlank(allowNull: true)]
public string $firstname;

#[NotBlank]
#[OA\Property(
deprecated: true,
description: 'Property lastname is deprecated, use property name instead.'
)]
#[NotBlank(allowNull: true)]
public string $lastname;

#[NotBlank(allowNull: true)]
public string $name;

#[NotBlank]
#[Email]
public string $email;

#[Callback]
public function validate(ExecutionContextInterface $context, $payload)
{
if ((empty($this->firstname) || empty($this->lastname)) && empty($this->name)) {
$context->buildViolation('Passing values for fields "$firstname" and "$lastname" is deprecated. Pass a value for field "$name" instead.')
->atPath('firstname')
->addViolation();
}
}
}
26 changes: 25 additions & 1 deletion src/Entity/Attendee.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,35 @@ class Attendee
#[ORM\Column(type: 'string', length: 255)]
private string $lastname;

#[ORM\Column(type: 'string', length: 255)]
private string $name;

#[ORM\Column(type: 'string', length: 255)]
private string $email;

#[ORM\ManyToMany(targetEntity: Workshop::class, inversedBy: 'attendees')]
private Collection $workshops;

public function __construct(string $identifier, string $firstname, string $lastname, string $email)
public function __construct(string $identifier, string $firstname, string $lastname, string $name, string $email)
{
if ((!empty($firstname) || !empty($lastname)) && empty($name)) {
@trigger_error('Passing values for argument "$firstname" or "$lastname" is deprecated. Pass a value for argument "$name" instead.', E_USER_DEPRECATED);
}

Assert::uuid($identifier, 'Argument $identifier is not a valid UUID: %s');

if ((empty($firstname) || empty($lastname)) && empty($name)) {
throw new \InvalidArgumentException('Passing values for argument "$firstname" and "$lastname" is deprecated. Pass a value for argument "$name" instead.');
}

Assert::email($email);

$this->identifier = Uuid::fromString($identifier);
$this->firstname = $firstname;
$this->lastname = $lastname;

$this->name = empty($name) ? $firstname.' '.$lastname : $name;

$this->email = $email;

$this->workshops = new ArrayCollection();
Expand All @@ -62,6 +77,8 @@ public function getIdentifier(): string

public function getFirstname(): string
{
@trigger_error('Calling Attendee::getFirstname() is deprecated. Use Attendee::getName()', E_USER_DEPRECATED);

return $this->firstname;
}

Expand All @@ -74,6 +91,8 @@ public function changeFirstname(string $firstname): self

public function getLastname(): string
{
@trigger_error('Calling Attendee::getLastname() is deprecated. Use Attendee::getName()', E_USER_DEPRECATED);

return $this->lastname;
}

Expand All @@ -84,6 +103,11 @@ public function changeLastname(string $lastname): self
return $this;
}

public function getName(): string
{
return $this->name;
}

public function getEmail(): string
{
return $this->email;
Expand Down
9 changes: 5 additions & 4 deletions tests/Controller/Attendee/CreateControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function test_it_should_create_an_attendee(): void
$expectedAttendee = $attendeesAfter[0];
static::assertSame('Paul', $expectedAttendee->getFirstname());
static::assertSame('Paulsen', $expectedAttendee->getLastname());
static::assertSame('Paul Paulsen', $expectedAttendee->getName());
static::assertSame('[email protected]', $expectedAttendee->getEmail());
}

Expand All @@ -57,9 +58,9 @@ public static function provideUnprocessableAttendeeData(): \Generator
yield 'empty data' => ['{}'];
yield 'wrong json one' => ['{'];
yield 'wrong json two' => ['}'];
yield 'missing firstname' => ['{"lastname": "Paulsen", "email": "[email protected]"}'];
yield 'missing lastname' => ['{"firstname": "Paul", "email": "[email protected]"}'];
yield 'missing email' => ['{"firstname": "Paul", "lastname": "Paulsen"}'];
yield 'wrong email' => ['{"firstname": "Paul", "lastname": "Paulsen", "email": "paulpaulsende"}'];
yield 'missing firstname and missing name' => ['{"lastname": "Paulsen", "email": "[email protected]"}'];
yield 'missing lastname and missing name' => ['{"firstname": "Paul", "email": "[email protected]"}'];
yield 'missing email' => ['{"name": "Paul Paulsen"}'];
yield 'wrong email' => ['{"name": "Paul Paulsen", "email": "paulpaulsende"}'];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
"errors": [
{
"message": "Validation failed.",
"detail": "firstname: This value should not be blank."
},
{
"message": "Validation failed.",
"detail": "lastname: This value should not be blank."
"detail": "firstname: Passing values for fields \"$firstname\" and \"$lastname\" is deprecated. Pass a value for field \"$name\" instead."
},
{
"message": "Validation failed.",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"errors": [
{
"message": "Validation failed.",
"detail": "firstname: Passing values for fields \"$firstname\" and \"$lastname\" is deprecated. Pass a value for field \"$name\" instead."
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"errors": [
{
"message": "Validation failed.",
"detail": "firstname: Passing values for fields \"$firstname\" and \"$lastname\" is deprecated. Pass a value for field \"$name\" instead."
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"identifier": "803449f4-9a4c-4ecb-8ce4-cebc804fe70a",
"firstname": "Jan",
"lastname": "Sch\u00e4dlich",
"name": "Jan Sch\u00e4dlich",
"email": "[email protected]",
"workshops": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"identifier": "803449f4-9a4c-4ecb-8ce4-cebc804fe70a",
"firstname": "Jan",
"lastname": "Sch\u00e4dlich",
"name": "Jan Sch\u00e4dlich",
"email": "[email protected]",
"workshops": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
"identifier": "4878f198-36ab-4fe3-8189-19662a9764fa",
"firstname": "a",
"lastname": "1",
"name": "a1",
"email": "[email protected]",
"workshops": []
},
{
"identifier": "e942ce16-27c2-494f-9d93-03412da980c5",
"firstname": "b",
"lastname": "2",
"name": "b2",
"email": "[email protected]",
"workshops": []
},
{
"identifier": "4714fb8a-83d8-49af-abbf-7c68fc6c9656",
"firstname": "c",
"lastname": "3",
"name": "c3",
"email": "[email protected]",
"workshops": []
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,39 @@
"identifier": "4878f198-36ab-4fe3-8189-19662a9764fa",
"firstname": "a",
"lastname": "1",
"name": "a1",
"email": "[email protected]",
"workshops": []
},
{
"identifier": "e942ce16-27c2-494f-9d93-03412da980c5",
"firstname": "b",
"lastname": "2",
"name": "b2",
"email": "[email protected]",
"workshops": []
},
{
"identifier": "4714fb8a-83d8-49af-abbf-7c68fc6c9656",
"firstname": "c",
"lastname": "3",
"name": "c3",
"email": "[email protected]",
"workshops": []
},
{
"identifier": "65445e8c-a6c6-4955-9eb2-5fb60d6a991e",
"firstname": "d",
"lastname": "4",
"name": "d4",
"email": "[email protected]",
"workshops": []
},
{
"identifier": "3aacd688-5b81-4aba-a5ea-ac7668ba95b6",
"firstname": "e",
"lastname": "5",
"name": "e5",
"email": "[email protected]",
"workshops": []
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
"identifier": "65445e8c-a6c6-4955-9eb2-5fb60d6a991e",
"firstname": "d",
"lastname": "4",
"name": "d4",
"email": "[email protected]",
"workshops": []
},
{
"identifier": "3aacd688-5b81-4aba-a5ea-ac7668ba95b6",
"firstname": "e",
"lastname": "5",
"name": "e5",
"email": "[email protected]",
"workshops": []
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"identifier": "17058af8-1b0f-4afe-910d-669b4bd0fd26",
"firstname": "Jan",
"lastname": "Sch\u00e4dlich",
"name": "Jan Sch\u00e4dlich",
"email": "[email protected]",
"workshops": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"identifier": "17058af8-1b0f-4afe-910d-669b4bd0fd26",
"firstname": "Jan",
"lastname": "Sch\u00e4dlich",
"name": "Jan Sch\u00e4dlich",
"email": "[email protected]",
"workshops": [
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Controller/Attendee/fixtures/delete_attendee.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
App\Entity\Attendee:
attendee_1:
__construct: [ 'bb5cb8a8-0df8-404f-a3f3-54ee5c9cf855', 'Jan', 'Schädlich', '[email protected]' ]
__construct: [ 'bb5cb8a8-0df8-404f-a3f3-54ee5c9cf855', 'Jan', 'Schädlich', 'Jan Schädlich', '[email protected]' ]
2 changes: 1 addition & 1 deletion tests/Controller/Attendee/fixtures/list_attendee.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ App\Entity\Workshop:

App\Entity\Attendee:
attendee_1:
__construct: [ '803449f4-9a4c-4ecb-8ce4-cebc804fe70a', 'Jan', 'Schädlich', '[email protected]' ]
__construct: [ '803449f4-9a4c-4ecb-8ce4-cebc804fe70a', 'Jan', 'Schädlich', 'Jan Schädlich', '[email protected]' ]
10 changes: 5 additions & 5 deletions tests/Controller/Attendee/fixtures/paginate_attendee.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
App\Entity\Attendee:
attendee_1:
__construct: [ '4878f198-36ab-4fe3-8189-19662a9764fa', 'a', '1', '[email protected]' ]
__construct: [ '4878f198-36ab-4fe3-8189-19662a9764fa', 'a', '1', 'a1', 'a1@test.de' ]
attendee_2:
__construct: [ 'e942ce16-27c2-494f-9d93-03412da980c5', 'b', '2', '[email protected]' ]
__construct: [ 'e942ce16-27c2-494f-9d93-03412da980c5', 'b', '2', 'b2', 'b2@test.de' ]
attendee_3:
__construct: [ '4714fb8a-83d8-49af-abbf-7c68fc6c9656', 'c', '3', '[email protected]' ]
__construct: [ '4714fb8a-83d8-49af-abbf-7c68fc6c9656', 'c', '3', 'c3', 'c3@test.de' ]
attendee_4:
__construct: [ '65445e8c-a6c6-4955-9eb2-5fb60d6a991e', 'd', '4', '[email protected]' ]
__construct: [ '65445e8c-a6c6-4955-9eb2-5fb60d6a991e', 'd', '4', 'd4', 'd4@test.de' ]
attendee_5:
__construct: [ '3aacd688-5b81-4aba-a5ea-ac7668ba95b6', 'e', '5', '[email protected]' ]
__construct: [ '3aacd688-5b81-4aba-a5ea-ac7668ba95b6', 'e', '5', 'e5', 'e5@test.de' ]
2 changes: 1 addition & 1 deletion tests/Controller/Attendee/fixtures/read_attendee.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ App\Entity\Workshop:

App\Entity\Attendee:
attendee_1:
__construct: [ '17058af8-1b0f-4afe-910d-669b4bd0fd26', 'Jan', 'Schädlich', '[email protected]' ]
__construct: [ '17058af8-1b0f-4afe-910d-669b4bd0fd26', 'Jan', 'Schädlich', 'Jan Schädlich', '[email protected]' ]
2 changes: 1 addition & 1 deletion tests/Controller/Attendee/fixtures/update_attendee.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
App\Entity\Attendee:
attendee_1:
__construct: [ 'b38aa3e4-f9de-4dca-aaeb-3ec36a9feb6c', 'Jan', 'Schädlich', '[email protected]' ]
__construct: [ 'b38aa3e4-f9de-4dca-aaeb-3ec36a9feb6c', 'Jan', 'Schädlich', 'Jan Schädlich', '[email protected]' ]
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"identifier": "2a451d60-2fef-437b-838e-10edb2ade8eb",
"firstname": "Jan",
"lastname": "Sch\u00e4dlich",
"name": "Jan Sch\u00e4dlich",
"email": "[email protected]",
"workshops": [
"RESTful Webservices in Symfony"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"identifier": "2a451d60-2fef-437b-838e-10edb2ade8eb",
"firstname": "Jan",
"lastname": "Sch\u00e4dlich",
"name": "Jan Sch\u00e4dlich",
"email": "[email protected]",
"workshops": [
"RESTful Webservices in Symfony"
Expand Down
Loading

0 comments on commit a484a85

Please sign in to comment.