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
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManager;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use Doctrine\ORM\EntityRepository;
|
|
|
|
use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post;
|
2015-10-26 03:31:24 +00:00
|
|
|
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\ApiException;
|
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
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var EntityManager
|
|
|
|
*/
|
|
|
|
private $em;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var EntityRepository
|
|
|
|
*/
|
|
|
|
private $postRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var UserFactory
|
|
|
|
*/
|
|
|
|
private $userFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var CommentFactory
|
|
|
|
*/
|
|
|
|
private $commentFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var TagFactory
|
|
|
|
*/
|
|
|
|
private $tagFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param EntityManager $em
|
|
|
|
*/
|
|
|
|
public function __construct(EntityManagerInterface $em, UserFactory $userFactory, CommentFactory $commentFactory, TagFactory $tagFactory)
|
|
|
|
{
|
|
|
|
$this->userFactory = $userFactory;
|
|
|
|
$this->commentFactory = $commentFactory;
|
|
|
|
$this->tagFactory = $tagFactory;
|
|
|
|
$this->em = $em;
|
|
|
|
$this->postRepository = $em->getRepository('SkobkinPointToolsBundle:Blogs\Post');
|
|
|
|
}
|
|
|
|
|
2015-10-26 03:31:24 +00:00
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return Post
|
|
|
|
* @throws ApiException
|
|
|
|
* @throws InvalidResponseException
|
|
|
|
*/
|
2015-10-26 02:57:53 +00:00
|
|
|
public function createFromArray(array $data)
|
|
|
|
{
|
|
|
|
$this->validateData($data);
|
|
|
|
|
|
|
|
if (null === ($post = $this->postRepository->find($data['post']['id']))) {
|
|
|
|
$createdAt = new \DateTime($data['post']['created']);
|
|
|
|
$author = $this->userFactory->createFromArray($data['post']['author']);
|
|
|
|
|
|
|
|
$post = new Post($data['post']['id'], $data['post']['type'], $data['post']['text'], $createdAt, $author);
|
|
|
|
$this->em->persist($post);
|
|
|
|
}
|
|
|
|
|
|
|
|
$post->setText($data['post']['text']);
|
|
|
|
|
|
|
|
// Tags
|
|
|
|
$tags = $this->tagFactory->createFromListArray($data['post']['tags']);
|
|
|
|
|
|
|
|
// Removing deleted tags
|
|
|
|
foreach ($post->getTags() as $tag) {
|
|
|
|
if (false === in_array($tag, $tags, true)) {
|
|
|
|
$post->removeTag($tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adding new tags
|
|
|
|
foreach ($tags as $tag) {
|
|
|
|
if (!$post->getTags()->contains($tag)) {
|
|
|
|
$post->addTag($tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-26 03:31:24 +00:00
|
|
|
// Flushing post before linking comments
|
|
|
|
try {
|
|
|
|
$this->em->flush($post);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw new ApiException(sprintf('Error while flushing changes for #%s: %s', $data['post']['id'], $e->getMessage()), 0, $e);
|
|
|
|
}
|
|
|
|
|
2015-10-26 02:57:53 +00:00
|
|
|
// Comments
|
|
|
|
$comments = $this->commentFactory->createFromListArray($data['comments']);
|
|
|
|
|
|
|
|
// Marking removed comments
|
2015-10-26 03:31:24 +00:00
|
|
|
foreach ($post->getComments() as $comment) {
|
|
|
|
if (false === in_array($comment, $comments, true)) {
|
|
|
|
$comment->setDeleted(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adding comments
|
|
|
|
foreach ($comments as $comment) {
|
|
|
|
$post->addComment($comment);
|
|
|
|
}
|
2015-10-26 02:57:53 +00:00
|
|
|
|
|
|
|
return $post;
|
|
|
|
}
|
|
|
|
|
2015-10-26 03:31:24 +00:00
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @throws InvalidResponseException
|
|
|
|
*/
|
2015-10-26 02:57:53 +00:00
|
|
|
private function validateData(array $data)
|
|
|
|
{
|
|
|
|
if (!array_key_exists('post', $data)) {
|
|
|
|
throw new InvalidResponseException('Post data not found in API response');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!array_key_exists('comments', $data)) {
|
|
|
|
throw new InvalidResponseException('Comments data not found in API response');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(
|
|
|
|
array_key_exists('id', $data['post']) &&
|
|
|
|
array_key_exists('type', $data['post']) &&
|
|
|
|
array_key_exists('text', $data['post']) &&
|
|
|
|
array_key_exists('tags', $data['post']) &&
|
|
|
|
array_key_exists('author', $data['post'])
|
|
|
|
)) {
|
|
|
|
throw new InvalidResponseException('Post content not found in API response');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|