From a12bf9d9a20d2e44340ffb6fcb337fb68a9edccc Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Thu, 28 Feb 2019 02:26:01 +0300 Subject: [PATCH] WS 'post' processing draft. --- .../ProcessWebsocketUpdatesCommand.php | 12 ++++++-- .../DTO/Api/WebSocket/Message.php | 1 - .../PointToolsBundle/Entity/Blogs/Post.php | 2 +- .../Bundle/PointToolsBundle/Entity/User.php | 11 ++++++- .../Service/Factory/Blogs/PostFactory.php | 29 +++++++++++++++++-- .../Service/Factory/UserFactory.php | 23 ++++++++++----- .../WebSocket/WebSocketMessageProcessor.php | 4 ++- 7 files changed, 66 insertions(+), 16 deletions(-) diff --git a/src/Skobkin/Bundle/PointToolsBundle/Command/ProcessWebsocketUpdatesCommand.php b/src/Skobkin/Bundle/PointToolsBundle/Command/ProcessWebsocketUpdatesCommand.php index 7e5fba3..d51d36d 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Command/ProcessWebsocketUpdatesCommand.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Command/ProcessWebsocketUpdatesCommand.php @@ -2,6 +2,7 @@ namespace Skobkin\Bundle\PointToolsBundle\Command; +use Doctrine\ORM\EntityManagerInterface; use JMS\Serializer\Serializer; use Leezy\PheanstalkBundle\Proxy\PheanstalkProxy; use Pheanstalk\Job; @@ -17,6 +18,9 @@ use Symfony\Component\Console\Output\OutputInterface; */ class ProcessWebsocketUpdatesCommand extends Command { + /** @var EntityManagerInterface */ + private $em; + /** @var PheanstalkProxy */ private $bsClient; @@ -33,12 +37,14 @@ class ProcessWebsocketUpdatesCommand extends Command private $sentryClient; public function __construct( + EntityManagerInterface $em, \Raven_Client $raven, PheanstalkProxy $bsClient, string $bsTubeName, Serializer $serializer, WebSocketMessageProcessor $processor ) { + $this->em = $em; $this->sentryClient = $raven; $this->serializer = $serializer; $this->messageProcessor = $processor; @@ -84,17 +90,19 @@ class ProcessWebsocketUpdatesCommand extends Command try { if ($this->messageProcessor->processMessage($message)) { + $this->em->flush(); + if (!$keepJobs) { $this->bsClient->delete($job); } } } catch (UnsupportedTypeException $e) { - $output->writeln('Unsupported message type: '.$message->getA()); + $output->writeln(' Unsupported message type: '.$message->getA()); $this->sentryClient->captureException($e); continue; } catch (\Exception $e) { - $output->writeln('Message processing error: '.$e->getMessage()); + $output->writeln(' Message processing error: '.$e->getMessage()); $this->sentryClient->captureException($e); continue; diff --git a/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/WebSocket/Message.php b/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/WebSocket/Message.php index 1415e76..f0e8669 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/WebSocket/Message.php +++ b/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/WebSocket/Message.php @@ -295,7 +295,6 @@ class Message implements ValidableInterface return $this->hasCommonMandatoryData() && ( // Text can be empty ("") though null !== $this->text && - null !== $this->private && null !== $this->tags ); } diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/Blogs/Post.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/Blogs/Post.php index 94e5477..55b84dc 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Entity/Blogs/Post.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Entity/Blogs/Post.php @@ -110,7 +110,7 @@ class Post private $comments; - public function __construct(string $id, User $author, \DateTime $createdAt, string $type) + public function __construct(string $id, User $author, \DateTime $createdAt, string $type = self::TYPE_POST) { $this->id = $id; $this->author = $author; diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/User.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/User.php index 5b67a9e..9b5ee5a 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Entity/User.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Entity/User.php @@ -191,15 +191,24 @@ class User return $this->createdAt; } + public function updateCreatedAt(\DateTime $date): self + { + $this->createdAt = $date; + + return $this; + } + public function getUpdatedAt(): ?\DateTime { return $this->updatedAt; } - public function updatePrivacy(?bool $public, ?bool $whitelistOnly): void + public function updatePrivacy(bool $public, bool $whitelistOnly): self { $this->public = $public; $this->whitelistOnly = $whitelistOnly; + + return $this; } public function isPublic(): ?bool diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/Blogs/PostFactory.php b/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/Blogs/PostFactory.php index e14d0e6..28b08a8 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/Blogs/PostFactory.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/Blogs/PostFactory.php @@ -151,7 +151,11 @@ class PostFactory extends AbstractFactory if (null === $post = $this->postRepository->find($message->getPostId())) { /** @var User $author */ if (null === $author = $this->userRepository->find($message->getAuthorId())) { - // @todo create user + $author = $this->userFactory->findOrCreateFromIdLoginAndName( + $message->getAuthorId(), + $message->getAuthor(), + $message->getAuthorName() + ); } $post = new Post( @@ -163,7 +167,26 @@ class PostFactory extends AbstractFactory $this->postRepository->add($post); } - $post->setText($message->getText()); + $post + ->setText($message->getText()) + ->setPrivate((bool) $message->getPrivate()) + ; + + try { + $this->updatePostTags($post, $message->getTags() ?: []); + } catch (\Exception $e) { + $this->logger->error('Error while updating post tags'); + throw $e; + } + + try { + $this->updatePostFiles($post, $message->getFiles() ?: []); + } catch (\Exception $e) { + $this->logger->error('Error while updating post files'); + throw $e; + } + + return $post; } private function findOrCreateFromApiDto(ApiPost $postData, User $author): Post @@ -181,7 +204,7 @@ class PostFactory extends AbstractFactory $post ->setText($postData->getText()) - ->setPrivate($postData->getPrivate()) + ->setPrivate((bool) $postData->getPrivate()) ; return $post; diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/UserFactory.php b/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/UserFactory.php index 4e4a107..df3e54e 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/UserFactory.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/UserFactory.php @@ -20,27 +20,25 @@ class UserFactory extends AbstractFactory } /** - * @param UserDTO $userData - * - * @return User - * * @throws InvalidUserDataException */ public function findOrCreateFromDTO(UserDTO $userData): User { - // @todo LOG - if (!$userData->isValid()) { throw new InvalidUserDataException('Invalid user data', $userData); } + $createdAt = \DateTime::createFromFormat(self::DATE_FORMAT, $userData->getCreated()) ?: new \DateTime(); + /** @var User $user */ if (null === ($user = $this->userRepository->find($userData->getId()))) { $user = new User( $userData->getId(), - \DateTime::createFromFormat(self::DATE_FORMAT, $userData->getCreated()) ?: new \DateTime() + $createdAt ); $this->userRepository->add($user); + } else { + $user->updateCreatedAt($createdAt); } $user->updateLoginAndName($userData->getLogin(), $userData->getName()); @@ -52,6 +50,17 @@ class UserFactory extends AbstractFactory return $user; } + public function findOrCreateFromIdLoginAndName(int $id, string $login, ?string $name): 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); + } + + return $user; + } + /** * @return User[] */ diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/WebSocket/WebSocketMessageProcessor.php b/src/Skobkin/Bundle/PointToolsBundle/Service/WebSocket/WebSocketMessageProcessor.php index faea402..c3d2e8d 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Service/WebSocket/WebSocketMessageProcessor.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/WebSocket/WebSocketMessageProcessor.php @@ -47,7 +47,9 @@ class WebSocketMessageProcessor private function processPost(Message $postData): bool { - return false; + $this->postFactory->findOrCreateFromWebsocketDto($postData); + + return true; } private function processComment(Message $commentData): bool