PostApi now using PostFactory.

This commit is contained in:
Alexey Skobkin 2015-10-26 06:00:39 +03:00
parent 3b25afd6aa
commit 98252f6dff
2 changed files with 16 additions and 101 deletions

View File

@ -8,25 +8,24 @@ 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;
/**
* Basic Point.im user API functions from /api/user/*
* Basic Point.im user API functions from /api/post
*/
class PostApi extends AbstractApi
{
const PATH_ALL_POSTS = '/api/all';
/**
* @var EntityManager
* @var PostFactory
*/
protected $em;
private $postFactory;
public function __construct(Client $httpClient, $https = true, $baseUrl = null, EntityManagerInterface $entityManager)
public function __construct(Client $httpClient, $https = true, $baseUrl = null, PostFactory $postFactory)
{
parent::__construct($httpClient, $https, $baseUrl);
$this->em = $entityManager;
$this->postFactory = $postFactory;
}
public function getName()
@ -35,106 +34,22 @@ class PostApi extends AbstractApi
}
/**
* Get user subscribers by user id
* Get post with comments by id
*
* @param $id
*
* @param int $id
* @return User[]
*/
public function getUserSubscribersById($id)
public function getPostById($id)
{
if (!is_numeric($id)) {
throw new \InvalidArgumentException('$id must be an integer');
if (!is_string($id)) {
throw new \InvalidArgumentException('$id must be an string');
}
$usersList = $this->getGetRequestData('/api/user/id/' . (int) $id . '/subscribers', [], true);
$postData = $this->getGetRequestData('/api/post/'.$id, [], true);
$users = $this->getUsersFromList($usersList);
$post = $this->postFactory->createFromArray($postData);
return $users;
}
/**
* @param array $users
* @return User[]
*/
private function getUsersFromList(array $users = [])
{
/** @var EntityRepository $userRepo */
$userRepo = $this->em->getRepository('SkobkinPointToolsBundle:User');
$resultUsers = [];
foreach ($users as $userData) {
if (array_key_exists('id', $userData) && array_key_exists('login', $userData) && array_key_exists('name', $userData) && is_numeric($userData['id'])) {
// @todo Optimize with prehashed id's list
$user = $userRepo->findOneBy(['id' => $userData['id']]);
if (!$user) {
$user = new User();
$user->setId((int) $userData['id']);
$this->em->persist($user);
}
// Updating data
if ($user->getLogin() !== $userData['login']) {
$user->setLogin($userData['login']);
}
if ($user->getName() !== $userData['name']) {
$user->setName($userData['name']);
}
$resultUsers[] = $user;
}
}
$this->em->flush();
return $resultUsers;
}
/**
* @param array $posts
*/
private function getPostsFromList(array $posts = [])
{
/** @var EntityRepository $postRepo */
$postRepo = $this->em->getRepository('SkobkinPointToolsBundle:Blogs\Post');
$resultUsers = [];
foreach ($posts as $postData) {
if (array_key_exists('id', $postData) && array_key_exists('uid', $postData) && array_key_exists('post', $postData)) {
// @todo Optimize with prehashed id's list
$post = $postRepo->findOneBy(['id' => $postData['id']]);
if (!$post) {
$post = new Post();
$post
->setId($postData['id'])
->setText($postData['post']['text'])
//->setCreatedAt()
//->setAuthor()
->setType($postData['post']['type'])
;
$this->em->persist($post);
}
// Updating data
if ($post->getLogin() !== $postData['login']) {
$post->setLogin($postData['login']);
}
if ($post->getName() !== $postData['name']) {
$post->setName($postData['name']);
}
$resultUsers[] = $post;
}
}
$this->em->flush();
return $resultUsers;
return $post;
}
}

View File

@ -219,7 +219,7 @@ class UserApi extends AbstractApi
throw new \InvalidArgumentException('$userInfo must be an array');
}
// @todo Return ID existance check when @ap-Codkelden will fix this API behaviour
// @todo Refactor to UserFactory->createFromArray()
if (array_key_exists('id', $userInfo) && array_key_exists('login', $userInfo) && array_key_exists('name', $userInfo) && is_numeric($userInfo['id'])) {
/** @var User $user */
if (null === ($user = $this->userRepository->find($userInfo['id']))) {