From 1034ce46927a635c41d7bbf426131d4f88137465 Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Mon, 26 Oct 2015 06:31:24 +0300 Subject: [PATCH] Forgotten code and some other fixes. --- .../PointToolsBundle/Entity/Blogs/Comment.php | 2 +- .../PointToolsBundle/Entity/Blogs/Post.php | 37 ++++++++++++++++++- .../Service/Factory/PostFactory.php | 33 +++++++++++++++-- .../Service/Factory/UserFactory.php | 1 - .../PointToolsBundle/Service/PostApi.php | 8 +--- 5 files changed, 69 insertions(+), 12 deletions(-) diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/Blogs/Comment.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/Blogs/Comment.php index 119794d..f10d227 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Entity/Blogs/Comment.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Entity/Blogs/Comment.php @@ -55,7 +55,7 @@ class Comment * @var Post * * @ORM\Id - * @ORM\ManyToOne(targetEntity="Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post") + * @ORM\ManyToOne(targetEntity="Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post", inversedBy="comments") * @ORM\JoinColumn(name="post_id") */ private $post; diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/Blogs/Post.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/Blogs/Post.php index f472dbd..7cb6ec0 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Entity/Blogs/Post.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Entity/Blogs/Post.php @@ -73,6 +73,8 @@ class Post /** * @var Comment[]|ArrayCollection + * + * @ORM\OneToMany(targetEntity="Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Comment", mappedBy="post", fetch="EXTRA_LAZY") */ private $comments; @@ -212,7 +214,7 @@ class Post /** * Get tags * - * @return ArrayCollection + * @return Tag[]|ArrayCollection */ public function getTags() { @@ -251,4 +253,37 @@ class Post { return $this->deleted; } + + /** + * Add comments + * + * @param Comment $comment + * @return Post + */ + public function addComment(Comment $comment) + { + $this->comments[] = $comment; + + return $this; + } + + /** + * Remove comments + * + * @param Comment $comment + */ + public function removeComment(Comment $comment) + { + $this->comments->removeElement($comment); + } + + /** + * Get comments + * + * @return Comment[]|ArrayCollection + */ + public function getComments() + { + return $this->comments; + } } diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/PostFactory.php b/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/PostFactory.php index 6aa9447..3ed1732 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/PostFactory.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/PostFactory.php @@ -6,10 +6,8 @@ use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post; +use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\ApiException; use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\InvalidResponseException; -use Skobkin\Bundle\PointToolsBundle\Service\Factory\UserFactory; -use Skobkin\Bundle\PointToolsBundle\Service\Factory\CommentFactory; -use Skobkin\Bundle\PointToolsBundle\Service\Factory\TagFactory; class PostFactory @@ -51,6 +49,13 @@ class PostFactory $this->postRepository = $em->getRepository('SkobkinPointToolsBundle:Blogs\Post'); } + /** + * @param array $data + * + * @return Post + * @throws ApiException + * @throws InvalidResponseException + */ public function createFromArray(array $data) { $this->validateData($data); @@ -82,14 +87,36 @@ class PostFactory } } + // 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); + } + // Comments $comments = $this->commentFactory->createFromListArray($data['comments']); // Marking removed comments + foreach ($post->getComments() as $comment) { + if (false === in_array($comment, $comments, true)) { + $comment->setDeleted(true); + } + } + + // Adding comments + foreach ($comments as $comment) { + $post->addComment($comment); + } return $post; } + /** + * @param array $data + * + * @throws InvalidResponseException + */ private function validateData(array $data) { if (!array_key_exists('post', $data)) { diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/UserFactory.php b/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/UserFactory.php index ae4b2e6..4191621 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/UserFactory.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/UserFactory.php @@ -5,7 +5,6 @@ namespace Skobkin\Bundle\PointToolsBundle\Service\Factory; use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; -use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post; use Skobkin\Bundle\PointToolsBundle\Entity\User; use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\ApiException; use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\InvalidResponseException; diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/PostApi.php b/src/Skobkin/Bundle/PointToolsBundle/Service/PostApi.php index 8a8d701..7eb214b 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Service/PostApi.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/PostApi.php @@ -2,12 +2,8 @@ namespace Skobkin\Bundle\PointToolsBundle\Service; -use Doctrine\ORM\EntityManager; -use Doctrine\ORM\EntityManagerInterface; -use Doctrine\ORM\EntityRepository; use Guzzle\Service\Client; use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post; -use Skobkin\Bundle\PointToolsBundle\Entity\User; use Skobkin\Bundle\PointToolsBundle\Service\Factory\PostFactory; /** @@ -34,11 +30,11 @@ class PostApi extends AbstractApi } /** - * Get post with comments by id + * Get post with tags and comments by id * * @param $id * - * @return User[] + * @return Post[] */ public function getPostById($id) {