diff --git a/src/Skobkin/Bundle/PointToolsBundle/Command/ProcessWebsocketUpdatesCommand.php b/src/Skobkin/Bundle/PointToolsBundle/Command/ProcessWebsocketUpdatesCommand.php index d51d36d..a802847 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Command/ProcessWebsocketUpdatesCommand.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Command/ProcessWebsocketUpdatesCommand.php @@ -95,6 +95,8 @@ class ProcessWebsocketUpdatesCommand extends Command if (!$keepJobs) { $this->bsClient->delete($job); } + + // todo cleaning IdentityMap } } catch (UnsupportedTypeException $e) { $output->writeln(' Unsupported message type: '.$message->getA()); diff --git a/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/Comment.php b/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/Comment.php index 26916f3..edd5334 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/Comment.php +++ b/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/Comment.php @@ -4,39 +4,22 @@ namespace Skobkin\Bundle\PointToolsBundle\DTO\Api; class Comment implements ValidableInterface { - /** - * @var string|null - */ - private $postId; - - /** - * @var int|null - */ + /** @var int|null */ private $number; - /** - * @var int|null - */ + /** @var int|null */ private $toCommentId; - /** - * @var string|null - */ + /** @var string|null */ private $created; - /** - * @var string|null - */ + /** @var string|null */ private $text; - /** - * @var User|null - */ + /** @var User|null */ private $author; - /** - * @var bool|null - */ + /** @var bool|null */ private $isRec; diff --git a/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/MetaPost.php b/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/MetaPost.php index cb2f26a..764c998 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/MetaPost.php +++ b/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/MetaPost.php @@ -4,27 +4,17 @@ namespace Skobkin\Bundle\PointToolsBundle\DTO\Api; class MetaPost implements ValidableInterface { - /** - * @var Post|null - */ + /** @var Post|null */ private $post; - /** - * @var Comment[]|null - */ + /** @var Comment[]|null */ private $comments; - public function getPost(): ?Post { return $this->post; } - public function setPost(?Post $post): void - { - $this->post = $post; - } - /** * @return Comment[]|null */ @@ -33,20 +23,8 @@ class MetaPost implements ValidableInterface return $this->comments; } - /** - * @param Comment[]|null $comments - */ - public function setComments(?array $comments): void - { - $this->comments = $comments; - } - public function isValid(): bool { - if (null !== $this->post && $this->post->isValid()) { - return true; - } - - return false; + return (null !== $this->post && $this->post->isValid()); } } \ No newline at end of file diff --git a/src/Skobkin/Bundle/PointToolsBundle/Exception/Api/PostNotFoundException.php b/src/Skobkin/Bundle/PointToolsBundle/Exception/Api/PostNotFoundException.php new file mode 100644 index 0000000..567c7c7 --- /dev/null +++ b/src/Skobkin/Bundle/PointToolsBundle/Exception/Api/PostNotFoundException.php @@ -0,0 +1,20 @@ +id = $id; + } + + public function getId(): string + { + return $this->id; + } +} \ No newline at end of file diff --git a/src/Skobkin/Bundle/PointToolsBundle/Exception/Api/UserNotFoundException.php b/src/Skobkin/Bundle/PointToolsBundle/Exception/Api/UserNotFoundException.php index 31da7b5..442e528 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Exception/Api/UserNotFoundException.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Exception/Api/UserNotFoundException.php @@ -4,16 +4,11 @@ namespace Skobkin\Bundle\PointToolsBundle\Exception\Api; class UserNotFoundException extends NotFoundException { - /** - * @var int - */ - protected $userId; - - /** - * @var string - */ - protected $login; + /** @var int */ + private $userId; + /** @var string */ + private $login; /** * {@inheritdoc} diff --git a/src/Skobkin/Bundle/PointToolsBundle/Resources/config/serializer/DTO.Api.Comment.yml b/src/Skobkin/Bundle/PointToolsBundle/Resources/config/serializer/DTO.Api.Comment.yml index 4e24a05..a545993 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Resources/config/serializer/DTO.Api.Comment.yml +++ b/src/Skobkin/Bundle/PointToolsBundle/Resources/config/serializer/DTO.Api.Comment.yml @@ -2,10 +2,6 @@ Skobkin\Bundle\PointToolsBundle\DTO\Api\Comment: exclusion_policy: none access_type: public_method properties: - postId: - serialized_name: 'post_id' - type: 'Skobkin\Bundle\PointToolsBundle\DTO\Api\Post' - max_depth: 2 number: serialized_name: 'id' type: integer diff --git a/src/Skobkin/Bundle/PointToolsBundle/Resources/config/serializer/DTO.Api.MetaPost.yml b/src/Skobkin/Bundle/PointToolsBundle/Resources/config/serializer/DTO.Api.MetaPost.yml index fe6290f..a2571c1 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Resources/config/serializer/DTO.Api.MetaPost.yml +++ b/src/Skobkin/Bundle/PointToolsBundle/Resources/config/serializer/DTO.Api.MetaPost.yml @@ -1,10 +1,9 @@ Skobkin\Bundle\PointToolsBundle\DTO\Api\MetaPost: exclusion_policy: none - access_type: public_method properties: post: + serialized_name: 'post' type: 'Skobkin\Bundle\PointToolsBundle\DTO\Api\Post' - max_depth: 2 comments: + serialized_name: 'comments' type: 'array' - max_depth: 2 diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/Api/PostApi.php b/src/Skobkin/Bundle/PointToolsBundle/Service/Api/PostApi.php index e9875a4..8a3f798 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Service/Api/PostApi.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/Api/PostApi.php @@ -5,6 +5,9 @@ namespace Skobkin\Bundle\PointToolsBundle\Service\Api; use GuzzleHttp\ClientInterface; use JMS\Serializer\SerializerInterface; 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; /** @@ -12,6 +15,8 @@ use Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\PostFactory; */ class PostApi extends AbstractApi { + private const PREFIX = '/api/post/'; + /** * @var PostFactory */ @@ -24,4 +29,24 @@ class PostApi extends AbstractApi $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); + } } diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/Api/UserApi.php b/src/Skobkin/Bundle/PointToolsBundle/Service/Api/UserApi.php index fc265e0..ce529fe 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Service/Api/UserApi.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/Api/UserApi.php @@ -3,9 +3,7 @@ namespace Skobkin\Bundle\PointToolsBundle\Service\Api; use GuzzleHttp\ClientInterface; -use JMS\Serializer\{ - DeserializationContext, SerializerInterface -}; +use JMS\Serializer\{DeserializationContext, SerializerInterface}; use Psr\Log\LoggerInterface; use Skobkin\Bundle\PointToolsBundle\DTO\Api\{Auth, User as UserDTO}; use Skobkin\Bundle\PointToolsBundle\Entity\User; diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/Blogs/CommentFactory.php b/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/Blogs/CommentFactory.php index 33b294d..33bbcae 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/Blogs/CommentFactory.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/Blogs/CommentFactory.php @@ -6,6 +6,7 @@ use Psr\Log\LoggerInterface; use Skobkin\Bundle\PointToolsBundle\DTO\Api\WebSocket\Message; use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Comment; use Skobkin\Bundle\PointToolsBundle\Repository\Blogs\{CommentRepository, PostRepository}; +use Skobkin\Bundle\PointToolsBundle\Service\Api\PostApi; use Skobkin\Bundle\PointToolsBundle\Service\Factory\{AbstractFactory, UserFactory}; class CommentFactory extends AbstractFactory @@ -19,13 +20,17 @@ class CommentFactory extends AbstractFactory /** @var 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); $this->userFactory = $userFactory; $this->commentRepository = $commentRepository; $this->postRepository = $postRepository; + $this->postApi = $postApi; } 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() + } } } \ No newline at end of file diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/Blogs/PostFactory.php b/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/Blogs/PostFactory.php index 28b08a8..9d5670e 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/Blogs/PostFactory.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/Blogs/PostFactory.php @@ -98,8 +98,6 @@ class PostFactory extends AbstractFactory /** * Create full post with tags, files and comments * - * @todo Implement comments - * * @throws InvalidDataException */ public function findOrCreateFromDtoWithContent(MetaPost $metaPost): Post @@ -133,6 +131,8 @@ class PostFactory extends AbstractFactory throw $e; } + // @TODO implement comments + return $post; } @@ -149,14 +149,11 @@ class PostFactory extends AbstractFactory } if (null === $post = $this->postRepository->find($message->getPostId())) { - /** @var User $author */ - if (null === $author = $this->userRepository->find($message->getAuthorId())) { - $author = $this->userFactory->findOrCreateFromIdLoginAndName( - $message->getAuthorId(), - $message->getAuthor(), - $message->getAuthorName() - ); - } + $author = $this->userFactory->findOrCreateFromIdLoginAndName( + $message->getAuthorId(), + $message->getAuthor(), + $message->getAuthorName() + ); $post = new Post( $message->getPostId(), diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/UserFactory.php b/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/UserFactory.php index df3e54e..0ca8fee 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/UserFactory.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/UserFactory.php @@ -52,10 +52,14 @@ class UserFactory extends AbstractFactory public function findOrCreateFromIdLoginAndName(int $id, string $login, ?string $name): User { + /** @var User $user */ 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 $user = new User($id, new \DateTime(), $login, $name); $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; diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/WebSocket/WebSocketMessageProcessor.php b/src/Skobkin/Bundle/PointToolsBundle/Service/WebSocket/WebSocketMessageProcessor.php index c3d2e8d..0674051 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Service/WebSocket/WebSocketMessageProcessor.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/WebSocket/WebSocketMessageProcessor.php @@ -54,7 +54,12 @@ class WebSocketMessageProcessor private function processComment(Message $commentData): bool { + // Not done yet return false; + + $this->commentFactory->findOrCreateFromWebsocketMessage($commentData); + + return true; } private function processRecommendation(Message $recommendData): bool