From 3e3cc96d77200f55822fb6f409f7137fd78ff8ef Mon Sep 17 00:00:00 2001 From: Ondrej Vlach Date: Fri, 2 Aug 2024 14:14:06 +0200 Subject: [PATCH] feat(db): add remote convertors --- .../Persist/RemoteCommentConvertor.php | 72 +++++++++++ src/Service/Persist/RemotePostsConvertor.php | 72 +++++++++++ src/Service/Persist/RemoteUsersConvertor.php | 116 ++++++++++++++++++ 3 files changed, 260 insertions(+) create mode 100644 src/Service/Persist/RemoteCommentConvertor.php create mode 100644 src/Service/Persist/RemotePostsConvertor.php create mode 100644 src/Service/Persist/RemoteUsersConvertor.php diff --git a/src/Service/Persist/RemoteCommentConvertor.php b/src/Service/Persist/RemoteCommentConvertor.php new file mode 100644 index 0000000..3cf6d2a --- /dev/null +++ b/src/Service/Persist/RemoteCommentConvertor.php @@ -0,0 +1,72 @@ + + */ + public function convert(iterable $remoteComments): \Generator + { + foreach ($remoteComments as $remoteComment) { + $comment = $this->commentsRepository->find($remoteComment->id); + if ($comment === null) { + $this->logger->info("Creating new comment for ID: {$remoteComment->id}"); + yield $this->createComment($remoteComment); + } else { + $this->logger->info("Updating post for ID: {$remoteComment->id}"); + $this->updateComment($remoteComment, $comment); + yield $comment; + } + } + } + + /** + * Create new comment entity from remote comment + * @param Comment $remoteComment + * @return \App\Entity\Database\Comments\Comment + */ + protected function createComment(Comment $remoteComment): \App\Entity\Database\Comments\Comment + { + $post = $this->postRepository->find($remoteComment->postId) ?? throw new \LogicException("Post not found for ID: {$remoteComment->postId}"); + return new \App\Entity\Database\Comments\Comment( + $remoteComment->id, + $post, + $remoteComment->name, + $remoteComment->email, + $remoteComment->body + ); + } + + /** + * Update existing comment entity with remote comment data + * @param Comment $remoteComment + * @param \App\Entity\Database\Comments\Comment $comment + * @return void + */ + protected function updateComment(Comment $remoteComment, \App\Entity\Database\Comments\Comment $comment): void + { + $post = $this->postRepository->find($remoteComment->postId) ?? throw new \LogicException("Post not found for ID: {$remoteComment->postId}"); + $comment->post = $post; + $comment->name = $remoteComment->name; + $comment->email = $remoteComment->email; + $comment->body = $remoteComment->body; + } +} diff --git a/src/Service/Persist/RemotePostsConvertor.php b/src/Service/Persist/RemotePostsConvertor.php new file mode 100644 index 0000000..9286abc --- /dev/null +++ b/src/Service/Persist/RemotePostsConvertor.php @@ -0,0 +1,72 @@ + + */ + public function convert(iterable $remotePosts): \Generator + { + foreach ($remotePosts as $remotePost) { + $post = $this->postRepository->find($remotePost->id); + if ($post === null) { + $this->logger->info("Creating new post for ID: {$remotePost->id}"); + yield $this->createPost($remotePost); + } else { + $this->logger->info("Updating post for ID: {$remotePost->id}"); + $this->updatePost($remotePost, $post); + yield $post; + } + } + } + + /** + * Create a new post from the remote data. + * @param Post $remotePost + * @return \App\Entity\Database\Posts\Post + */ + protected function createPost(Post $remotePost): \App\Entity\Database\Posts\Post + { + $user = $this->userRepository->find($remotePost->userId) ?? throw new \LogicException("User not found for ID: {$remotePost->userId}"); + return new \App\Entity\Database\Posts\Post( + $remotePost->id, + $user, + $remotePost->title, + $remotePost->body, + new ArrayCollection(), + ); + } + + /** + * Update an existing post with the remote data. + * @param Post $remotePost + * @param \App\Entity\Database\Posts\Post $post + * @return void + */ + protected function updatePost(Post $remotePost, \App\Entity\Database\Posts\Post $post): void + { + $user = $this->userRepository->find($remotePost->userId) ?? throw new \LogicException("User not found for ID: {$remotePost->userId}"); + $post->user = $user; + $post->title = $remotePost->title; + $post->body = $remotePost->body; + } +} diff --git a/src/Service/Persist/RemoteUsersConvertor.php b/src/Service/Persist/RemoteUsersConvertor.php new file mode 100644 index 0000000..cdc12f7 --- /dev/null +++ b/src/Service/Persist/RemoteUsersConvertor.php @@ -0,0 +1,116 @@ + + */ + public function convert(iterable $remoteUsers): \Generator + { + $ids = array_map(function ($remoteUser) { + return $remoteUser->id; + }, (array) $remoteUsers); + + $existentIds = $this->userRepository->findByIds($ids); + + foreach ($remoteUsers as $remoteUser) { + if (isset($existentIds[$remoteUser->id])) { + $localUser = $existentIds[$remoteUser->id]; + $this->updateUser($remoteUser, $localUser); + $this->logger->debug("Updating user with ID: {$remoteUser->id}"); + yield $localUser; + } else { + $this->logger->debug("Creating new user with ID: {$remoteUser->id}"); + yield $this->createUser($remoteUser); + } + } + } + + /** + * Update user from the given remote user + * @param User $remoteUser + * @param \App\Entity\Database\Users\User $user + * @return void + */ + protected function updateUser(User $remoteUser, \App\Entity\Database\Users\User $user): void + { + $user->name = $remoteUser->name; + $user->username = $remoteUser->username; + $user->email = $remoteUser->email; + $user->phone = $remoteUser->phone; + $user->website = $remoteUser->website; + $user->address->city = $remoteUser->address->city; + $user->address->street = $remoteUser->address->street; + $user->address->suite = $remoteUser->address->suite; + $user->address->zipcode = $remoteUser->address->zipcode; + $user->address->lat = (float) $remoteUser->address->geo->lat; + $user->address->lng = (float) $remoteUser->address->geo->lng; + if ($user->company->name !== $remoteUser->company->name) { + $user->company = $this->companyRepository->findByName($remoteUser->company->name) ?? new Company( + null, + new ArrayCollection(), + $remoteUser->company->name, + $remoteUser->company->catchPhrase, + $remoteUser->company->bs, + ); + } else { + $user->company->catchPhrase = $remoteUser->company->catchPhrase; + $user->company->bs = $remoteUser->company->bs; + } + } + + /** + * Creates a new user from the given remote user + * @param User $remoteUser + * @return \App\Entity\Database\Users\User + */ + protected function createUser(User $remoteUser): \App\Entity\Database\Users\User + { + return new \App\Entity\Database\Users\User( + $remoteUser->id, + $remoteUser->name, + $remoteUser->username, + $remoteUser->email, + $remoteUser->phone, + $remoteUser->website, + new Address( + null, + null, + $remoteUser->address->street, + $remoteUser->address->suite, + $remoteUser->address->city, + $remoteUser->address->zipcode, + (float) $remoteUser->address->geo->lat, + (float) $remoteUser->address->geo->lng, + ), + $this->companyRepository->findByName($remoteUser->company->name) ?? new Company( + null, + new ArrayCollection(), + $remoteUser->company->name, + $remoteUser->company->catchPhrase, + $remoteUser->company->bs, + ) + ); + } +}