2015-10-26 02:57:53 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Service\Factory;
|
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use Doctrine\ORM\EntityRepository;
|
2016-03-23 19:38:44 +00:00
|
|
|
use Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler\User as UserDTO;
|
2015-10-26 02:57:53 +00:00
|
|
|
use Skobkin\Bundle\PointToolsBundle\Entity\User;
|
|
|
|
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\ApiException;
|
2016-03-23 19:38:44 +00:00
|
|
|
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\Factory\InvalidUserDataException;
|
2015-10-26 02:57:53 +00:00
|
|
|
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\InvalidResponseException;
|
|
|
|
|
|
|
|
|
|
|
|
class UserFactory
|
|
|
|
{
|
|
|
|
/**
|
2017-01-08 23:26:06 +00:00
|
|
|
* @var EntityManagerInterface
|
2015-10-26 02:57:53 +00:00
|
|
|
*/
|
|
|
|
private $em;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var EntityRepository
|
|
|
|
*/
|
|
|
|
private $userRepository;
|
|
|
|
|
|
|
|
/**
|
2017-01-08 23:26:06 +00:00
|
|
|
* @param EntityManagerInterface $em
|
2015-10-26 02:57:53 +00:00
|
|
|
*/
|
2017-01-08 23:26:06 +00:00
|
|
|
public function __construct(EntityManagerInterface $em)
|
2015-10-26 02:57:53 +00:00
|
|
|
{
|
|
|
|
$this->em = $em;
|
|
|
|
$this->userRepository = $em->getRepository('SkobkinPointToolsBundle:User');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return User
|
2017-01-08 23:26:06 +00:00
|
|
|
*
|
2015-10-26 02:57:53 +00:00
|
|
|
* @throws ApiException
|
|
|
|
* @throws InvalidResponseException
|
|
|
|
*/
|
2017-01-08 23:26:06 +00:00
|
|
|
public function createFromArray(array $data): User
|
2015-10-26 02:57:53 +00:00
|
|
|
{
|
2016-03-23 19:38:44 +00:00
|
|
|
$this->validateArrayData($data);
|
2015-10-26 02:57:53 +00:00
|
|
|
|
|
|
|
/** @var User $user */
|
|
|
|
if (null === ($user = $this->userRepository->find($data['id']))) {
|
|
|
|
// Creating new user
|
|
|
|
$user = new User($data['id']);
|
|
|
|
$this->em->persist($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updating data
|
|
|
|
$user
|
|
|
|
->setLogin($data['login'])
|
|
|
|
->setName($data['name'])
|
|
|
|
;
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2016-03-23 19:38:44 +00:00
|
|
|
/**
|
|
|
|
* @param UserDTO $userData
|
|
|
|
*
|
|
|
|
* @return User
|
2017-01-08 23:26:06 +00:00
|
|
|
*
|
2016-03-23 19:38:44 +00:00
|
|
|
* @throws ApiException
|
|
|
|
* @throws InvalidUserDataException
|
|
|
|
*/
|
2017-01-08 23:26:06 +00:00
|
|
|
public function createFromDTO(UserDTO $userData): User
|
2016-03-23 19:38:44 +00:00
|
|
|
{
|
|
|
|
$this->validateDTOData($userData);
|
|
|
|
|
|
|
|
/** @var User $user */
|
|
|
|
if (null === ($user = $this->userRepository->find($userData->getId()))) {
|
|
|
|
// Creating new user
|
|
|
|
$user = new User($userData->getId());
|
|
|
|
$this->em->persist($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updating data
|
|
|
|
$user
|
|
|
|
->setLogin($userData->getLogin())
|
|
|
|
->setName($userData->getName())
|
|
|
|
;
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2015-10-26 02:57:53 +00:00
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @throws InvalidResponseException
|
|
|
|
*/
|
2016-03-23 19:38:44 +00:00
|
|
|
private function validateArrayData(array $data)
|
2015-10-26 02:57:53 +00:00
|
|
|
{
|
2016-03-23 19:38:44 +00:00
|
|
|
if (!(array_key_exists('id', $data) || !!array_key_exists('login', $data) || !array_key_exists('name', $data))) {
|
2015-10-26 02:57:53 +00:00
|
|
|
throw new InvalidResponseException('Invalid user data');
|
|
|
|
}
|
|
|
|
}
|
2016-03-23 19:38:44 +00:00
|
|
|
|
|
|
|
/**
|
2017-01-08 23:26:06 +00:00
|
|
|
* @param UserDTO $data
|
2016-03-23 19:38:44 +00:00
|
|
|
*
|
|
|
|
* @throws InvalidResponseException
|
|
|
|
*/
|
|
|
|
private function validateDTOData(UserDTO $data)
|
|
|
|
{
|
|
|
|
if (!$data->getId() || !$data->getLogin()) {
|
|
|
|
throw new InvalidUserDataException('User have no id or login', $data);
|
|
|
|
}
|
|
|
|
}
|
2015-10-26 02:57:53 +00:00
|
|
|
}
|