2015-03-23 23:41:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Service;
|
|
|
|
|
2015-05-28 22:47:06 +00:00
|
|
|
use Doctrine\ORM\EntityManager;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use Doctrine\ORM\EntityRepository;
|
|
|
|
use Guzzle\Service\Client;
|
2015-03-23 23:41:41 +00:00
|
|
|
use Skobkin\Bundle\PointToolsBundle\Entity\User;
|
2015-10-01 18:05:11 +00:00
|
|
|
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\ApiException;
|
2015-03-23 23:41:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Basic Point.im user API functions from /api/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';
|
2015-03-27 19:49:21 +00:00
|
|
|
|
2015-05-28 22:47:06 +00:00
|
|
|
const AVATAR_SIZE_SMALL = '24';
|
|
|
|
const AVATAR_SIZE_MEDIUM = '40';
|
|
|
|
const AVATAR_SIZE_LARGE = '80';
|
|
|
|
|
2015-03-23 23:41:41 +00:00
|
|
|
/**
|
|
|
|
* @var string Base URL for user avatars
|
|
|
|
*/
|
2015-05-28 22:47:06 +00:00
|
|
|
protected $avatarsBaseUrl = 'point.im/avatar/';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var EntityManager
|
|
|
|
*/
|
|
|
|
protected $em;
|
|
|
|
|
|
|
|
|
|
|
|
public function __construct(Client $httpClient, $https = true, $baseUrl = null, EntityManagerInterface $entityManager)
|
|
|
|
{
|
|
|
|
parent::__construct($httpClient, $https, $baseUrl);
|
|
|
|
|
|
|
|
$this->em = $entityManager;
|
|
|
|
}
|
2015-03-27 19:49:21 +00:00
|
|
|
|
2015-03-23 23:41:41 +00:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return 'skobkin_point_tools_api_user';
|
|
|
|
}
|
2015-03-27 19:49:21 +00:00
|
|
|
|
2015-03-23 23:41:41 +00:00
|
|
|
/**
|
2015-05-28 22:47:06 +00:00
|
|
|
* Get user subscribers by user login
|
2015-03-27 19:49:21 +00:00
|
|
|
*
|
2015-03-23 23:41:41 +00:00
|
|
|
* @param string $login
|
|
|
|
* @return User[]
|
|
|
|
*/
|
|
|
|
public function getUserSubscribersByLogin($login)
|
|
|
|
{
|
2015-05-28 22:47:06 +00:00
|
|
|
$usersList = $this->getGetRequestData('/api/user/' . $login . '/subscribers', [], true);
|
|
|
|
|
|
|
|
$users = $this->getUsersFromList($usersList);
|
|
|
|
|
|
|
|
return $users;
|
|
|
|
}
|
|
|
|
|
2015-05-31 21:19:03 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2015-08-08 09:22:35 +00:00
|
|
|
/**
|
|
|
|
* Get user subscriptions by user login
|
|
|
|
*
|
|
|
|
* @param string $login
|
|
|
|
* @return User[]
|
|
|
|
*/
|
|
|
|
public function getUserSubscriptionsByLogin($login)
|
|
|
|
{
|
|
|
|
$usersList = $this->getGetRequestData('/api/user/' . $login . '/subscriptions', [], true);
|
|
|
|
|
|
|
|
$users = $this->getUsersFromList($usersList);
|
|
|
|
|
|
|
|
return $users;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get user subscriptions by user id
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return User[]
|
|
|
|
*/
|
|
|
|
public function getUserSubscriptionsById($id)
|
|
|
|
{
|
|
|
|
if (!is_numeric($id)) {
|
|
|
|
throw new \InvalidArgumentException('$id must be an integer');
|
|
|
|
}
|
|
|
|
|
|
|
|
$usersList = $this->getGetRequestData('/api/user/id/' . (int) $id . '/subscriptions', [], true);
|
|
|
|
|
|
|
|
$users = $this->getUsersFromList($usersList);
|
|
|
|
|
|
|
|
return $users;
|
|
|
|
}
|
|
|
|
|
2015-05-28 22:47:06 +00:00
|
|
|
/**
|
|
|
|
* @return User[]
|
|
|
|
*/
|
|
|
|
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');
|
|
|
|
|
|
|
|
$resultUsers = [];
|
2015-03-27 19:49:21 +00:00
|
|
|
|
2015-05-28 22:47:06 +00:00
|
|
|
foreach ($users as $userData) {
|
|
|
|
if (array_key_exists('id', $userData) && array_key_exists('login', $userData) && array_key_exists('name', $userData) && is_numeric($userData['id'])) {
|
2015-03-27 19:49:21 +00:00
|
|
|
|
2015-05-28 22:47:06 +00:00
|
|
|
// @todo Optimize with prehashed id's list
|
|
|
|
$user = $userRepo->findOneBy(['id' => $userData['id']]);
|
2015-03-27 19:49:21 +00:00
|
|
|
|
2015-05-28 22:47:06 +00:00
|
|
|
if (!$user) {
|
|
|
|
$user = new User();
|
|
|
|
$user->setId((int) $userData['id']);
|
|
|
|
$this->em->persist($user);
|
2015-10-01 18:05:11 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$this->em->flush();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw new ApiException(sprintf('Error while flushing new user [%d] %s', $user->getId(), $user->getLogin()), 0, $e);
|
|
|
|
}
|
2015-05-28 22:47:06 +00:00
|
|
|
}
|
2015-03-27 19:49:21 +00:00
|
|
|
|
2015-05-28 22:47:06 +00:00
|
|
|
// Updating data
|
|
|
|
if ($user->getLogin() !== $userData['login']) {
|
|
|
|
$user->setLogin($userData['login']);
|
|
|
|
}
|
|
|
|
if ($user->getName() !== $userData['name']) {
|
|
|
|
$user->setName($userData['name']);
|
|
|
|
}
|
2015-03-27 19:49:21 +00:00
|
|
|
|
2015-10-01 18:05:11 +00:00
|
|
|
try {
|
|
|
|
$this->em->flush();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw new ApiException(sprintf('Error while flushing changes for [%d] %s', $user->getId(), $user->getLogin()), 0, $e);
|
|
|
|
}
|
|
|
|
|
2015-05-28 22:47:06 +00:00
|
|
|
$resultUsers[] = $user;
|
2015-03-23 23:41:41 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-27 19:49:21 +00:00
|
|
|
|
2015-05-28 22:47:06 +00:00
|
|
|
return $resultUsers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $login
|
|
|
|
*/
|
|
|
|
public function getAvatarUrl(User $user, $size)
|
|
|
|
{
|
|
|
|
return ($this->useHttps ? 'https://' : 'http://') . $this->avatarsBaseUrl . $user->getLogin() . '/' . $size;
|
2015-03-23 23:41:41 +00:00
|
|
|
}
|
|
|
|
}
|