2015-10-26 02:57:53 +00:00
|
|
|
<?php
|
|
|
|
|
2016-03-23 17:06:49 +00:00
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs;
|
2015-10-26 02:57:53 +00:00
|
|
|
|
2017-01-08 23:26:06 +00:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2016-03-23 22:49:22 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler\MetaPost;
|
|
|
|
use Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler\PostsPage;
|
2015-10-26 02:57:53 +00:00
|
|
|
use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post;
|
2016-03-23 22:49:22 +00:00
|
|
|
use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\PostTag;
|
2017-01-09 18:43:41 +00:00
|
|
|
use Skobkin\Bundle\PointToolsBundle\Exception\Factory\Blog\InvalidDataException;
|
2017-01-11 16:27:08 +00:00
|
|
|
use Skobkin\Bundle\PointToolsBundle\Repository\Blogs\PostRepository;
|
2015-10-26 03:31:24 +00:00
|
|
|
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\ApiException;
|
2017-01-09 18:43:41 +00:00
|
|
|
use Skobkin\Bundle\PointToolsBundle\Exception\Factory\Blog\InvalidPostDataException;
|
2015-10-26 02:57:53 +00:00
|
|
|
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\InvalidResponseException;
|
2016-03-23 17:06:49 +00:00
|
|
|
use Skobkin\Bundle\PointToolsBundle\Service\Factory\UserFactory;
|
2015-10-26 02:57:53 +00:00
|
|
|
|
|
|
|
class PostFactory
|
|
|
|
{
|
2016-03-23 22:49:22 +00:00
|
|
|
/**
|
|
|
|
* @var LoggerInterface
|
|
|
|
*/
|
2017-01-11 16:27:08 +00:00
|
|
|
private $logger;
|
2016-03-23 22:49:22 +00:00
|
|
|
|
2015-10-26 02:57:53 +00:00
|
|
|
/**
|
2017-01-08 23:26:06 +00:00
|
|
|
* @var EntityManagerInterface
|
2015-10-26 02:57:53 +00:00
|
|
|
*/
|
|
|
|
private $em;
|
|
|
|
|
|
|
|
/**
|
2017-01-11 16:27:08 +00:00
|
|
|
* @var PostRepository
|
2015-10-26 02:57:53 +00:00
|
|
|
*/
|
|
|
|
private $postRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var UserFactory
|
|
|
|
*/
|
|
|
|
private $userFactory;
|
|
|
|
|
2016-03-24 21:28:43 +00:00
|
|
|
/**
|
|
|
|
* @var FileFactory
|
|
|
|
*/
|
|
|
|
private $fileFactory;
|
|
|
|
|
2015-10-26 02:57:53 +00:00
|
|
|
/**
|
|
|
|
* @var CommentFactory
|
|
|
|
*/
|
|
|
|
private $commentFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var TagFactory
|
|
|
|
*/
|
|
|
|
private $tagFactory;
|
|
|
|
|
2017-01-08 23:26:06 +00:00
|
|
|
|
2017-01-11 16:27:08 +00:00
|
|
|
public function __construct(
|
|
|
|
LoggerInterface $logger,
|
|
|
|
EntityManagerInterface $em,
|
|
|
|
PostRepository $postRepository,
|
|
|
|
UserFactory $userFactory,
|
|
|
|
FileFactory $fileFactory,
|
|
|
|
CommentFactory $commentFactory,
|
|
|
|
TagFactory $tagFactory
|
|
|
|
) {
|
|
|
|
$this->logger = $logger;
|
|
|
|
$this->em = $em;
|
|
|
|
$this->postRepository = $postRepository;
|
2015-10-26 02:57:53 +00:00
|
|
|
$this->userFactory = $userFactory;
|
2016-03-24 21:28:43 +00:00
|
|
|
$this->fileFactory = $fileFactory;
|
2015-10-26 02:57:53 +00:00
|
|
|
$this->commentFactory = $commentFactory;
|
|
|
|
$this->tagFactory = $tagFactory;
|
|
|
|
}
|
|
|
|
|
2015-10-26 03:31:24 +00:00
|
|
|
/**
|
2016-03-23 22:49:22 +00:00
|
|
|
* Creates posts and return status of new insertions
|
2015-10-26 03:31:24 +00:00
|
|
|
*
|
2016-12-12 18:23:11 +00:00
|
|
|
* @param PostsPage $page
|
2016-03-23 22:49:22 +00:00
|
|
|
*
|
|
|
|
* @return bool
|
2017-01-06 21:38:20 +00:00
|
|
|
*
|
2015-10-26 03:31:24 +00:00
|
|
|
* @throws ApiException
|
|
|
|
* @throws InvalidResponseException
|
|
|
|
*/
|
2017-01-08 23:26:06 +00:00
|
|
|
public function createFromPageDTO(PostsPage $page): bool
|
2015-10-26 02:57:53 +00:00
|
|
|
{
|
2016-03-23 22:49:22 +00:00
|
|
|
$posts = [];
|
2015-10-26 02:57:53 +00:00
|
|
|
|
2016-03-23 22:49:22 +00:00
|
|
|
$hasNew = false;
|
2015-10-26 02:57:53 +00:00
|
|
|
|
2016-03-23 22:49:22 +00:00
|
|
|
foreach ($page->getPosts() as $postData) {
|
|
|
|
try {
|
|
|
|
if (null === $this->postRepository->find($postData->getPost()->getId())) {
|
|
|
|
$hasNew = true;
|
|
|
|
}
|
2015-10-26 02:57:53 +00:00
|
|
|
|
2016-03-23 22:49:22 +00:00
|
|
|
$post = $this->createFromDTO($postData);
|
|
|
|
$posts[] = $post;
|
|
|
|
} catch (\Exception $e) {
|
2017-01-11 16:27:08 +00:00
|
|
|
$this->logger->error('Error while processing post DTO', [
|
2016-03-25 23:48:39 +00:00
|
|
|
'post_id' => $postData->getPost()->getId(),
|
|
|
|
'exception' => get_class($e),
|
|
|
|
'message' => $e->getMessage(),
|
|
|
|
'file' => $e->getFile(),
|
|
|
|
'line' => $e->getLine(),
|
|
|
|
]);
|
2015-10-26 02:57:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 22:49:22 +00:00
|
|
|
foreach ($posts as $post) {
|
|
|
|
if ($this->em->getUnitOfWork()->isScheduledForInsert($post)) {
|
|
|
|
$hasNew = true;
|
2015-10-26 02:57:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 22:49:22 +00:00
|
|
|
return $hasNew;
|
|
|
|
}
|
2015-10-26 02:57:53 +00:00
|
|
|
|
2016-03-23 22:49:22 +00:00
|
|
|
/**
|
|
|
|
* @todo Implement full post with comments processing
|
|
|
|
*
|
|
|
|
* @param MetaPost $postData
|
|
|
|
*
|
|
|
|
* @return Post
|
2017-01-08 23:26:06 +00:00
|
|
|
*
|
2016-03-23 22:49:22 +00:00
|
|
|
* @throws ApiException
|
2017-01-09 18:43:41 +00:00
|
|
|
* @throws InvalidDataException
|
2016-03-23 22:49:22 +00:00
|
|
|
*/
|
2017-01-08 23:26:06 +00:00
|
|
|
private function createFromDTO(MetaPost $postData): Post
|
2016-03-23 22:49:22 +00:00
|
|
|
{
|
|
|
|
if (!$this->validateMetaPost($postData)) {
|
2017-01-09 18:43:41 +00:00
|
|
|
throw new InvalidDataException('Invalid post data');
|
2015-10-26 03:31:24 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 22:49:22 +00:00
|
|
|
if (!$postData->getPost()->getAuthor()->getId()) {
|
2017-01-11 16:27:08 +00:00
|
|
|
$this->logger->error('Post author does not contain id', ['post_id' => $postData->getPost()->getId()]);
|
2017-01-08 23:26:06 +00:00
|
|
|
throw new InvalidPostDataException('Post author does not contain id', $postData->getPost());
|
2015-10-26 03:31:24 +00:00
|
|
|
}
|
2015-10-26 02:57:53 +00:00
|
|
|
|
2016-03-25 23:48:39 +00:00
|
|
|
try {
|
|
|
|
$user = $this->userFactory->createFromDTO($postData->getPost()->getAuthor());
|
|
|
|
} catch (\Exception $e) {
|
2017-01-11 16:27:08 +00:00
|
|
|
$this->logger->error('Error while creating user from DTO');
|
2016-03-25 23:48:39 +00:00
|
|
|
throw $e;
|
|
|
|
}
|
2016-03-23 22:49:22 +00:00
|
|
|
|
|
|
|
if (null === ($post = $this->postRepository->find($postData->getPost()->getId()))) {
|
|
|
|
// Creating new post
|
|
|
|
$post = new Post($postData->getPost()->getId());
|
2017-01-11 16:27:08 +00:00
|
|
|
$this->postRepository->add($post);
|
2016-03-23 22:49:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Updating data
|
|
|
|
$post
|
|
|
|
->setAuthor($user)
|
|
|
|
->setCreatedAt((new \DateTime($postData->getPost()->getCreated())) ?: null)
|
|
|
|
// @fixme Need bugfix for point API (type is not showing now)
|
|
|
|
//->setType($postData->getPost()->getType())
|
|
|
|
->setText($postData->getPost()->getText())
|
|
|
|
->setPrivate($postData->getPost()->getPrivate())
|
|
|
|
;
|
|
|
|
|
2016-03-25 23:48:39 +00:00
|
|
|
try {
|
|
|
|
$this->updatePostTags($post, $postData->getPost()->getTags() ?: []);
|
|
|
|
} catch (\Exception $e) {
|
2017-01-11 16:27:08 +00:00
|
|
|
$this->logger->error('Error while updating post tags');
|
2016-03-25 23:48:39 +00:00
|
|
|
throw $e;
|
|
|
|
}
|
2016-03-23 22:49:22 +00:00
|
|
|
|
2016-03-25 23:48:39 +00:00
|
|
|
try {
|
|
|
|
$this->updatePostFiles($post, $postData->getPost()->getFiles() ?: []);
|
|
|
|
} catch (\Exception $e) {
|
2017-01-11 16:27:08 +00:00
|
|
|
$this->logger->error('Error while updating post files');
|
2016-03-25 23:48:39 +00:00
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
|
2015-10-26 02:57:53 +00:00
|
|
|
return $post;
|
|
|
|
}
|
|
|
|
|
2015-10-26 03:31:24 +00:00
|
|
|
/**
|
2016-03-23 22:49:22 +00:00
|
|
|
* @param Post $post
|
2016-12-12 18:23:11 +00:00
|
|
|
* @param string[] $tagsStrings
|
2015-10-26 03:31:24 +00:00
|
|
|
*/
|
2016-03-23 22:49:22 +00:00
|
|
|
private function updatePostTags(Post $post, array $tagsStrings)
|
2015-10-26 02:57:53 +00:00
|
|
|
{
|
2016-03-23 22:49:22 +00:00
|
|
|
$tags = $this->tagFactory->createFromStringsArray($tagsStrings);
|
|
|
|
|
|
|
|
// Hashing tags strings
|
|
|
|
$tagStringsHash = [];
|
|
|
|
foreach ($tagsStrings as $tagsString) {
|
|
|
|
$tagStringsHash[mb_strtolower($tagsString)] = $tagsString;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hashing current post tags
|
|
|
|
$newTagsHash = [];
|
|
|
|
foreach ($tags as $tag) {
|
|
|
|
$newTagsHash[mb_strtolower($tag->getText())] = $tag;
|
2015-10-26 02:57:53 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 22:49:22 +00:00
|
|
|
// Hashing old post tags (from DB)
|
|
|
|
$oldTagsHash = [];
|
|
|
|
foreach ($post->getPostTags() as $postTag) {
|
|
|
|
$oldTagsHash[mb_strtolower($postTag->getOriginalTagText())] = $postTag;
|
2015-10-26 02:57:53 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 22:49:22 +00:00
|
|
|
// Adding missing tags
|
|
|
|
foreach ($tags as $tag) {
|
|
|
|
if (!array_key_exists(mb_strtolower($tag->getText()), $oldTagsHash)) {
|
|
|
|
$tmpPostTag = (new PostTag($tag))
|
|
|
|
->setText($tagStringsHash[mb_strtolower($tag->getText())])
|
|
|
|
;
|
|
|
|
$post->addPostTag($tmpPostTag);
|
|
|
|
}
|
2015-10-26 02:57:53 +00:00
|
|
|
}
|
2016-03-23 22:49:22 +00:00
|
|
|
|
|
|
|
// Removing deleted tags
|
|
|
|
foreach ($post->getPostTags() as $postTag) {
|
|
|
|
if (!array_key_exists(mb_strtolower($postTag->getOriginalTagText()), $newTagsHash)) {
|
|
|
|
$post->removePostTag($postTag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 21:28:43 +00:00
|
|
|
/**
|
|
|
|
* @param Post $post
|
|
|
|
* @param array $urls
|
|
|
|
*/
|
|
|
|
private function updatePostFiles(Post $post, array $urls)
|
|
|
|
{
|
|
|
|
$files = $this->fileFactory->createFromUrlsArray($urls);
|
|
|
|
|
|
|
|
// Adding missing files
|
|
|
|
foreach ($files as $file) {
|
|
|
|
if (!$post->getFiles()->contains($file)) {
|
|
|
|
$post->addFile($file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removing deleted files
|
|
|
|
foreach ($post->getFiles() as $file) {
|
|
|
|
if (!in_array($file, $files, true)) {
|
|
|
|
$post->removeFile($file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 22:49:22 +00:00
|
|
|
private function validateMetaPost(MetaPost $post)
|
|
|
|
{
|
|
|
|
if (!$post->getPost()->getId()) {
|
2017-01-11 16:27:08 +00:00
|
|
|
$this->logger->error('Post DTO contains no id');
|
2016-03-23 22:49:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null === $post->getPost()->getAuthor()->getId()) {
|
2017-01-11 16:27:08 +00:00
|
|
|
$this->logger->error('Post DTO contains no valid User DTO.', ['post_id' => $post->getPost()->getId()]);
|
2016-03-23 22:49:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2015-10-26 02:57:53 +00:00
|
|
|
}
|
|
|
|
}
|