-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from conferencetools/validate-basket
Refactored creation of basket out into a factory service
- Loading branch information
Showing
14 changed files
with
351 additions
and
47 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace ConferenceTools\Tickets\Domain\Service\Basket; | ||
|
||
use ConferenceTools\Tickets\Domain\ValueObject\Basket; | ||
|
||
interface BasketValidator | ||
{ | ||
public function validate(Basket $basket): void; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace ConferenceTools\Tickets\Domain\Service\Basket; | ||
|
||
use Carnage\Cqrs\Aggregate\Identity\GeneratorInterface; | ||
use ConferenceTools\Tickets\Domain\Service\Configuration; | ||
use ConferenceTools\Tickets\Domain\ValueObject\Basket; | ||
use ConferenceTools\Tickets\Domain\ValueObject\DiscountCode; | ||
use ConferenceTools\Tickets\Domain\ValueObject\TicketReservation; | ||
use ConferenceTools\Tickets\Domain\ValueObject\TicketReservationRequest; | ||
|
||
class Factory | ||
{ | ||
/** | ||
* @var GeneratorInterface | ||
*/ | ||
private $ticketIdGenerator; | ||
|
||
/** | ||
* @var Configuration | ||
*/ | ||
private $configuration; | ||
|
||
/** | ||
* @var BasketValidator | ||
*/ | ||
private $basketValidator; | ||
|
||
public function __construct( | ||
GeneratorInterface $ticketIdGenerator, | ||
Configuration $configuration, | ||
BasketValidator $basketValidator | ||
) { | ||
$this->ticketIdGenerator = $ticketIdGenerator; | ||
$this->configuration = $configuration; | ||
$this->basketValidator = $basketValidator; | ||
} | ||
|
||
public function basket(TicketReservationRequest ...$reservationRequests): Basket | ||
{ | ||
$tickets = $this->createTicketReservations(...$reservationRequests); | ||
|
||
return Basket::fromReservations( | ||
$this->configuration, | ||
$this->basketValidator, | ||
...$tickets | ||
); | ||
} | ||
|
||
public function basketWithDiscount( | ||
DiscountCode $discountCode, | ||
TicketReservationRequest ...$reservationRequests | ||
): Basket { | ||
$tickets = $this->createTicketReservations(...$reservationRequests); | ||
|
||
return Basket::fromReservationsWithDiscount( | ||
$this->configuration, | ||
$this->basketValidator, | ||
$discountCode, | ||
...$tickets | ||
); | ||
} | ||
|
||
/** | ||
* @return TicketReservation[] | ||
*/ | ||
private function createTicketReservations(TicketReservationRequest ...$reservationRequests): array | ||
{ | ||
$tickets = []; | ||
foreach ($reservationRequests as $reservationRequest) { | ||
for ($i = 0; $i < $reservationRequest->getQuantity(); $i++) { | ||
$tickets[] = new TicketReservation($reservationRequest->getTicketType(), | ||
$this->ticketIdGenerator->generateIdentity()); | ||
} | ||
} | ||
|
||
return $tickets; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace ConferenceTools\Tickets\Domain\Service\Basket; | ||
|
||
use ConferenceTools\Tickets\Domain\ValueObject\Basket; | ||
|
||
class ValidateBasket implements BasketValidator | ||
{ | ||
public function validate(Basket $basket): void | ||
{ | ||
if (count($basket->getTickets()) === 0) { | ||
throw new \DomainException('You must choose at least 1 ticket to purchase'); | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.