WS 'comment' processing. WIP.

This commit is contained in:
Alexey Skobkin 2019-02-28 03:43:40 +03:00
parent b0a6fbfb7f
commit 6b5a60b2a5
13 changed files with 98 additions and 78 deletions

View file

@ -95,6 +95,8 @@ class ProcessWebsocketUpdatesCommand extends Command
if (!$keepJobs) { if (!$keepJobs) {
$this->bsClient->delete($job); $this->bsClient->delete($job);
} }
// todo cleaning IdentityMap
} }
} catch (UnsupportedTypeException $e) { } catch (UnsupportedTypeException $e) {
$output->writeln(' Unsupported message type: '.$message->getA()); $output->writeln(' Unsupported message type: '.$message->getA());

View file

@ -4,39 +4,22 @@ namespace Skobkin\Bundle\PointToolsBundle\DTO\Api;
class Comment implements ValidableInterface class Comment implements ValidableInterface
{ {
/** /** @var int|null */
* @var string|null
*/
private $postId;
/**
* @var int|null
*/
private $number; private $number;
/** /** @var int|null */
* @var int|null
*/
private $toCommentId; private $toCommentId;
/** /** @var string|null */
* @var string|null
*/
private $created; private $created;
/** /** @var string|null */
* @var string|null
*/
private $text; private $text;
/** /** @var User|null */
* @var User|null
*/
private $author; private $author;
/** /** @var bool|null */
* @var bool|null
*/
private $isRec; private $isRec;

View file

@ -4,27 +4,17 @@ namespace Skobkin\Bundle\PointToolsBundle\DTO\Api;
class MetaPost implements ValidableInterface class MetaPost implements ValidableInterface
{ {
/** /** @var Post|null */
* @var Post|null
*/
private $post; private $post;
/** /** @var Comment[]|null */
* @var Comment[]|null
*/
private $comments; private $comments;
public function getPost(): ?Post public function getPost(): ?Post
{ {
return $this->post; return $this->post;
} }
public function setPost(?Post $post): void
{
$this->post = $post;
}
/** /**
* @return Comment[]|null * @return Comment[]|null
*/ */
@ -33,20 +23,8 @@ class MetaPost implements ValidableInterface
return $this->comments; return $this->comments;
} }
/**
* @param Comment[]|null $comments
*/
public function setComments(?array $comments): void
{
$this->comments = $comments;
}
public function isValid(): bool public function isValid(): bool
{ {
if (null !== $this->post && $this->post->isValid()) { return (null !== $this->post && $this->post->isValid());
return true;
}
return false;
} }
} }

View file

@ -0,0 +1,20 @@
<?php
namespace Skobkin\Bundle\PointToolsBundle\Exception\Api;
class PostNotFoundException extends NotFoundException
{
/** @var string */
private $id;
public function __construct(string $id, \Exception $previous)
{
parent::__construct('Post not found', 0, $previous);
$this->id = $id;
}
public function getId(): string
{
return $this->id;
}
}

View file

@ -4,16 +4,11 @@ namespace Skobkin\Bundle\PointToolsBundle\Exception\Api;
class UserNotFoundException extends NotFoundException class UserNotFoundException extends NotFoundException
{ {
/** /** @var int */
* @var int private $userId;
*/
protected $userId;
/**
* @var string
*/
protected $login;
/** @var string */
private $login;
/** /**
* {@inheritdoc} * {@inheritdoc}

View file

@ -2,10 +2,6 @@ Skobkin\Bundle\PointToolsBundle\DTO\Api\Comment:
exclusion_policy: none exclusion_policy: none
access_type: public_method access_type: public_method
properties: properties:
postId:
serialized_name: 'post_id'
type: 'Skobkin\Bundle\PointToolsBundle\DTO\Api\Post'
max_depth: 2
number: number:
serialized_name: 'id' serialized_name: 'id'
type: integer type: integer

View file

@ -1,10 +1,9 @@
Skobkin\Bundle\PointToolsBundle\DTO\Api\MetaPost: Skobkin\Bundle\PointToolsBundle\DTO\Api\MetaPost:
exclusion_policy: none exclusion_policy: none
access_type: public_method
properties: properties:
post: post:
serialized_name: 'post'
type: 'Skobkin\Bundle\PointToolsBundle\DTO\Api\Post' type: 'Skobkin\Bundle\PointToolsBundle\DTO\Api\Post'
max_depth: 2
comments: comments:
serialized_name: 'comments'
type: 'array<Skobkin\Bundle\PointToolsBundle\DTO\Api\Comment>' type: 'array<Skobkin\Bundle\PointToolsBundle\DTO\Api\Comment>'
max_depth: 2

View file

@ -5,6 +5,9 @@ namespace Skobkin\Bundle\PointToolsBundle\Service\Api;
use GuzzleHttp\ClientInterface; use GuzzleHttp\ClientInterface;
use JMS\Serializer\SerializerInterface; use JMS\Serializer\SerializerInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Skobkin\Bundle\PointToolsBundle\DTO\Api\MetaPost;
use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post;
use Skobkin\Bundle\PointToolsBundle\Exception\Api\{NotFoundException, PostNotFoundException};
use Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\PostFactory; use Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\PostFactory;
/** /**
@ -12,6 +15,8 @@ use Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\PostFactory;
*/ */
class PostApi extends AbstractApi class PostApi extends AbstractApi
{ {
private const PREFIX = '/api/post/';
/** /**
* @var PostFactory * @var PostFactory
*/ */
@ -24,4 +29,24 @@ class PostApi extends AbstractApi
$this->postFactory = $postFactory; $this->postFactory = $postFactory;
} }
/**
* @throws PostNotFoundException
*/
public function getById(string $id): Post
{
try {
$postData = $this->getGetJsonData(
self::PREFIX.$id,
[],
MetaPost::class
);
} catch (NotFoundException $e) {
throw new PostNotFoundException($id, $e);
}
// Not catching ForbiddenException right now
return $this->postFactory->findOrCreateFromDtoWithContent($postData);
}
} }

View file

@ -3,9 +3,7 @@
namespace Skobkin\Bundle\PointToolsBundle\Service\Api; namespace Skobkin\Bundle\PointToolsBundle\Service\Api;
use GuzzleHttp\ClientInterface; use GuzzleHttp\ClientInterface;
use JMS\Serializer\{ use JMS\Serializer\{DeserializationContext, SerializerInterface};
DeserializationContext, SerializerInterface
};
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Skobkin\Bundle\PointToolsBundle\DTO\Api\{Auth, User as UserDTO}; use Skobkin\Bundle\PointToolsBundle\DTO\Api\{Auth, User as UserDTO};
use Skobkin\Bundle\PointToolsBundle\Entity\User; use Skobkin\Bundle\PointToolsBundle\Entity\User;

View file

@ -6,6 +6,7 @@ use Psr\Log\LoggerInterface;
use Skobkin\Bundle\PointToolsBundle\DTO\Api\WebSocket\Message; use Skobkin\Bundle\PointToolsBundle\DTO\Api\WebSocket\Message;
use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Comment; use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Comment;
use Skobkin\Bundle\PointToolsBundle\Repository\Blogs\{CommentRepository, PostRepository}; use Skobkin\Bundle\PointToolsBundle\Repository\Blogs\{CommentRepository, PostRepository};
use Skobkin\Bundle\PointToolsBundle\Service\Api\PostApi;
use Skobkin\Bundle\PointToolsBundle\Service\Factory\{AbstractFactory, UserFactory}; use Skobkin\Bundle\PointToolsBundle\Service\Factory\{AbstractFactory, UserFactory};
class CommentFactory extends AbstractFactory class CommentFactory extends AbstractFactory
@ -19,13 +20,17 @@ class CommentFactory extends AbstractFactory
/** @var UserFactory */ /** @var UserFactory */
private $userFactory; private $userFactory;
/** @var PostApi */
private $postApi;
public function __construct(LoggerInterface $logger, CommentRepository $commentRepository, PostRepository $postRepository, UserFactory $userFactory)
public function __construct(LoggerInterface $logger, CommentRepository $commentRepository, PostRepository $postRepository, UserFactory $userFactory, PostApi $postApi)
{ {
parent::__construct($logger); parent::__construct($logger);
$this->userFactory = $userFactory; $this->userFactory = $userFactory;
$this->commentRepository = $commentRepository; $this->commentRepository = $commentRepository;
$this->postRepository = $postRepository; $this->postRepository = $postRepository;
$this->postApi = $postApi;
} }
public function findOrCreateFromWebsocketMessage(Message $message): Comment public function findOrCreateFromWebsocketMessage(Message $message): Comment
@ -41,6 +46,19 @@ class CommentFactory extends AbstractFactory
)); ));
} }
if (null === $comment = $this->commentRepository->findOneBy(['post' => $post, 'number' => $message->getCommentId()])) {
$author = $this->userFactory->findOrCreateFromIdLoginAndName(
$message->getAuthorId(),
$message->getAuthor(),
$message->getAuthorName()
);
if (null === $post = $this->postRepository->find($message->getPostId())) {
$post = $this->postApi->getById($message->getPostId());
}
// TODO
//$comment = new Comment()
}
} }
} }

