Created
December 3, 2017 17:00
-
-
Save JanMikes/269df0cd3ed4dfc70c5a99b8a0ed30bf to your computer and use it in GitHub Desktop.
Creating DTO from request body (dumb way!)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare (strict_types=1); | |
namespace OdbavTo\App\Presenters; | |
use Nette\Application\IResponse; | |
use Nette\Application\Request; | |
use OdbavTo\App\HttpRequest\RequestBodyHelper; | |
use OdbavTo\App\Responses\CreatedResponse; | |
use OdbavTo\Application\CreateTicket\CreateTicketRequest; | |
use OdbavTo\Application\CreateTicket\CreateTicketUseCase; | |
use OdbavTo\Application\Ticket\TicketInputValueDTO; | |
final class CreateTicketPresenter extends UserAwarePresenter implements Presenter | |
{ | |
/** @var CreateTicketUseCase */ | |
private $useCase; | |
/** @var RequestBodyHelper */ | |
private $requestBodyHelper; | |
public function __construct(CreateTicketUseCase $useCase, RequestBodyHelper $requestBodyHelper) | |
{ | |
$this->useCase = $useCase; | |
$this->requestBodyHelper = $requestBodyHelper; | |
} | |
public function __invoke(Request $appRequest): IResponse | |
{ | |
$inputsValue = $this->requestBodyHelper->getValue('inputs', FALSE); | |
$inputs = []; | |
if (is_array($inputsValue)) { | |
foreach ($inputsValue as $inputValue) { | |
$inputId = $inputValue['inputId'] ?? $inputValue['id']; | |
$inputs[] = TicketInputValueDTO::fromNative($inputId, $inputValue['value']); | |
} | |
} | |
$response = $this->useCase->handle( | |
new CreateTicketRequest( | |
$this->userId, | |
$appRequest->getParameter('eventId'), | |
$this->requestBodyHelper->getValue('/variantId'), | |
$this->requestBodyHelper->getValue('/guest/email', FALSE), | |
$this->requestBodyHelper->getValue('/guest/firstName', FALSE), | |
$this->requestBodyHelper->getValue('/guest/lastName', FALSE), | |
$this->requestBodyHelper->getValue('/guest/phone', FALSE), | |
$this->requestBodyHelper->getValue('/guest/companyName', FALSE), | |
$this->requestBodyHelper->getValue('/guest/companyId', FALSE), | |
$this->requestBodyHelper->getValue('/guest/companyVatId', FALSE), | |
$this->requestBodyHelper->getValue('/guest/street', FALSE), | |
$this->requestBodyHelper->getValue('/guest/city', FALSE), | |
$this->requestBodyHelper->getValue('/guest/countryCode', FALSE), | |
$this->requestBodyHelper->getValue('/guest/postalCode', FALSE), | |
$inputs, | |
$this->requestBodyHelper->getValue('/anonymous') | |
) | |
); | |
return new CreatedResponse($response); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare (strict_types=1); | |
namespace OdbavTo\Application\CreateTicket; | |
use OdbavTo\Application\Ticket\TicketInputValueDTO; | |
use OdbavTo\Domain\Model\Company\Company; | |
use OdbavTo\Domain\Model\Company\CompanyId; | |
use OdbavTo\Domain\Model\Company\CompanyName; | |
use OdbavTo\Domain\Model\Company\CompanyVatId; | |
use OdbavTo\Domain\Model\Customer\User\UserId; | |
use OdbavTo\Domain\Model\Event\EventId; | |
use OdbavTo\Domain\Model\Geography\Address; | |
use OdbavTo\Domain\Model\Geography\City; | |
use OdbavTo\Domain\Model\Geography\Country; | |
use OdbavTo\Domain\Model\Geography\CountryCode; | |
use OdbavTo\Domain\Model\Geography\PostalCode; | |
use OdbavTo\Domain\Model\Geography\Street; | |
use OdbavTo\Domain\Model\Identity\Email; | |
use OdbavTo\Domain\Model\Identity\Phone; | |
use OdbavTo\Domain\Model\Person\FirstName; | |
use OdbavTo\Domain\Model\Person\LastName; | |
use OdbavTo\Domain\Model\Person\Name; | |
use OdbavTo\Domain\Model\Person\Person; | |
use OdbavTo\Domain\Model\Event\TicketVariant\TicketVariantId; | |
class CreateTicketRequest | |
{ | |
/** @var UserId */ | |
private $userId; | |
/** @var EventId */ | |
private $eventId; | |
/** @var TicketVariantId */ | |
private $variantId; | |
/** @var Person */ | |
private $guest; | |
/** @var TicketInputValueDTO[] */ | |
private $inputs; | |
/** @var bool */ | |
private $anonymous; | |
public function __construct( | |
UserId $userId, | |
string $eventId, | |
string $variantId, | |
?string $email, | |
?string $firstName, | |
?string $lastName, | |
?string $phone, | |
?string $companyName, | |
?string $companyId, | |
?string $companyVatId, | |
?string $street, | |
?string $city, | |
?string $countryCode, | |
?string $postalCode, | |
array $inputs, | |
bool $anonymous | |
) { | |
$this->userId = $userId; | |
$this->eventId = EventId::fromString($eventId); | |
$this->variantId = TicketVariantId::fromString($variantId); | |
$this->inputs = $inputs; | |
$this->guest = new Person( | |
new Email($email), | |
new Name( | |
new FirstName($firstName), | |
new LastName($lastName) | |
), | |
new Phone(null, $phone), | |
new Address( | |
new Street($street), | |
new City($city), | |
new PostalCode($postalCode), | |
new Country(CountryCode::get($countryCode)) | |
), | |
new Company( | |
new CompanyName($companyName), | |
new CompanyId($companyId), | |
new CompanyVatId($companyVatId) | |
) | |
); | |
$this->anonymous = $anonymous; | |
} | |
public function userId(): UserId | |
{ | |
return $this->userId; | |
} | |
public function eventId(): EventId | |
{ | |
return $this->eventId; | |
} | |
public function variantId(): TicketVariantId | |
{ | |
return $this->variantId; | |
} | |
public function guest(): Person | |
{ | |
return $this->guest; | |
} | |
/** @return TicketInputValueDTO[] */ | |
public function inputs(): array | |
{ | |
return $this->inputs; | |
} | |
public function anonymous(): bool | |
{ | |
return $this->anonymous; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment