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;
|
2015-10-05 00:59:22 +00:00
|
|
|
use Guzzle\Http\Exception\ClientErrorResponseException;
|
2015-05-28 22:47:06 +00:00
|
|
|
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-10-05 00:59:22 +00:00
|
|
|
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\InvalidResponseException;
|
|
|
|
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\UserNotFoundException;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2015-03-23 23:41:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Basic Point.im user API functions from /api/user/*
|
|
|
|
*/
|
|
|
|
class UserApi extends AbstractApi
|
|
|
|
{
|
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-10-05 00:59:22 +00:00
|
|
|
protected $avatarsBaseUrl = '//point.im/avatar/';
|
2015-05-28 22:47:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var EntityManager
|
|
|
|
*/
|
|
|
|
protected $em;
|
|
|
|
|
2015-10-05 01:02:06 +00:00
|
|
|
/**
|
|
|
|
* @var EntityRepository
|
|
|
|
*/
|
|
|
|
protected $userRepository;
|
|
|
|
|
2015-05-28 22:47:06 +00:00
|
|
|
|
|
|
|
public function __construct(Client $httpClient, $https = true, $baseUrl = null, EntityManagerInterface $entityManager)
|
|
|
|
{
|
|
|
|
parent::__construct($httpClient, $https, $baseUrl);
|
|
|
|
|
|
|
|
$this->em = $entityManager;
|
2015-10-05 01:02:06 +00:00
|
|
|
$this->userRepository = $this->em->getRepository('SkobkinPointToolsBundle:User');
|
2015-05-28 22:47:06 +00:00
|
|
|
}
|
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[]
|
2015-10-05 00:59:22 +00:00
|
|
|
* @throws ApiException
|
|
|
|
* @throws InvalidResponseException
|
|
|
|
* @throws UserNotFoundException
|
2015-03-23 23:41:41 +00:00
|
|
|
*/
|
|
|
|
public function getUserSubscribersByLogin($login)
|
|
|
|
{
|
2015-10-05 00:59:22 +00:00
|
|
|
try {
|
|
|
|
$usersList = $this->getGetRequestData('/api/user/'.$login.'/subscribers', [], true);
|
|
|
|
} catch (ClientErrorResponseException $e) {
|
|
|
|
if (Response::HTTP_NOT_FOUND === $e->getResponse()->getStatusCode()) {
|
|
|
|
throw new UserNotFoundException('User not found', 0, $e, null, $login);
|
|
|
|
} else {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
2015-05-28 22:47:06 +00:00
|
|
|
|
2015-10-05 00:59:22 +00:00
|
|
|
return $this->getUsersFromList($usersList);
|
2015-05-28 22:47:06 +00:00
|
|
|
}
|
|
|
|
|
2015-05-31 21:19:03 +00:00
|
|
|
/**
|
|
|
|
* Get user subscribers by user id
|
|
|
|
*
|
2015-10-05 00:59:22 +00:00
|
|
|
* @param $id
|
2015-05-31 21:19:03 +00:00
|
|
|
* @return User[]
|
2015-10-05 00:59:22 +00:00
|
|
|
* @throws ApiException
|
|
|
|
* @throws InvalidResponseException
|
|
|
|
* @throws UserNotFoundException
|
2015-05-31 21:19:03 +00:00
|
|
|
*/
|
|
|
|
public function getUserSubscribersById($id)
|
|
|
|
{
|
|
|
|
if (!is_numeric($id)) {
|
|
|
|
throw new \InvalidArgumentException('$id must be an integer');
|
|
|
|
}
|
|
|
|
|
2015-10-05 00:59:22 +00:00
|
|
|
try {
|
|
|
|
$usersList = $this->getGetRequestData('/api/user/id/'.(int) $id.'/subscribers', [], true);
|
|
|
|
} catch (ClientErrorResponseException $e) {
|
|
|
|
if (Response::HTTP_NOT_FOUND === $e->getResponse()->getStatusCode()) {
|
|
|
|
throw new UserNotFoundException('User not found', 0, $e, $id);
|
|
|
|
} else {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
2015-05-31 21:19:03 +00:00
|
|
|
|
2015-10-05 00:59:22 +00:00
|
|
|
return $this->getUsersFromList($usersList);
|
2015-05-31 21:19:03 +00:00
|
|
|
}
|
|
|
|
|
2015-08-08 09:22:35 +00:00
|
|
|
/**
|
|
|
|
* Get user subscriptions by user login
|
|
|
|
*
|
|
|
|
* @param string $login
|
|
|
|
* @return User[]
|
2015-10-05 00:59:22 +00:00
|
|
|
* @throws ApiException
|
|
|
|
* @throws InvalidResponseException
|
|
|
|
* @throws UserNotFoundException
|
2015-08-08 09:22:35 +00:00
|
|
|
*/
|
|
|
|
public function getUserSubscriptionsByLogin($login)
|
|
|
|
{
|
2015-10-05 00:59:22 +00:00
|
|
|
try {
|
|
|
|
$usersList = $this->getGetRequestData('/api/user/'.$login.'/subscriptions', [], true);
|
|
|
|
} catch (ClientErrorResponseException $e) {
|
|
|
|
if (Response::HTTP_NOT_FOUND === $e->getResponse()->getStatusCode()) {
|
|
|
|
throw new UserNotFoundException('User not found', 0, $e, null, $login);
|
|
|
|
} else {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
2015-08-08 09:22:35 +00:00
|
|
|
|
2015-10-05 00:59:22 +00:00
|
|
|
return $this->getUsersFromList($usersList);
|
2015-08-08 09:22:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get user subscriptions by user id
|
|
|
|
*
|
2015-10-05 00:59:22 +00:00
|
|
|
* @param $id
|
2015-08-08 09:22:35 +00:00
|
|
|
* @return User[]
|
2015-10-05 00:59:22 +00:00
|
|
|
* @throws ApiException
|
|
|
|
* @throws InvalidResponseException
|
|
|
|
* @throws UserNotFoundException
|
2015-08-08 09:22:35 +00:00
|
|
|
*/
|
|
|
|
public function getUserSubscriptionsById($id)
|
|
|
|
{
|
|
|
|
if (!is_numeric($id)) {
|
|
|
|
throw new \InvalidArgumentException('$id must be an integer');
|
|
|
|
}
|
|
|
|
|
2015-10-05 00:59:22 +00:00
|
|
|
try {
|
|
|
|
$usersList = $this->getGetRequestData('/api/user/id/'.(int) $id.'/subscriptions', [], true);
|
|
|
|
} catch (ClientErrorResponseException $e) {
|
|
|
|
if (Response::HTTP_NOT_FOUND === $e->getResponse()->getStatusCode()) {
|
|
|
|
throw new UserNotFoundException('User not found', 0, $e, $id);
|
|
|
|
} else {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
2015-08-08 09:22:35 +00:00
|
|
|
|
2015-10-05 00:59:22 +00:00
|
|
|
return $this->getUsersFromList($usersList);
|
|
|
|
}
|
2015-08-08 09:22:35 +00:00
|
|
|
|
2015-10-05 00:59:22 +00:00
|
|
|
/**
|
|
|
|
* Get user by id
|
|
|
|
*
|
|
|
|
* @param $id
|
|
|
|
* @return User
|
|
|
|
* @throws UserNotFoundException
|
|
|
|
* @throws ClientErrorResponseException
|
|
|
|
*/
|
|
|
|
public function getUserById($id)
|
|
|
|
{
|
|
|
|
if (!is_numeric($id)) {
|
|
|
|
throw new \InvalidArgumentException('$id must be an integer');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$userInfo = $this->getGetRequestData('/api/user/id/'.(int) $id, [], true);
|
|
|
|
} catch (ClientErrorResponseException $e) {
|
|
|
|
if (Response::HTTP_NOT_FOUND === $e->getResponse()->getStatusCode()) {
|
|
|
|
throw new UserNotFoundException('User not found', 0, $e, $id);
|
|
|
|
} else {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getUserFromUserInfo($userInfo);
|
2015-08-08 09:22:35 +00:00
|
|
|
}
|
|
|
|
|
2015-05-28 22:47:06 +00:00
|
|
|
/**
|
2015-10-05 00:59:22 +00:00
|
|
|
* Finds and updates or create new user from API response data
|
|
|
|
*
|
|
|
|
* @param array $userInfo
|
|
|
|
* @return User
|
|
|
|
* @throws ApiException
|
|
|
|
* @throws InvalidResponseException
|
|
|
|
*/
|
|
|
|
public function getUserFromUserInfo(array $userInfo)
|
|
|
|
{
|
|
|
|
if (!is_array($userInfo)) {
|
|
|
|
throw new \InvalidArgumentException('$userInfo must be an array');
|
|
|
|
}
|
|
|
|
|
|
|
|
// @todo Return ID existance check when @ap-Codkelden will fix this API behaviour
|
|
|
|
if (array_key_exists('id', $userInfo) && array_key_exists('login', $userInfo) && array_key_exists('name', $userInfo) && is_numeric($userInfo['id'])) {
|
|
|
|
/** @var User $user */
|
2015-10-05 01:02:06 +00:00
|
|
|
if (null === ($user = $this->userRepository->find($userInfo['id']))) {
|
2015-10-05 00:59:22 +00:00
|
|
|
// Creating new user
|
|
|
|
$user = new User($userInfo['id']);
|
|
|
|
$this->em->persist($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updating data
|
|
|
|
$user
|
|
|
|
->setLogin($userInfo['login'])
|
|
|
|
->setName($userInfo['name'])
|
|
|
|
;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->em->flush($user);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw new ApiException(sprintf('Error while flushing changes for [%d] %s: %s', $user->getId(), $user->getLogin(), $e->getMessage()), 0, $e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new InvalidResponseException('Invalid API response. Mandatory fields do not exist.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get array of User objects from API response containing user list
|
|
|
|
*
|
|
|
|
* @param array $users
|
2015-05-28 22:47:06 +00:00
|
|
|
* @return User[]
|
2015-10-05 00:59:22 +00:00
|
|
|
* @throws ApiException
|
|
|
|
* @throws InvalidResponseException
|
2015-05-28 22:47:06 +00:00
|
|
|
*/
|
|
|
|
private function getUsersFromList(array $users = [])
|
|
|
|
{
|
|
|
|
if (!is_array($users)) {
|
|
|
|
throw new \InvalidArgumentException('$users must be an array');
|
|
|
|
}
|
|
|
|
|
2015-10-05 00:59:22 +00:00
|
|
|
/** @var User[] $resultUsers */
|
2015-05-28 22:47:06 +00:00
|
|
|
$resultUsers = [];
|
2015-03-27 19:49:21 +00:00
|
|
|
|
2015-10-05 00:59:22 +00:00
|
|
|
foreach ($users as $userInfo) {
|
|
|
|
if (array_key_exists('id', $userInfo) && array_key_exists('login', $userInfo) && array_key_exists('name', $userInfo) && is_numeric($userInfo['id'])) {
|
2015-03-27 19:49:21 +00:00
|
|
|
|
2015-05-28 22:47:06 +00:00
|
|
|
// @todo Optimize with prehashed id's list
|
2015-10-05 01:02:06 +00:00
|
|
|
if (null === ($user = $this->userRepository->find($userInfo['id']))) {
|
2015-10-05 00:59:22 +00:00
|
|
|
$user = new User((int) $userInfo['id']);
|
2015-05-28 22:47:06 +00:00
|
|
|
$this->em->persist($user);
|
|
|
|
}
|
2015-03-27 19:49:21 +00:00
|
|
|
|
2015-05-28 22:47:06 +00:00
|
|
|
// Updating data
|
2015-10-05 00:59:22 +00:00
|
|
|
$user
|
|
|
|
->setLogin($userInfo['login'])
|
|
|
|
->setName($userInfo['name'])
|
|
|
|
;
|
2015-03-27 19:49:21 +00:00
|
|
|
|
2015-10-01 18:05:11 +00:00
|
|
|
try {
|
2015-10-05 00:59:22 +00:00
|
|
|
$this->em->flush($user);
|
2015-10-01 18:05:11 +00:00
|
|
|
} catch (\Exception $e) {
|
2015-10-01 19:14:30 +00:00
|
|
|
throw new ApiException(sprintf('Error while flushing changes for [%d] %s: %s', $user->getId(), $user->getLogin(), $e->getMessage()), 0, $e);
|
2015-10-01 18:05:11 +00:00
|
|
|
}
|
|
|
|
|
2015-05-28 22:47:06 +00:00
|
|
|
$resultUsers[] = $user;
|
2015-10-05 00:59:22 +00:00
|
|
|
} else {
|
|
|
|
throw new InvalidResponseException('Invalid API response. Mandatory fields do not exist.');
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-05 00:59:22 +00:00
|
|
|
* Creates avatar with specified size URL for user
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
* @param int $size
|
|
|
|
* @return string
|
2015-05-28 22:47:06 +00:00
|
|
|
*/
|
|
|
|
public function getAvatarUrl(User $user, $size)
|
|
|
|
{
|
2015-10-05 00:59:22 +00:00
|
|
|
if (!in_array($size, [self::AVATAR_SIZE_SMALL, self::AVATAR_SIZE_MEDIUM, self::AVATAR_SIZE_LARGE], true)) {
|
|
|
|
throw new \InvalidArgumentException('Avatar size must be one of restricted variants. See UserApi class AVATAR_SIZE_* constants.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->avatarsBaseUrl.$user->getLogin().'/'.$size;
|
2015-03-23 23:41:41 +00:00
|
|
|
}
|
|
|
|
}
|