|
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\Routing\ClassResourceInterface; |
|
15
|
|
|
use Sulu\Bundle\CommentBundle\Entity\ThreadInterface; |
|
16
|
|
|
use Sulu\Component\Rest\Exception\EntityNotFoundException; |
|
17
|
|
|
use Sulu\Component\Rest\ListBuilder\FieldDescriptorInterface; |
|
18
|
|
|
use Sulu\Component\Rest\ListBuilder\ListRepresentation; |
|
19
|
|
|
use Sulu\Component\Rest\RequestParametersTrait; |
|
20
|
|
|
use Sulu\Component\Rest\RestController; |
|
21
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
22
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
23
|
|
|
|
|
24
|
|
|
class ThreadController extends RestController implements ClassResourceInterface |
|
25
|
|
|
{ |
|
26
|
|
|
use RequestParametersTrait; |
|
27
|
|
|
|
|
28
|
|
|
public function cgetAction(Request $request): Response |
|
29
|
|
|
{ |
|
30
|
|
|
$restHelper = $this->get('sulu_core.doctrine_rest_helper'); |
|
31
|
|
|
$factory = $this->get('sulu_core.doctrine_list_builder_factory'); |
|
32
|
|
|
$listBuilder = $factory->create($this->getParameter('sulu.model.thread.class')); |
|
33
|
|
|
|
|
34
|
|
|
/** @var FieldDescriptorInterface[] $fieldDescriptors */ |
|
35
|
|
|
$fieldDescriptors = $this->get('sulu_core.list_builder.field_descriptor_factory') |
|
36
|
|
|
->getFieldDescriptors('threads'); |
|
37
|
|
|
$restHelper->initializeListBuilder($listBuilder, $fieldDescriptors); |
|
38
|
|
|
|
|
39
|
|
View Code Duplication |
foreach ($request->query->all() as $filterKey => $filterValue) { |
|
|
|
|
|
|
40
|
|
|
if (isset($fieldDescriptors[$filterKey])) { |
|
41
|
|
|
$listBuilder->where($fieldDescriptors[$filterKey], $filterValue); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$typeParameter = $request->get('types'); |
|
46
|
|
|
if ($typeParameter) { |
|
47
|
|
|
$listBuilder->in($fieldDescriptors['type'], array_filter(explode(',', $typeParameter))); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$items = $listBuilder->execute(); |
|
51
|
3 |
|
$list = new ListRepresentation( |
|
52
|
|
|
$items, |
|
53
|
3 |
|
'threads', |
|
54
|
3 |
|
$request->attributes->get('_route'), |
|
55
|
3 |
|
$request->query->all(), |
|
56
|
|
|
$listBuilder->getCurrentPage(), |
|
57
|
3 |
|
$listBuilder->getLimit(), |
|
58
|
3 |
|
$listBuilder->count() |
|
59
|
|
|
); |
|
60
|
3 |
|
|
|
61
|
3 |
|
return $this->handleView($this->view($list, 200)); |
|
62
|
3 |
|
} |
|
63
|
|
|
|
|
64
|
|
View Code Duplication |
public function getAction(int $id): Response |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
3 |
|
$thread = $this->get('sulu.repository.thread')->findThreadById($id); |
|
67
|
3 |
|
if (!$thread) { |
|
68
|
1 |
|
throw new EntityNotFoundException(ThreadInterface::class, $id); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
3 |
|
return $this->handleView($this->view($thread)); |
|
72
|
3 |
|
} |
|
73
|
|
|
|
|
74
|
3 |
View Code Duplication |
public function putAction(int $id, Request $request): Response |
|
|
|
|
|
|
75
|
3 |
|
{ |
|
76
|
3 |
|
/** @var ThreadInterface|null $thread */ |
|
77
|
3 |
|
$thread = $this->get('sulu.repository.thread')->findThreadById($id); |
|
78
|
3 |
|
if (!$thread) { |
|
79
|
3 |
|
throw new EntityNotFoundException(ThreadInterface::class, $id); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
3 |
|
$thread->setTitle($request->request->get('title')); |
|
83
|
|
|
|
|
84
|
|
|
$this->get('sulu_comment.manager')->updateThread($thread); |
|
85
|
|
|
$this->get('doctrine.orm.entity_manager')->flush(); |
|
86
|
|
|
|
|
87
|
|
|
return $this->handleView($this->view($thread)); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
View Code Duplication |
public function cdeleteAction(Request $request): Response |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
|
|
/** @var int[] $ids */ |
|
93
|
|
|
$ids = array_filter(explode(',', $request->query->get('ids'))); |
|
94
|
1 |
|
if (0 === count($ids)) { |
|
95
|
|
|
return $this->handleView($this->view(null, 204)); |
|
96
|
1 |
|
} |
|
97
|
1 |
|
|
|
98
|
|
|
$this->get('sulu_comment.manager')->deleteThreads($ids); |
|
99
|
|
|
$this->get('doctrine.orm.entity_manager')->flush(); |
|
100
|
|
|
|
|
101
|
1 |
|
return $this->handleView($this->view(null, 204)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
View Code Duplication |
public function deleteAction(int $id): Response |
|
|
|
|
|
|
105
|
|
|
{ |
|
106
|
|
|
$this->get('sulu_comment.manager')->deleteThreads([$id]); |
|
107
|
|
|
$this->get('doctrine.orm.entity_manager')->flush(); |
|
108
|
|
|
|
|
109
|
|
|
return $this->handleView($this->view(null, 204)); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
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.