|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Service\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
|
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
7
|
|
|
use Doctrine\ORM\EntityRepository; |
|
8
|
|
|
use Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler\User as UserDTO; |
|
9
|
|
|
use Skobkin\Bundle\PointToolsBundle\Entity\User; |
|
10
|
|
|
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\ApiException; |
|
11
|
|
|
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\Factory\InvalidUserDataException; |
|
12
|
|
|
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\InvalidResponseException; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
class UserFactory |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var EntityManager |
|
19
|
|
|
*/ |
|
20
|
|
|
private $em; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var EntityRepository |
|
24
|
|
|
*/ |
|
25
|
|
|
private $userRepository; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param EntityManager $em |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(EntityManager $em) |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
|
|
$this->em = $em; |
|
33
|
|
|
$this->userRepository = $em->getRepository('SkobkinPointToolsBundle:User'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param array $data |
|
38
|
|
|
* |
|
39
|
|
|
* @return User |
|
40
|
|
|
* @throws ApiException |
|
41
|
|
|
* @throws InvalidResponseException |
|
42
|
|
|
*/ |
|
43
|
|
|
public function createFromArray(array $data) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->validateArrayData($data); |
|
46
|
|
|
|
|
47
|
|
|
/** @var User $user */ |
|
48
|
|
View Code Duplication |
if (null === ($user = $this->userRepository->find($data['id']))) { |
|
|
|
|
|
|
49
|
|
|
// Creating new user |
|
50
|
|
|
$user = new User($data['id']); |
|
51
|
|
|
$this->em->persist($user); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// Updating data |
|
55
|
|
|
$user |
|
56
|
|
|
->setLogin($data['login']) |
|
57
|
|
|
->setName($data['name']) |
|
58
|
|
|
; |
|
59
|
|
|
|
|
60
|
|
|
return $user; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param UserDTO $userData |
|
65
|
|
|
* |
|
66
|
|
|
* @return User |
|
67
|
|
|
* @throws ApiException |
|
68
|
|
|
* @throws InvalidUserDataException |
|
69
|
|
|
*/ |
|
70
|
|
|
public function createFromDTO(UserDTO $userData) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->validateDTOData($userData); |
|
73
|
|
|
|
|
74
|
|
|
/** @var User $user */ |
|
75
|
|
|
if (null === ($user = $this->userRepository->find($userData->getId()))) { |
|
76
|
|
|
// Creating new user |
|
77
|
|
|
$user = new User($userData->getId()); |
|
78
|
|
|
$this->em->persist($user); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// Updating data |
|
82
|
|
|
$user |
|
83
|
|
|
->setLogin($userData->getLogin()) |
|
84
|
|
|
->setName($userData->getName()) |
|
85
|
|
|
; |
|
86
|
|
|
|
|
87
|
|
|
return $user; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param array $data |
|
92
|
|
|
* |
|
93
|
|
|
* @throws InvalidResponseException |
|
94
|
|
|
*/ |
|
95
|
|
|
private function validateArrayData(array $data) |
|
96
|
|
|
{ |
|
97
|
|
|
if (!(array_key_exists('id', $data) || !!array_key_exists('login', $data) || !array_key_exists('name', $data))) { |
|
98
|
|
|
throw new InvalidResponseException('Invalid user data'); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param array $data |
|
104
|
|
|
* |
|
105
|
|
|
* @throws InvalidResponseException |
|
106
|
|
|
*/ |
|
107
|
|
|
private function validateDTOData(UserDTO $data) |
|
108
|
|
|
{ |
|
109
|
|
|
if (!$data->getId() || !$data->getLogin()) { |
|
110
|
|
|
throw new InvalidUserDataException('User have no id or login', $data); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
The
EntityManagermight become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManageris closed. Any other code which depends on the same instance of theEntityManagerduring this request will fail.On the other hand, if you instead inject the
ManagerRegistry, thegetManager()method guarantees that you will always get a usable manager instance.