Skip to content

Instantly share code, notes, and snippets.

@ismail1432
Last active December 19, 2021 10:50
Show Gist options
  • Save ismail1432/8162ca1601d87da7a76d6c3333d3ef95 to your computer and use it in GitHub Desktop.
Save ismail1432/8162ca1601d87da7a76d6c3333d3ef95 to your computer and use it in GitHub Desktop.
<?php
final class ArticleStatus
{
public DRAFT = 'draft';
public PUBLISHED = 'published';
public DELETED = 'deleted';
private string $value;
public function ALL = [
self::DRAFT,
self::PUBLISHED,
self::DELETED,
];
public static function create(string $status): self
{
if(!in_array($status, self::ALL)) {
throw new LogicException("Unsupported value blabla...");
}
$articleStatus = new self;
$articleStatus->value = $status;
return $articleStatus;
}
public function getValue(): string
{
return $this->getValue();
}
}
final class Article
{
private ArticleStatus $status;
public function markAsPublished(): void
{
if ($this->status->getValue() === ArticleStatus::DELETED)) {
throw new LogicException(sprintf("Cannot mark as published as it is deleted"));
}
$this->status = ArticleStatus::create(ArticleStatus::PUBLISHED);
}
}
class PublishArticleHandler
{
public function __invoke(Article $article)
{
$article->markAsPublished();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment