Skip to content

Instantly share code, notes, and snippets.

@asprega
Created October 19, 2021 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asprega/f23444902509be6bc3b685f608051953 to your computer and use it in GitHub Desktop.
Save asprega/f23444902509be6bc3b685f608051953 to your computer and use it in GitHub Desktop.
[ClockMock article on Medium] Codebase
<?php
declare(strict_types=1);
namespace App\Entity;
use DateTimeImmutable;
class Entity
{
private DateTimeImmutable $creationDate;
private ?DateTimeImmutable $processingDate = null;
public function __construct()
{
$this->creationDate = new DateTimeImmutable();
}
public function getCreationDate(): DateTimeImmutable
{
return $this->creationDate;
}
public function getProcessingDate(): ?DateTimeImmutable
{
return $this->processingDate;
}
public function scheduleNextProcessing(DateTimeImmutable $processingDate): void
{
$this->processingDate = $processingDate;
}
}
<?php
declare(strict_types=1);
namespace App\Service;
use DateTimeImmutable;
use Exception;
use App\Entity\Entity;
class Service
{
public function doSomething(): void
{
$now = new DateTimeImmutable();
if ((int) $now->format('d') > 28) {
throw new Exception('This thing cannot be done after the 28th of every month.');
}
// ... Something is done here
}
public function scheduleProcessing(Entity $entity): void
{
$entity->scheduleNextProcessing(new DateTimeImmutable('+5 minutes'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment