|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Sulu. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Sulu GmbH |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sulu\Bundle\CommentBundle\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use FOS\RestBundle\Controller\Annotations\Post; |
|
15
|
|
|
use FOS\RestBundle\Routing\ClassResourceInterface; |
|
16
|
|
|
use Sulu\Bundle\CommentBundle\Entity\CommentInterface; |
|
17
|
|
|
use Sulu\Bundle\CommentBundle\Entity\CommentRepositoryInterface; |
|
18
|
|
|
use Sulu\Component\Rest\Exception\EntityNotFoundException; |
|
19
|
|
|
use Sulu\Component\Rest\Exception\RestException; |
|
20
|
|
|
use Sulu\Component\Rest\ListBuilder\FieldDescriptorInterface; |
|
21
|
|
|
use Sulu\Component\Rest\ListBuilder\ListRepresentation; |
|
22
|
|
|
use Sulu\Component\Rest\RequestParametersTrait; |
|
23
|
|
|
use Sulu\Component\Rest\RestController; |
|
24
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
25
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Provides an api for comments. |
|
29
|
|
|
*/ |
|
30
|
|
|
class CommentController extends RestController implements ClassResourceInterface |
|
31
|
|
|
{ |
|
32
|
|
|
use RequestParametersTrait; |
|
33
|
|
|
|
|
34
|
|
|
public function cgetAction(Request $request): Response |
|
35
|
|
|
{ |
|
36
|
|
|
$restHelper = $this->get('sulu_core.doctrine_rest_helper'); |
|
37
|
|
|
$factory = $this->get('sulu_core.doctrine_list_builder_factory'); |
|
38
|
|
|
$listBuilder = $factory->create($this->getParameter('sulu.model.comment.class')); |
|
39
|
|
|
|
|
40
|
|
|
/** @var FieldDescriptorInterface[] $fieldDescriptors */ |
|
41
|
|
|
$fieldDescriptors = $this->get('sulu_core.list_builder.field_descriptor_factory') |
|
42
|
|
|
->getFieldDescriptors('comments'); |
|
43
|
|
|
$restHelper->initializeListBuilder($listBuilder, $fieldDescriptors); |
|
44
|
|
|
|
|
45
|
|
|
if ($request->query->get('threadType')) { |
|
46
|
|
|
$listBuilder->in( |
|
47
|
|
|
$fieldDescriptors['threadType'], |
|
48
|
|
|
array_filter(explode(',', $request->query->get('threadType'))) |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
$request->query->remove('threadType'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
3 |
View Code Duplication |
foreach ($request->query->all() as $filterKey => $filterValue) { |
|
|
|
|
|
|
55
|
|
|
if (isset($fieldDescriptors[$filterKey])) { |
|
56
|
3 |
|
$listBuilder->where($fieldDescriptors[$filterKey], $filterValue); |
|
57
|
3 |
|
} |
|
58
|
3 |
|
} |
|
59
|
|
|
|
|
60
|
3 |
|
$results = $listBuilder->execute(); |
|
61
|
3 |
|
$list = new ListRepresentation( |
|
62
|
|
|
$results, |
|
63
|
3 |
|
'comments', |
|
64
|
1 |
|
$request->attributes->get('_route'), |
|
65
|
1 |
|
$request->query->all(), |
|
66
|
1 |
|
$listBuilder->getCurrentPage(), |
|
67
|
|
|
$listBuilder->getLimit(), |
|
68
|
|
|
$listBuilder->count() |
|
69
|
1 |
|
); |
|
70
|
|
|
|
|
71
|
|
|
return $this->handleView($this->view($list, 200)); |
|
72
|
3 |
|
} |
|
73
|
1 |
|
|
|
74
|
1 |
View Code Duplication |
public function getAction(int $id): Response |
|
|
|
|
|
|
75
|
|
|
{ |
|
76
|
|
|
$comment = $this->get('sulu.repository.comment')->findCommentById($id); |
|
77
|
|
|
if (!$comment) { |
|
78
|
3 |
|
throw new EntityNotFoundException(CommentInterface::class, $id); |
|
79
|
3 |
|
} |
|
80
|
|
|
|
|
81
|
3 |
|
return $this->handleView($this->view($comment)); |
|
82
|
3 |
|
} |
|
83
|
3 |
|
|
|
84
|
3 |
View Code Duplication |
public function putAction(int $id, Request $request): Response |
|
|
|
|
|
|
85
|
3 |
|
{ |
|
86
|
3 |
|
/** @var CommentInterface|null $comment */ |
|
87
|
|
|
$comment = $this->get('sulu.repository.comment')->findCommentById($id); |
|
88
|
|
|
if (!$comment) { |
|
89
|
3 |
|
throw new EntityNotFoundException(CommentInterface::class, $id); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$comment->setMessage($request->request->get('message')); |
|
93
|
|
|
|
|
94
|
|
|
$this->get('sulu_comment.manager')->update($comment); |
|
95
|
|
|
$this->get('doctrine.orm.entity_manager')->flush(); |
|
96
|
|
|
|
|
97
|
|
|
return $this->handleView($this->view($comment)); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
View Code Duplication |
public function cdeleteAction(Request $request): Response |
|
|
|
|
|
|
101
|
1 |
|
{ |
|
102
|
|
|
/** @var int[] $ids */ |
|
103
|
1 |
|
$ids = array_filter(explode(',', $request->query->get('ids'))); |
|
104
|
1 |
|
if (0 === count($ids)) { |
|
105
|
|
|
return $this->handleView($this->view(null, 204)); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
1 |
|
$this->get('sulu_comment.manager')->delete($ids); |
|
109
|
|
|
$this->get('doctrine.orm.entity_manager')->flush(); |
|
110
|
|
|
|
|
111
|
|
|
return $this->handleView($this->view(null, 204)); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
View Code Duplication |
public function deleteAction(int $id): Response |
|
|
|
|
|
|
115
|
|
|
{ |
|
116
|
|
|
$this->get('sulu_comment.manager')->delete([$id]); |
|
117
|
|
|
$this->get('doctrine.orm.entity_manager')->flush(); |
|
118
|
|
|
|
|
119
|
|
|
return $this->handleView($this->view(null, 204)); |
|
120
|
|
|
} |
|
121
|
1 |
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* trigger a action for given comment specified over action get-parameter |
|
124
|
1 |
|
* - publish: Publish a comment |
|
125
|
1 |
|
* - unpublish: Unpublish a comment. |
|
126
|
|
|
* |
|
127
|
|
|
* @Post("/comments/{id}") |
|
128
|
|
|
*/ |
|
129
|
1 |
|
public function postTriggerAction(int $id, Request $request): Response |
|
130
|
|
|
{ |
|
131
|
1 |
|
$action = $this->getRequestParameter($request, 'action', true); |
|
132
|
1 |
|
|
|
133
|
|
|
/** @var CommentRepositoryInterface $commentRepository */ |
|
134
|
1 |
|
$commentRepository = $this->get('sulu.repository.comment'); |
|
135
|
|
|
$commentManager = $this->get('sulu_comment.manager'); |
|
136
|
|
|
$comment = $commentRepository->findCommentById($id); |
|
137
|
|
|
if (!$comment) { |
|
138
|
|
|
return $this->handleView($this->view(null, 404)); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
switch ($action) { |
|
142
|
|
|
case 'unpublish': |
|
143
|
|
|
$commentManager->unpublish($comment); |
|
144
|
1 |
|
|
|
145
|
|
|
break; |
|
146
|
1 |
|
case 'publish': |
|
147
|
1 |
|
$commentManager->publish($comment); |
|
148
|
|
|
|
|
149
|
1 |
|
break; |
|
150
|
|
|
default: |
|
151
|
|
|
throw new RestException('Unrecognized action: ' . $action); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$this->get('doctrine.orm.entity_manager')->flush(); |
|
155
|
|
|
|
|
156
|
|
|
return $this->handleView($this->view($comment)); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.