1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Superdesk Web Publisher Content Bundle. |
5
|
|
|
* |
6
|
|
|
* Copyright 2015 Sourcefabric z.u. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2015 Sourcefabric z.ú |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace SWP\Bundle\ContentBundle\Controller; |
16
|
|
|
|
17
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
18
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
19
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
20
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; |
21
|
|
|
use SWP\Component\Common\Criteria\Criteria; |
22
|
|
|
use SWP\Component\Common\Pagination\PaginationData; |
23
|
|
|
use SWP\Component\Common\Response\ResourcesListResponse; |
24
|
|
|
use SWP\Component\Common\Response\ResponseContext; |
25
|
|
|
use SWP\Component\Common\Response\SingleResourceResponse; |
26
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
27
|
|
|
use Symfony\Component\HttpFoundation\Request; |
28
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
29
|
|
|
use SWP\Bundle\ContentBundle\Form\Type\ArticleType; |
30
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleInterface; |
31
|
|
|
|
32
|
|
|
class ArticleController extends Controller |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* List all articles for current tenant. |
36
|
|
|
* |
37
|
|
|
* @ApiDoc( |
38
|
|
|
* resource=true, |
39
|
|
|
* description="List all articles for current tenant", |
40
|
|
|
* statusCodes={ |
41
|
|
|
* 200="Returned on success.", |
42
|
|
|
* } |
43
|
|
|
* ) |
44
|
|
|
* @Route("/api/{version}/content/articles/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_content_list_articles") |
45
|
|
|
* @Method("GET") |
46
|
|
|
* |
47
|
|
|
* @Cache(expires="10 minutes", public=true) |
48
|
|
|
* |
49
|
|
|
* @param Request $request |
50
|
|
|
* |
51
|
|
|
* @return ResourcesListResponse |
52
|
|
|
*/ |
53
|
|
|
public function listAction(Request $request) |
54
|
|
|
{ |
55
|
|
|
$articles = $this->get('swp.repository.article') |
56
|
|
|
->getPaginatedByCriteria(new Criteria(), [], new PaginationData($request)); |
57
|
|
|
|
58
|
|
|
return new ResourcesListResponse($articles); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Show single tenant article. |
63
|
|
|
* |
64
|
|
|
* @ApiDoc( |
65
|
|
|
* resource=true, |
66
|
|
|
* description="Show single tenant article", |
67
|
|
|
* statusCodes={ |
68
|
|
|
* 200="Returned on success." |
69
|
|
|
* } |
70
|
|
|
* ) |
71
|
|
|
* @Route("/api/{version}/content/articles/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_content_show_articles", requirements={"id"=".+"}) |
72
|
|
|
* @Method("GET") |
73
|
|
|
* |
74
|
|
|
* @Cache(expires="10 minutes", public=true) |
75
|
|
|
*/ |
76
|
10 |
View Code Duplication |
public function getAction($id) |
|
|
|
|
77
|
|
|
{ |
78
|
10 |
|
$article = $this->get('swp.provider.article')->getOneById($id); |
79
|
|
|
|
80
|
10 |
|
if (null === $article) { |
81
|
|
|
throw new NotFoundHttpException('Article was not found.'); |
82
|
|
|
} |
83
|
|
|
|
84
|
10 |
|
return new SingleResourceResponse($article); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Updates articles. |
89
|
|
|
* |
90
|
|
|
* Possible article statuses are: |
91
|
|
|
* |
92
|
|
|
* * new |
93
|
|
|
* * submitted |
94
|
|
|
* * published |
95
|
|
|
* * unpublished |
96
|
|
|
* |
97
|
|
|
* Changing status from any status to `published` will make article visible for every user. |
98
|
|
|
* |
99
|
|
|
* Changing status from `published` to any other will make article hidden for user who don't have rights to see unpublished articles. |
100
|
|
|
* |
101
|
|
|
* @ApiDoc( |
102
|
|
|
* resource=true, |
103
|
|
|
* description="Updates articles", |
104
|
|
|
* statusCodes={ |
105
|
|
|
* 200="Returned on success.", |
106
|
|
|
* 400="Returned when validation failed.", |
107
|
|
|
* 500="Returned when unexpected error." |
108
|
|
|
* }, |
109
|
|
|
* input="SWP\Bundle\ContentBundle\Form\Type\ArticleType" |
110
|
|
|
* ) |
111
|
|
|
* @Route("/api/{version}/content/articles/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_content_update_articles", requirements={"id"=".+"}) |
112
|
|
|
* @Method("PATCH") |
113
|
|
|
*/ |
114
|
4 |
|
public function updateAction(Request $request, $id) |
115
|
|
|
{ |
116
|
4 |
|
$objectManager = $this->get('swp.object_manager.article'); |
117
|
4 |
|
$article = $this->findOr404($id); |
118
|
4 |
|
$originalArticleStatus = $article->getStatus(); |
119
|
|
|
|
120
|
4 |
|
$form = $this->createForm(ArticleType::class, $article, ['method' => $request->getMethod()]); |
121
|
|
|
|
122
|
4 |
|
$form->handleRequest($request); |
123
|
4 |
|
if ($form->isValid()) { |
124
|
4 |
|
$this->reactOnStatusChange($originalArticleStatus, $article); |
125
|
4 |
|
$article->setUpdatedAt(new \DateTime()); |
126
|
4 |
|
$objectManager->flush(); |
127
|
4 |
|
$objectManager->refresh($article); |
128
|
|
|
|
129
|
4 |
|
return new SingleResourceResponse($article); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return new SingleResourceResponse($form, new ResponseContext(500)); |
133
|
|
|
} |
134
|
|
|
|
135
|
4 |
|
private function reactOnStatusChange($originalArticleStatus, ArticleInterface $article) |
136
|
|
|
{ |
137
|
4 |
|
$newArticleStatus = $article->getStatus(); |
138
|
4 |
|
if ($originalArticleStatus === $newArticleStatus) { |
139
|
4 |
|
return; |
140
|
|
|
} |
141
|
|
|
|
142
|
1 |
|
$articleService = $this->container->get('swp.service.article'); |
143
|
|
|
switch ($newArticleStatus) { |
144
|
1 |
|
case ArticleInterface::STATUS_PUBLISHED: |
145
|
1 |
|
$articleService->publish($article); |
146
|
1 |
|
break; |
147
|
|
|
default: |
148
|
|
|
$articleService->unpublish($article, $newArticleStatus); |
149
|
|
|
break; |
150
|
|
|
} |
151
|
1 |
|
} |
152
|
|
|
|
153
|
4 |
View Code Duplication |
private function findOr404($id) |
|
|
|
|
154
|
|
|
{ |
155
|
4 |
|
if (null === $article = $this->get('swp.provider.article')->getOneById($id)) { |
156
|
|
|
throw new NotFoundHttpException('Article was not found.'); |
157
|
|
|
} |
158
|
|
|
|
159
|
4 |
|
return $article; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
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.