userFactory = $userFactory; $this->commentFactory = $commentFactory; $this->tagFactory = $tagFactory; $this->em = $em; $this->postRepository = $em->getRepository('SkobkinPointToolsBundle:Blogs\Post'); } 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); } } // Comments $comments = $this->commentFactory->createFromListArray($data['comments']); // Marking removed comments return $post; } 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'); } } }