PostFactory::findOrCreateFromWebsocketDto() draft. UserFactory::DATE_FORMAT moved to AbstractFactory.

This commit is contained in:
Alexey Skobkin 2019-02-24 02:07:17 +03:00
parent d528b45436
commit 300dbcb466
4 changed files with 43 additions and 8 deletions

View file

@ -6,6 +6,8 @@ use Psr\Log\LoggerInterface;
abstract class AbstractFactory abstract class AbstractFactory
{ {
public const DATE_FORMAT = 'Y-m-d_H:i:s';
/** @var LoggerInterface */ /** @var LoggerInterface */
protected $logger; protected $logger;

View file

@ -4,10 +4,11 @@ namespace Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Skobkin\Bundle\PointToolsBundle\DTO\Api\{MetaPost, Post as PostDTO, PostsPage}; use Skobkin\Bundle\PointToolsBundle\DTO\Api\{MetaPost, Post as ApiPost, PostsPage};
use Skobkin\Bundle\PointToolsBundle\DTO\Api\WebSocket\Message as WebsocketMessage;
use Skobkin\Bundle\PointToolsBundle\Entity\{Blogs\Post, Blogs\PostTag, User}; use Skobkin\Bundle\PointToolsBundle\Entity\{Blogs\Post, Blogs\PostTag, User};
use Skobkin\Bundle\PointToolsBundle\Exception\{Api\InvalidResponseException, Factory\Blog\InvalidDataException}; use Skobkin\Bundle\PointToolsBundle\Exception\{Api\InvalidResponseException, Factory\Blog\InvalidDataException};
use Skobkin\Bundle\PointToolsBundle\Repository\Blogs\PostRepository; use Skobkin\Bundle\PointToolsBundle\Repository\{Blogs\PostRepository, UserRepository};
use Skobkin\Bundle\PointToolsBundle\Service\Factory\{AbstractFactory, UserFactory}; use Skobkin\Bundle\PointToolsBundle\Service\Factory\{AbstractFactory, UserFactory};
class PostFactory extends AbstractFactory class PostFactory extends AbstractFactory
@ -18,6 +19,9 @@ class PostFactory extends AbstractFactory
/** @var PostRepository */ /** @var PostRepository */
private $postRepository; private $postRepository;
/** @var UserRepository */
private $userRepository;
/** @var UserFactory */ /** @var UserFactory */
private $userFactory; private $userFactory;
@ -35,6 +39,7 @@ class PostFactory extends AbstractFactory
LoggerInterface $logger, LoggerInterface $logger,
EntityManagerInterface $em, EntityManagerInterface $em,
PostRepository $postRepository, PostRepository $postRepository,
UserRepository $userRepository,
UserFactory $userFactory, UserFactory $userFactory,
FileFactory $fileFactory, FileFactory $fileFactory,
CommentFactory $commentFactory, CommentFactory $commentFactory,
@ -43,6 +48,7 @@ class PostFactory extends AbstractFactory
parent::__construct($logger); parent::__construct($logger);
$this->em = $em; $this->em = $em;
$this->postRepository = $postRepository; $this->postRepository = $postRepository;
$this->userRepository = $userRepository;
$this->userFactory = $userFactory; $this->userFactory = $userFactory;
$this->fileFactory = $fileFactory; $this->fileFactory = $fileFactory;
$this->commentFactory = $commentFactory; $this->commentFactory = $commentFactory;
@ -111,7 +117,7 @@ class PostFactory extends AbstractFactory
throw $e; throw $e;
} }
$post = $this->findOrCreateFromDto($postData, $author); $post = $this->findOrCreateFromApiDto($postData, $author);
try { try {
$this->updatePostTags($post, $postData->getTags() ?: []); $this->updatePostTags($post, $postData->getTags() ?: []);
@ -130,7 +136,37 @@ class PostFactory extends AbstractFactory
return $post; return $post;
} }
private function findOrCreateFromDto(PostDTO $postData, User $author): Post public function findOrCreateFromWebsocketDto(WebsocketMessage $message): Post
{
if (!$message->isValid()) {
throw new InvalidDataException('Invalid post data');
}
if (!$message->isPost()) {
throw new \LogicException(sprintf(
'Incorrect message type received. \'post\' expected \'%s\' given',
$message->getA()
));
}
if (null === $post = $this->postRepository->find($message->getPostId())) {
/** @var User $author */
if (null === $author = $this->userRepository->find($message->getAuthorId())) {
// @todo create user
}
$post = new Post(
$message->getPostId(),
$author,
new \DateTime(),
Post::TYPE_POST
);
$this->postRepository->add($post);
}
$post->setText($message->getText());
}
private function findOrCreateFromApiDto(ApiPost $postData, User $author): Post
{ {
if (null === ($post = $this->postRepository->find($postData->getId()))) { if (null === ($post = $this->postRepository->find($postData->getId()))) {
// Creating new post // Creating new post

View file

@ -10,8 +10,6 @@ use Skobkin\Bundle\PointToolsBundle\Exception\Factory\InvalidUserDataException;
class UserFactory extends AbstractFactory class UserFactory extends AbstractFactory
{ {
public const DATE_FORMAT = 'Y-m-d_H:i:s';
/** @var UserRepository */ /** @var UserRepository */
private $userRepository; private $userRepository;

View file

@ -3,8 +3,7 @@
namespace Skobkin\Bundle\PointToolsBundle\Service\WebSocket; namespace Skobkin\Bundle\PointToolsBundle\Service\WebSocket;
use Skobkin\Bundle\PointToolsBundle\DTO\Api\WebSocket\Message; use Skobkin\Bundle\PointToolsBundle\DTO\Api\WebSocket\Message;
use Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\CommentFactory; use Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\{CommentFactory, PostFactory};
use Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\PostFactory;
class WebSocketMessageProcessor class WebSocketMessageProcessor
{ {