1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Superdesk Web Publisher Core Bundle. |
5
|
|
|
* |
6
|
|
|
* Copyright 2016 Sourcefabric z.ú. 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 2016 Sourcefabric z.ú |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace SWP\Bundle\CoreBundle\Controller; |
16
|
|
|
|
17
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
18
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
19
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
20
|
|
|
use SWP\Bundle\CoreBundle\Form\Type\FacebookApplicationType; |
21
|
|
|
use SWP\Bundle\CoreBundle\Model\FacebookApplication; |
22
|
|
|
use SWP\Bundle\FacebookInstantArticlesBundle\Model\ApplicationInterface; |
23
|
|
|
use SWP\Component\Common\Criteria\Criteria; |
24
|
|
|
use SWP\Component\Common\Pagination\PaginationData; |
25
|
|
|
use SWP\Component\Common\Response\ResourcesListResponse; |
26
|
|
|
use SWP\Component\Common\Response\ResponseContext; |
27
|
|
|
use SWP\Component\Common\Response\SingleResourceResponse; |
28
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
29
|
|
|
use Symfony\Component\HttpFoundation\Request; |
30
|
|
|
use Symfony\Component\HttpKernel\Exception\ConflictHttpException; |
31
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
32
|
|
|
|
33
|
|
View Code Duplication |
class FbApplicationController extends Controller |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @ApiDoc( |
37
|
|
|
* resource=true, |
38
|
|
|
* description="Lists Facebook Applications", |
39
|
|
|
* statusCodes={ |
40
|
|
|
* 200="Returned on success.", |
41
|
|
|
* 500="Unexpected error." |
42
|
|
|
* } |
43
|
|
|
* ) |
44
|
|
|
* @Route("/api/{version}/facebook/applications/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_list_facebook_applications") |
45
|
|
|
* @Method("GET") |
46
|
|
|
*/ |
47
|
2 |
|
public function listAction(Request $request) |
48
|
|
|
{ |
49
|
2 |
|
$repository = $this->get('swp.repository.facebook_application'); |
50
|
|
|
|
51
|
2 |
|
$items = $repository->getPaginatedByCriteria( |
52
|
2 |
|
new Criteria(), |
53
|
2 |
|
['id' => 'asc'], |
54
|
2 |
|
new PaginationData($request) |
55
|
|
|
); |
56
|
|
|
|
57
|
2 |
|
return new ResourcesListResponse($items); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @ApiDoc( |
62
|
|
|
* resource=true, |
63
|
|
|
* description="Create Facebook application", |
64
|
|
|
* statusCodes={ |
65
|
|
|
* 201="Returned on success.", |
66
|
|
|
* 400="Returned when not valid data." |
67
|
|
|
* }, |
68
|
|
|
* input="SWP\Bundle\CoreBundle\Form\Type\FacebookApplicationType" |
69
|
|
|
* ) |
70
|
|
|
* @Route("/api/{version}/facebook/applications/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_create_facebook_applications") |
71
|
|
|
* @Method("POST") |
72
|
|
|
*/ |
73
|
4 |
|
public function createAction(Request $request) |
74
|
|
|
{ |
75
|
|
|
/* @var FacebookApplication $feed */ |
76
|
4 |
|
$application = $this->get('swp.factory.facebook_application')->create(); |
77
|
4 |
|
$form = $this->createForm(FacebookApplicationType::class, $application, ['method' => $request->getMethod()]); |
78
|
|
|
|
79
|
4 |
|
$form->handleRequest($request); |
80
|
4 |
|
if ($form->isValid()) { |
81
|
4 |
|
$this->checkIfApplicationExists($application); |
82
|
4 |
|
$this->get('swp.repository.facebook_application')->add($application); |
83
|
|
|
|
84
|
4 |
|
return new SingleResourceResponse($application, new ResponseContext(201)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return new SingleResourceResponse($form, new ResponseContext(400)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @ApiDoc( |
92
|
|
|
* resource=true, |
93
|
|
|
* description="Delete Facebook application", |
94
|
|
|
* statusCodes={ |
95
|
|
|
* 204="Returned on success.", |
96
|
|
|
* 500="Unexpected error.", |
97
|
|
|
* 404="Application not found", |
98
|
|
|
* 409="Application is used by page" |
99
|
|
|
* } |
100
|
|
|
* ) |
101
|
|
|
* @Route("/api/{version}/facebook/applications/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_delete_facebook_applications") |
102
|
|
|
* @Method("DELETE") |
103
|
|
|
*/ |
104
|
1 |
|
public function deleteAction($id) |
105
|
|
|
{ |
106
|
1 |
|
$repository = $this->get('swp.repository.facebook_application'); |
107
|
1 |
|
if (null === $application = $repository->findOneBy(['id' => $id])) { |
108
|
|
|
throw new NotFoundHttpException('There is no Application with provided id!'); |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
if (null !== $page = $this->get('swp.repository.facebook_page')->findOneBy(['id' => $id])) { |
112
|
|
|
throw new ConflictHttpException(sprintf('This Application is used by page with id: %s!', $page->getId())); |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
$repository->remove($application); |
116
|
|
|
|
117
|
1 |
|
return new SingleResourceResponse(null, new ResponseContext(204)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param ApplicationInterface $application |
122
|
|
|
*/ |
123
|
4 |
|
private function checkIfApplicationExists(ApplicationInterface $application) |
124
|
|
|
{ |
125
|
4 |
|
if (null !== $this->get('swp.repository.facebook_application')->findOneBy([ |
126
|
4 |
|
'appId' => $application->getAppId(), |
127
|
|
|
]) |
128
|
|
|
) { |
129
|
1 |
|
throw new ConflictHttpException('This Application already exists!'); |
130
|
|
|
} |
131
|
4 |
|
} |
132
|
|
|
} |
133
|
|
|
|
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.