|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Superdesk Web Publisher Core Bundle. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright 2017 Sourcefabric z.ú. and contributors. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please see the |
|
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
|
12
|
|
|
* |
|
13
|
|
|
* @copyright 2017 Sourcefabric z.ú |
|
14
|
|
|
* @license http://www.superdesk.org/license |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace SWP\Bundle\CoreBundle\Controller; |
|
18
|
|
|
|
|
19
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
|
20
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; |
|
21
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
|
22
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
23
|
|
|
use SWP\Bundle\ContentBundle\Form\Type\ArticleType; |
|
24
|
|
|
use SWP\Component\Common\Criteria\Criteria; |
|
25
|
|
|
use SWP\Component\Common\Pagination\PaginationData; |
|
26
|
|
|
use SWP\Component\Common\Response\ResourcesListResponse; |
|
27
|
|
|
use SWP\Component\Common\Response\ResponseContext; |
|
28
|
|
|
use SWP\Component\Common\Response\SingleResourceResponse; |
|
29
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
30
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
31
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
32
|
|
|
|
|
33
|
|
|
class OrganizationController extends Controller |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* List all organizations. |
|
37
|
|
|
* |
|
38
|
|
|
* @ApiDoc( |
|
39
|
|
|
* resource=true, |
|
40
|
|
|
* description="List all organizations", |
|
41
|
|
|
* statusCodes={ |
|
42
|
|
|
* 200="Returned on success.", |
|
43
|
|
|
* 500="Returned when unexpected error occurred." |
|
44
|
|
|
* } |
|
45
|
|
|
* ) |
|
46
|
|
|
* @Route("/api/{version}/organizations/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_list_organizations") |
|
47
|
|
|
* @Method("GET") |
|
48
|
|
|
* |
|
49
|
|
|
* @Cache(expires="10 minutes", public=true) |
|
50
|
|
|
*/ |
|
51
|
|
View Code Duplication |
public function listAction(Request $request) |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
|
|
$organizations = $this->get('swp.repository.organization') |
|
54
|
|
|
->getPaginatedByCriteria(new Criteria(), [], new PaginationData($request)); |
|
55
|
|
|
|
|
56
|
|
|
return new ResourcesListResponse($organizations); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* List all current organization's articles. |
|
61
|
|
|
* |
|
62
|
|
|
* @ApiDoc( |
|
63
|
|
|
* resource=true, |
|
64
|
|
|
* description="List all current organization's articles", |
|
65
|
|
|
* statusCodes={ |
|
66
|
|
|
* 200="Returned on success.", |
|
67
|
|
|
* 500="Returned when unexpected error occurred." |
|
68
|
|
|
* }, |
|
69
|
|
|
* filters={ |
|
70
|
|
|
* {"name"="status", "dataType"="string", "pattern"="new|published|unpublished|canceled"} |
|
71
|
|
|
* } |
|
72
|
|
|
* ) |
|
73
|
|
|
* @Route("/api/{version}/organization/articles/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_list_organization_articles") |
|
74
|
|
|
* @Method("GET") |
|
75
|
|
|
* |
|
76
|
|
|
* @Cache(expires="10 minutes", public=true) |
|
77
|
|
|
*/ |
|
78
|
|
|
public function articlesAction(Request $request) |
|
79
|
|
|
{ |
|
80
|
|
|
$tenantContext = $this->get('swp_multi_tenancy.tenant_context'); |
|
81
|
|
|
$repository = $this->get('swp.repository.article'); |
|
82
|
|
|
$entityManager = $this->get('doctrine.orm.entity_manager'); |
|
83
|
|
|
$entityManager->getFilters()->disable('tenantable'); |
|
84
|
|
|
|
|
85
|
|
|
$items = $repository->getPaginatedByCriteria( |
|
86
|
|
|
new Criteria([ |
|
87
|
|
|
'organization' => $tenantContext->getTenant()->getOrganization(), |
|
88
|
|
|
'status' => $request->query->get('status', ''), |
|
89
|
|
|
]), |
|
90
|
|
|
[], |
|
91
|
|
|
new PaginationData($request) |
|
92
|
|
|
); |
|
93
|
|
|
|
|
94
|
|
|
$entityManager->getFilters()->enable('tenantable'); |
|
95
|
|
|
|
|
96
|
|
|
return new ResourcesListResponse($items); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Updates organization's article. |
|
101
|
|
|
* |
|
102
|
|
|
* @ApiDoc( |
|
103
|
|
|
* resource=true, |
|
104
|
|
|
* description="Updates organization's article", |
|
105
|
|
|
* statusCodes={ |
|
106
|
|
|
* 200="Returned on success.", |
|
107
|
|
|
* 400="Returned when validation failed.", |
|
108
|
|
|
* 500="Returned when unexpected error." |
|
109
|
|
|
* }, |
|
110
|
|
|
* input="SWP\Bundle\ContentBundle\Form\Type\ArticleType" |
|
111
|
|
|
* ) |
|
112
|
|
|
* @Route("/api/{version}/organization/articles/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_update_organization_articles", requirements={"id"="\d+"}) |
|
113
|
|
|
* @Method("PATCH") |
|
114
|
|
|
*/ |
|
115
|
|
View Code Duplication |
public function updateArticleAction(Request $request, int $id) |
|
|
|
|
|
|
116
|
|
|
{ |
|
117
|
|
|
$objectManager = $this->get('swp.object_manager.article'); |
|
118
|
|
|
$article = $this->getOrganizationArticle($id); |
|
119
|
|
|
$originalArticleStatus = $article->getStatus(); |
|
120
|
|
|
|
|
121
|
|
|
$form = $this->createForm(ArticleType::class, $article, ['method' => $request->getMethod()]); |
|
122
|
|
|
|
|
123
|
|
|
$form->handleRequest($request); |
|
124
|
|
|
if ($form->isValid()) { |
|
125
|
|
|
$this->get('swp.service.article')->reactOnStatusChange($originalArticleStatus, $article); |
|
126
|
|
|
$objectManager->flush(); |
|
127
|
|
|
$objectManager->refresh($article); |
|
128
|
|
|
|
|
129
|
|
|
return new SingleResourceResponse($article); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return new SingleResourceResponse($form, new ResponseContext(500)); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Show single tenant article. |
|
137
|
|
|
* |
|
138
|
|
|
* @ApiDoc( |
|
139
|
|
|
* resource=true, |
|
140
|
|
|
* description="Show single organization article", |
|
141
|
|
|
* statusCodes={ |
|
142
|
|
|
* 200="Returned on success." |
|
143
|
|
|
* } |
|
144
|
|
|
* ) |
|
145
|
|
|
* @Route("/api/{version}/organization/articles/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_show_organization_article", requirements={"id"="\d+"}) |
|
146
|
|
|
* @Method("GET") |
|
147
|
|
|
* |
|
148
|
|
|
* @Cache(expires="10 minutes", public=true) |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getAction(int $id) |
|
151
|
|
|
{ |
|
152
|
|
|
return new SingleResourceResponse($this->getOrganizationArticle($id)); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
private function getOrganizationArticle(int $id) |
|
156
|
|
|
{ |
|
157
|
|
|
$entityManager = $this->get('doctrine.orm.entity_manager'); |
|
158
|
|
|
$entityManager->getFilters()->disable('tenantable'); |
|
159
|
|
|
$article = $this->findOr404($id); |
|
160
|
|
|
$entityManager->getFilters()->enable('tenantable'); |
|
161
|
|
|
|
|
162
|
|
|
return $article; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
private function findOr404(int $id) |
|
166
|
|
|
{ |
|
167
|
|
|
$tenantContext = $this->get('swp_multi_tenancy.tenant_context'); |
|
168
|
|
|
|
|
169
|
|
|
if (null === $article = $this->get('swp.repository.article')->findOneBy([ |
|
170
|
|
|
'id' => $id, |
|
171
|
|
|
'organization' => $tenantContext->getTenant()->getOrganization(), |
|
172
|
|
|
])) { |
|
173
|
|
|
throw new NotFoundHttpException('Article was not found.'); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
return $article; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
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.