Skip to content

Instantly share code, notes, and snippets.

@tacoberu
Last active June 6, 2018 11:28
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 tacoberu/c69c3e5f476b0699937b to your computer and use it in GitHub Desktop.
Save tacoberu/c69c3e5f476b0699937b to your computer and use it in GitHub Desktop.
Jak používám anemický model.
<?php
class ArticleIdentification extends Article, Identification
{
}
class ArticleIdIdentification implements ArticleIdentification
{
function __construct($num)
{}
function getIdentKey()
{}
}
class ArticleSlugIdentification implements ArticleIdentification
{
function __construct($str)
{}
function getIdentKey()
{}
}
class ArticleNew implements Article
{
function __construct($name, $content, User $author)
{}
function getName()
{}
function getContent()
{}
function getAuthor()
{}
}
class ArticleEntry implements Article
{
function __construct(ArticleIdentification $id, $name, $content, User $author)
{}
function getId()
{}
function getName()
{}
function getContent()
{}
function getAuthor()
{}
}
class ArticleCreateService
{
function __construct(AclReader $acl, ArticleCreatePersistence $articleCreate, Majler $mail)
{}
function doPersist(ArticleNew $article)
{
$acl = $this->acl->read($article->getAuthor());
$acl->assert('create-article');
$entry = $this->articleCreate->doPersist($article);
$this->mailer->sendMail($entry);
return $entry;
}
}
class ArticleCreateDatebase implements ArticleCreatePersistence
{
function __construct(DBContext $db)
{}
function doPersist(ArticleNew $article)
{
$id = $this->db->insert([
'name' => $article->getName(),
'content' => $article->getContent(),
'id_author' => $article->getAuthor()->getId(),
]);
return new ArticleEntry(new ArticleIdentification($id), $name, $content, $article->getAuthor());
}
}
class ArticleRemoveDatebase implements ArticleRemovePersistence
{
function __construct(DBContext $db)
{}
function doRemove(Article $article)
{
switch (True) {
case $article instanceof ArticleEntry:
$id = $article->getId();
break;
case $article instanceof ArticleIdentification:
$id = $article;
break;
default:
throw new LogicException('...');
}
$this->fetch($id);
}
private function fetch(ArticleIdentification $id)
{
switch (True) {
case $id instanceof ArticleSlugIdentification:
$this->db->delete()->where('slug', $id->getIdentKey());
break;
case $id instanceof ArticleIdIdentification:
$this->db->delete()->where('id', $id->getIdentKey());
break;
default:
throw new LogicException('...');
}
}
}
@kitsaels
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment