· 7 years ago · Apr 12, 2018, 09:42 AM
1<?php
2
3namespace ApiBundle\Controller;
4
5use CoreBundle\Entity\Client;
6use CoreBundle\Form\PublishingChannelFormType;
7use CoreBundle\Repository\MediaChannelRepository;
8use CoreBundle\Repository\MediaContentRepository;
9use CoreBundle\Repository\PublishingChannelRepository;
10use CoreBundle\Services\Providers\TwitterClient;
11use CoreBundle\Services\PublishingChannel\FacebookChannelGenerator;
12use CoreBundle\Services\PublishingChannel\IPublishingChannel;
13use CoreBundle\Services\PublishingChannel\IPublishingChannelGenerator;
14use FOS\RestBundle\Controller\FOSRestController;
15use FOS\RestBundle\Controller\Annotations as Rest;
16use FOS\RestBundle\Request\ParamFetcher;
17use Symfony\Component\Config\Definition\Exception\Exception;
18use Symfony\Component\HttpFoundation\Request;
19use Symfony\Component\HttpFoundation\Response;
20use FOS\RestBundle\View\View;
21
22use Swagger\Annotations as SWG;
23use Nelmio\ApiDocBundle\Annotation\Model;
24
25use CoreBundle\Entity\PublishingChannel;
26use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
27use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
28use Symfony\Component\Security\Core\Exception\AccessDeniedException;
29
30
31class PublishingChannelsController extends FOSRestController
32{
33
34 use ClientAwareControllerTrait;
35
36 /**
37 *
38 * @Rest\Get("/publishing-channels", name="api_get_publishing_channels")
39 *
40 * @Rest\QueryParam(
41 * name="clientId",
42 * nullable=true,
43 * default=null,
44 * description="client id"
45 * )
46 *
47 * @SWG\Get(
48 * summary="Get list of all publishing channels",
49 * tags={"Publishing Channel"},
50 * description="Get list of all publishing channels",
51 * produces={"application/json"},
52 * @SWG\Parameter(
53 * name="Authorization",
54 * in="header",
55 * required=true,
56 * description="Bearer {userToken}",
57 * type="string"
58 * ),
59 * @SWG\Response(
60 * response=200,
61 * description="Get list of all publishing channels",
62 * @SWG\Schema(
63 * type="array",
64 * @Model(type=PublishingChannel::class)
65 * )
66 * ),
67 * @SWG\Response(
68 * response=401,
69 * description="Unauthorized: JWT Token not found"
70 * ),
71 * @SWG\Response(
72 * response=403,
73 * description="User is not allow to list client publishing channel"
74 * )
75 * )
76 *
77 * @param $clientId
78 * @return array|bool
79 */
80 public function getAllPublishingChannelsAction($clientId) {
81 $client = $this->getClient($clientId);
82
83 if(!$this->isClientAllow($client)) {
84 throw new AccessDeniedException("You are not allowed list publishing channel");
85 }
86
87 /** @var PublishingChannelRepository $publishingChannelRepository */
88 $publishingChannelRepository = $this->get('core.repository.publishing_channel');
89
90 return $publishingChannelRepository->findAllByClient($client);
91 }
92
93 /**
94 * @Rest\Get("/publishing-channels/{id}", name="api_get_publishing_channel_info")
95 *
96 * @SWG\Get(
97 * summary="Get info about specified publishing channel",
98 * tags={"Publishing Channel"},
99 * description="Get info about specified publishing channel",
100 * produces={"application/json"},
101 * @SWG\Parameter(
102 * name="Authorization",
103 * in="header",
104 * required=true,
105 * description="Bearer {userToken}",
106 * type="string"
107 * ),
108 * @SWG\Parameter(
109 * name="id",
110 * in="path",
111 * description="channel id",
112 * required=true,
113 * type="integer",
114 * format="int32"
115 * ),
116 * @SWG\Response(
117 * response=200,
118 * description="Return information about specified publishing channel if exist",
119 * @SWG\Schema(@Model(type=PublishingChannel::class))
120 * ),
121 * @SWG\Response(
122 * response=401,
123 * description="Unauthorized: JWT Token not found"
124 * ),
125 * @SWG\Response(
126 * response=404,
127 * description="Publishing channel with given id doesn't exist"
128 * ),
129 * @SWG\Response(
130 * response=403,
131 * description="Publishing channel does not belong to the this user"
132 * )
133 * )
134 *
135 * @param $id
136 * @return array
137 */
138 public function getPublishingChannelAction($id) {
139 /** @var PublishingChannelRepository $publishingChannelRepository */
140 $publishingChannelRepository = $this->get('core.repository.publishing_channel');
141
142 /** @var PublishingChannel $publishingChannel */
143 $publishingChannel = $publishingChannelRepository->find($id);
144 if(empty($publishingChannel)) {
145 throw new NotFoundHttpException("Publishing channel with given id doesn't exist");
146 }
147
148 if(!$this->isClientAllow($publishingChannel->getClient())) {
149 throw new AccessDeniedException("You are not allowed list publishing channel");
150 }
151
152 return $publishingChannel;
153 }
154
155
156 /**
157 * @Rest\Get("/publishing-channels/{id}/content", name="api_get_publishing_channel_content")
158 *
159 * @SWG\Get(
160 * summary="Get content for publishing channel",
161 * tags={"Publishing Channel"},
162 * description="Get content for specified publishing channel",
163 * produces={"application/json"},
164 * @SWG\Parameter(
165 * name="Authorization",
166 * in="header",
167 * required=true,
168 * description="Bearer {userToken}",
169 * type="string"
170 * ),
171 * @SWG\Parameter(
172 * name="id",
173 * in="path",
174 * description="channel id",
175 * required=true,
176 * type="integer",
177 * format="int32"
178 * ),
179 * @SWG\Response(
180 * response=200,
181 * description="Get publishing content for given channel",
182 * @SWG\Schema(
183 * type="array",
184 * @SWG\Items(ref="#/definitions/PublishingContent")
185 * )
186 * ),
187 * @SWG\Response(
188 * response=401,
189 * description="Unauthorized: JWT Token not found"
190 * ),
191 * @SWG\Response(
192 * response=404,
193 * description="Publishing channel with given id doesn't exist"
194 * ),
195 * @SWG\Response(
196 * response=403,
197 * description="Publishing channel does not belong to the this user"
198 * )
199 * )
200 *
201 * @param $id
202 * @return array
203 */
204 public function getPublishingChannelContentAction($id) {
205 /** @var PublishingChannelRepository $publishingChannelRepository */
206 $publishingChannelRepository = $this->get('core.repository.publishing_channel');
207
208 /** @var PublishingChannel $publishingChannel */
209 $publishingChannel = $publishingChannelRepository->find($id);
210 if(empty($publishingChannel)) {
211 throw new NotFoundHttpException("Publishing channel with given id doesn't exist");
212 }
213
214 if(!$this->isClientAllow($publishingChannel->getClient())) {
215 throw new AccessDeniedException("You are not allowed list publishing channel");
216 }
217
218 $content = $this->get('core.repository.publishing_content')->findByChannel($publishingChannel);
219 return $content;
220 }
221
222
223 /**
224 *
225 * @Rest\Post("/publishing-channels", name="api_add_publishing_channels")
226 *
227 * @Rest\RequestParam(
228 * name="clientId",
229 * key="clientId",
230 * default=null,
231 * description="clientId",
232 * strict=true,
233 * map=false,
234 * nullable=true
235 * )
236 *
237 * @Rest\RequestParam(
238 * name="type",
239 * key="type",
240 * requirements="(facebook-page|facebook-account|twitter|instagram)",
241 * default=null,
242 * description="Type",
243 * strict=true,
244 * map=false,
245 * nullable=false
246 * )
247 *
248 * @Rest\RequestParam(
249 * name="name",
250 * key="name",
251 * default=null,
252 * description="Name",
253 * strict=true,
254 * map=false,
255 * nullable=false
256 * )
257 *
258 * @Rest\RequestParam(
259 * name="channelId",
260 * key="channelId",
261 * default=null,
262 * description="channelId",
263 * strict=true,
264 * map=false,
265 * nullable=false
266 * )
267 *
268 * @Rest\RequestParam(
269 * name="accessToken",
270 * key="accessToken",
271 * default=null,
272 * description="accessToken",
273 * strict=true,
274 * map=false,
275 * nullable=false
276 * )
277 *
278 * @SWG\Post(
279 * summary="Adding new publishing channel",
280 * tags={"Publishing Channel"},
281 * description="Adding new publishing channel",
282 * produces={"application/json"},
283 * @SWG\Parameter(
284 * name="Authorization",
285 * in="header",
286 * required=true,
287 * description="Bearer {userToken}",
288 * type="string"
289 * ),
290 * @SWG\Parameter(
291 * name="type",
292 * in="formData",
293 * description="type. Enum filed with following types: facebook-page, facebook-account, twitter",
294 * type="string",
295 * enum={"facebook-page", "facebook-account", "twitter", "instagram"}
296 * ),
297 * @SWG\Parameter(
298 * name="name",
299 * in="formData",
300 * description="channel name",
301 * type="string"
302 * ),
303 * @SWG\Parameter(
304 * name="channelId",
305 * in="formData",
306 * description="channelId",
307 * type="string"
308 * ),
309 * @SWG\Parameter(
310 * name="accessToken",
311 * in="formData",
312 * description="AccessToken; For this accessToken is array contains oauthToken and oauthTokenSecret values",
313 * type="string"
314 * ),
315 * @SWG\Parameter(
316 * name="clientId",
317 * in="formData",
318 * description="clientId",
319 * type="string"
320 * ),
321 * @SWG\Response(
322 * response=201,
323 * description="No Content returns. New publishing channel has been created"
324 * ),
325 * @SWG\Response(
326 * response=401,
327 * description="Unauthorized: JWT Token not found"
328 * ),
329 * @SWG\Response(
330 * response=400,
331 * description="Bad params"
332 * ),
333 * @SWG\Response(
334 * response=403,
335 * description="Publishing channel does not belong to the this user"
336 * )
337 * )
338 *
339 * @param ParamFetcher $paramFetcher
340 * @return \Symfony\Component\Form\Form|static
341 * @throws \Exception
342 */
343 public function addNewChannelAction(ParamFetcher $paramFetcher) {
344 /** @var PublishingChannelRepository $publishingChannelRepository */
345 $publishingChannelRepository = $this->get('core.repository.publishing_channel');
346
347 $channelId = $paramFetcher->get('channelId');
348 $clientId = $paramFetcher->get('clientId');
349 $accessToken = $paramFetcher->get('accessToken');
350
351 $client = $this->getClient($clientId);
352
353 if($publishingChannelRepository->findOneByClientAndChannel($client, $channelId)) {
354 throw new BadRequestHttpException("Client has already has this channel");
355 }
356
357 if(!$this->isClientAllow($client)) {
358 throw new AccessDeniedException("You are not allowed list publishing channel");
359 }
360
361 try {
362 /** @var $channelGenerator IPublishingChannelGenerator */
363 $channelGenerator = $this->getGenerator($paramFetcher->get('type'));
364 $channelGenerator->generate($client, $accessToken, $channelId, $paramFetcher->get('name'));
365 } catch (\Exception $e) {
366 throw new \Exception($e->getMessage(), 400);
367 }
368
369 return View::create(null, 201);
370
371 }
372}