View file

@ -98,8 +98,6 @@ class PostFactory extends AbstractFactory
/** /**
* Create full post with tags, files and comments * Create full post with tags, files and comments
* *
* @todo Implement comments
*
* @throws InvalidDataException * @throws InvalidDataException
*/ */
public function findOrCreateFromDtoWithContent(MetaPost $metaPost): Post public function findOrCreateFromDtoWithContent(MetaPost $metaPost): Post
@ -133,6 +131,8 @@ class PostFactory extends AbstractFactory
throw $e; throw $e;
} }
// @TODO implement comments
return $post; return $post;
} }
@ -149,14 +149,11 @@ class PostFactory extends AbstractFactory
} }
if (null === $post = $this->postRepository->find($message->getPostId())) { if (null === $post = $this->postRepository->find($message->getPostId())) {
/** @var User $author */
if (null === $author = $this->userRepository->find($message->getAuthorId())) {
$author = $this->userFactory->findOrCreateFromIdLoginAndName( $author = $this->userFactory->findOrCreateFromIdLoginAndName(
$message->getAuthorId(), $message->getAuthorId(),
$message->getAuthor(), $message->getAuthor(),
$message->getAuthorName() $message->getAuthorName()
); );
}
$post = new Post( $post = new Post(
$message->getPostId(), $message->getPostId(),

View file

@ -52,10 +52,14 @@ class UserFactory extends AbstractFactory
public function findOrCreateFromIdLoginAndName(int $id, string $login, ?string $name): User public function findOrCreateFromIdLoginAndName(int $id, string $login, ?string $name): User
{ {
/** @var User $user */
if (null === $user = $this->userRepository->find($id)) { if (null === $user = $this->userRepository->find($id)) {
// We're using current date now but next time when we'll be updating user from API it'll be fixed // We're using current date now but next time when we'll be updating user from API it'll be fixed
$user = new User($id, new \DateTime(), $login, $name); $user = new User($id, new \DateTime(), $login, $name);
$this->userRepository->add($user); $this->userRepository->add($user);
} else {
// @todo update login?
// Probably don't because no name in the WS message (or maybe after PR to point?)
} }
return $user; return $user;

View file

@ -54,7 +54,12 @@ class WebSocketMessageProcessor
private function processComment(Message $commentData): bool private function processComment(Message $commentData): bool
{ {
// Not done yet
return false; return false;
$this->commentFactory->findOrCreateFromWebsocketMessage($commentData);
return true;
} }
private function processRecommendation(Message $recommendData): bool private function processRecommendation(Message $recommendData): bool