Post requests test implementation.
This commit is contained in:
parent
659a930037
commit
a3b1f60c6f
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
namespace Skobkin\Bundle\PointToolsBundle\Command;
|
||||
|
||||
use Skobkin\Bundle\PointToolsBundle\Service\SubscriptionsManager;
|
||||
use Skobkin\Bundle\PointToolsBundle\Service\UserApi;
|
||||
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
|
||||
use Symfony\Component\Console\Input\Input;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\Output;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class UpdatePostsCommand extends ContainerAwareCommand
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setName('point:update:posts')
|
||||
->setDescription('Update posts from /all')
|
||||
->addOption(
|
||||
'check-only',
|
||||
null,
|
||||
InputOption::VALUE_NONE,
|
||||
'If set, command will not perform write operations in the database'
|
||||
)
|
||||
// @todo add option for checking only selected user
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Input $input
|
||||
* @param Output $output
|
||||
* @return bool
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$log = $this->getContainer()->get('logger');
|
||||
|
||||
$log->info('UpdateSubscriptionsCommand started.');
|
||||
|
||||
/** @var UserApi $api */
|
||||
$api = $this->getContainer()->get('skobkin_point_tools.api_user');
|
||||
|
||||
|
||||
}
|
||||
}
|
140
src/Skobkin/Bundle/PointToolsBundle/Service/PostApi.php
Normal file
140
src/Skobkin/Bundle/PointToolsBundle/Service/PostApi.php
Normal file
|
@ -0,0 +1,140 @@
|
|||
<?php
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* Basic Point.im user API functions from /api/user/*
|
||||
*/
|
||||
class PostApi extends AbstractApi
|
||||
{
|
||||
const PATH_ALL_POSTS = '/api/all';
|
||||
|
||||
/**
|
||||
* @var EntityManager
|
||||
*/
|
||||
protected $em;
|
||||
|
||||
|
||||
public function __construct(Client $httpClient, $https = true, $baseUrl = null, EntityManagerInterface $entityManager)
|
||||
{
|
||||
parent::__construct($httpClient, $https, $baseUrl);
|
||||
|
||||
$this->em = $entityManager;
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return 'skobkin_point_tools_api_post';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user subscribers by user id
|
||||
*
|
||||
* @param int $id
|
||||
* @return User[]
|
||||
*/
|
||||
public function getUserSubscribersById($id)
|
||||
{
|
||||
if (!is_numeric($id)) {
|
||||
throw new \InvalidArgumentException('$id must be an integer');
|
||||
}
|
||||
|
||||
$usersList = $this->getGetRequestData('/api/user/id/' . (int) $id . '/subscribers', [], true);
|
||||
|
||||
$users = $this->getUsersFromList($usersList);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -13,10 +13,6 @@ use Skobkin\Bundle\PointToolsBundle\Entity\User;
|
|||
*/
|
||||
class UserApi extends AbstractApi
|
||||
{
|
||||
const PATH_USER_INFO = '/api/user/%s';
|
||||
const PATH_USER_SUBSCRIPTIONS = '/api/user/%s/subscriptions';
|
||||
const PATH_USER_SUBSCRIBERS = '/api/user/%s/subscribers';
|
||||
|
||||
const AVATAR_SIZE_SMALL = '24';
|
||||
const AVATAR_SIZE_MEDIUM = '40';
|
||||
const AVATAR_SIZE_LARGE = '80';
|
||||
|
@ -83,10 +79,6 @@ class UserApi extends AbstractApi
|
|||
*/
|
||||
private function getUsersFromList(array $users = [])
|
||||
{
|
||||
if (!is_array($users)) {
|
||||
throw new \InvalidArgumentException('$users must be an array');
|
||||
}
|
||||
|
||||
/** @var EntityRepository $userRepo */
|
||||
$userRepo = $this->em->getRepository('SkobkinPointToolsBundle:User');
|
||||
|
||||
|
@ -96,7 +88,7 @@ class UserApi extends AbstractApi
|
|||
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']]);
|
||||
$user = $userRepo->find($userData['id']);
|
||||
|
||||
if (!$user) {
|
||||
$user = new User();
|
||||
|
|
Loading…
Reference in a new issue