Skip to content

Instantly share code, notes, and snippets.

@SelrahcD
Created October 22, 2024 12:52
Show Gist options
  • Save SelrahcD/8247a7ded5d426724d2f8fbc0c77899b to your computer and use it in GitHub Desktop.
Save SelrahcD/8247a7ded5d426724d2f8fbc0c77899b to your computer and use it in GitHub Desktop.
approval
<?php
declare(strict_types=1);
use ApprovalTests\Approvals;
use ApprovalTests\CombinationApprovals;
use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\Rfc4122\UuidV4;
use Ramsey\Uuid\UuidInterface;
final class Logger {
/**
* @var array<array{message: string, params: array}>
*/
public static $logs = [];
public static function log(string $message, array $params): void
{
static::$logs[] = [
'message' => $message,
'params' => $params,
];
}
public static function resetLogs(): void
{
static::$logs = [];
}
}
final class Event {
public function __construct(public readonly string $eventName,
public readonly array $payload,
)
{
}
}
final class EventBus {
public function dispatch(Event $event): void
{
Logger::log('Event dispatched', [
'event-name' => $event->eventName,
'payload' => $event->payload,
]);
// Dispatch the event in the queuing system
// ...
}
}
final readonly class ResultatJeu {
public function __construct(
public UuidInterface $uuid,
public DateTimeImmutable $date,
public bool $gagnant,
public ?string $lot = null,
)
{}
}
final class JeuHotel {
private EventBus $eventBus;
public function __construct()
{
$this->eventBus = new EventBus();
}
public function participerAuTirage(int $age): ResultatJeu
{
$uuid = UuidV4::uuid4();
$gagnant = rand(0, 1);
$maintenant = new DateTimeImmutable();
if($gagnant) {
if($maintenant > ($maintenant->setTime(22, 00))) {
$this->eventBus->dispatch(new Event('JeuPerdu', ['uuid' => $uuid->toString()]));
return new ResultatJeu($uuid, $maintenant, false);
}
if($maintenant < ($maintenant->setTime(8, 00))) {
$this->eventBus->dispatch(new Event('JeuPerdu', ['uuid' => $uuid->toString()]));
return new ResultatJeu($uuid, $maintenant, false);
}
if ($age >= 18) {
if ($maintenant > ($maintenant->setTime(18, 00))) {
$this->eventBus->dispatch(new Event('JeuGagné', ['uuid' => $uuid->toString()]));
return new ResultatJeu($uuid, $maintenant, true, "Cocktail au bar ou au bar de la piscine");
}
}
else {
if ($maintenant > ($maintenant->setTime(18, 00))) {
$this->eventBus->dispatch(new Event('JeuGagné', ['uuid' => $uuid->toString()]));
return new ResultatJeu($uuid, $maintenant, true, "Soft au bar ou au bar de la piscine");
}
}
if($maintenant > ($maintenant->setTime(8, 00))) {
$this->eventBus->dispatch(new Event('JeuGagné', ['uuid' => $uuid->toString()]));
return new ResultatJeu($uuid, $maintenant, true, "10% de réduction à la boutique de l'hôtel");
}
}
$this->eventBus->dispatch(new Event('JeuPerdu', ['uuid' => $uuid->toString()]));
return new ResultatJeu($uuid, $maintenant, false, null);
}
}
class JeuHotelTest extends TestCase
{
protected function tearDown(): void
{
Logger::resetLogs();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment