|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
|
6
|
|
|
use Doctrine\ORM\EntityRepository; |
|
7
|
|
|
use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Comment; |
|
8
|
|
|
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\ApiException; |
|
9
|
|
|
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\InvalidResponseException; |
|
10
|
|
|
use Skobkin\Bundle\PointToolsBundle\Service\Factory\UserFactory; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class CommentFactory |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var EntityManager |
|
17
|
|
|
*/ |
|
18
|
|
|
private $em; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var EntityRepository |
|
22
|
|
|
*/ |
|
23
|
|
|
private $commentRepository; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var EntityRepository |
|
27
|
|
|
*/ |
|
28
|
|
|
private $postRepository; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var UserFactory |
|
32
|
|
|
*/ |
|
33
|
|
|
private $userFactory; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param EntityManager $em |
|
37
|
|
|
* @param UserFactory $userFactory |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(EntityManager $em, UserFactory $userFactory) |
|
|
|
|
|
|
40
|
|
|
{ |
|
41
|
|
|
$this->em = $em; |
|
42
|
|
|
$this->userFactory = $userFactory; |
|
43
|
|
|
$this->commentRepository = $em->getRepository('SkobkinPointToolsBundle:Blogs\Comment'); |
|
44
|
|
|
$this->postRepository = $em->getRepository('SkobkinPointToolsBundle:Blogs\Post'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param array $data |
|
49
|
|
|
* |
|
50
|
|
|
* @return Comment |
|
51
|
|
|
* @throws ApiException |
|
52
|
|
|
* @throws InvalidResponseException |
|
53
|
|
|
*/ |
|
54
|
|
|
public function createFromArray(array $data) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->validateData($data); |
|
57
|
|
|
|
|
58
|
|
|
if (null === ($comment = $this->commentRepository->find(['post' => $data['post_id'], 'id' => $data['id']]))) { |
|
59
|
|
|
// @fixme rare non-existing post bug |
|
60
|
|
|
$post = $this->postRepository->find($data['post_id']); |
|
61
|
|
|
$author = $this->userFactory->createFromArray($data['author']); |
|
62
|
|
|
if (null !== $data['to_comment_id']) { |
|
63
|
|
|
$toComment = $this->commentRepository->find(['post' => $data['post_id'], 'id' => $data['to_comment_id']]); |
|
64
|
|
|
} else { |
|
65
|
|
|
$toComment = null; |
|
66
|
|
|
} |
|
67
|
|
|
$createdAt = new \DateTime($data['created']); |
|
68
|
|
|
|
|
69
|
|
|
$comment = new Comment($data['id'], $post, $author, $toComment, $data['text'], $createdAt, $data['is_rec']); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
$this->em->persist($comment); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $comment; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param array $data |
|
79
|
|
|
* |
|
80
|
|
|
* @return Comment[] |
|
81
|
|
|
* @throws ApiException |
|
82
|
|
|
*/ |
|
83
|
|
|
public function createFromListArray(array $data) |
|
84
|
|
|
{ |
|
85
|
|
|
$comments = []; |
|
86
|
|
|
|
|
87
|
|
|
foreach ($data as $commentData) { |
|
88
|
|
|
$comments[] = $this->createFromArray($commentData); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $comments; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param array $data |
|
96
|
|
|
* |
|
97
|
|
|
* @throws InvalidResponseException |
|
98
|
|
|
*/ |
|
99
|
|
|
private function validateData(array $data) |
|
100
|
|
|
{ |
|
101
|
|
|
if (!array_key_exists('author', $data)) { |
|
102
|
|
|
throw new InvalidResponseException('Comment author data not found in API response'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
// Post |
|
106
|
|
|
if (!( |
|
107
|
|
|
array_key_exists('id', $data) && |
|
108
|
|
|
array_key_exists('is_rec', $data) && |
|
109
|
|
|
array_key_exists('to_comment_id', $data) && |
|
110
|
|
|
array_key_exists('post_id', $data) && |
|
111
|
|
|
array_key_exists('text', $data) && |
|
112
|
|
|
array_key_exists('created', $data) |
|
113
|
|
|
)) { |
|
114
|
|
|
throw new InvalidResponseException('Comment data not found in API response'); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
} |
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.