|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Superdesk Web Publisher Content 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\Method; |
|
21
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
22
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; |
|
23
|
|
|
use SWP\Bundle\CoreBundle\Form\Type\PublishDestinationType; |
|
24
|
|
|
use SWP\Bundle\CoreBundle\Model\PublishDestinationInterface; |
|
25
|
|
|
use SWP\Bundle\MultiTenancyBundle\MultiTenancyEvents; |
|
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
|
|
|
|
|
31
|
|
|
class PublishDestinationController extends Controller |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* Add a new publish destination. |
|
35
|
|
|
* |
|
36
|
|
|
* @ApiDoc( |
|
37
|
|
|
* resource=true, |
|
38
|
|
|
* description="Add a new publish destination", |
|
39
|
|
|
* statusCodes={ |
|
40
|
|
|
* 201="Returned on success.", |
|
41
|
|
|
* 400="Returned when form have errors" |
|
42
|
|
|
* }, |
|
43
|
|
|
* input="SWP\Bundle\CoreBundle\Form\Type\PublishDestinationType" |
|
44
|
|
|
* ) |
|
45
|
|
|
* |
|
46
|
|
|
* @Route("/api/{version}/organization/destinations/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_publishing_destination_create") |
|
47
|
|
|
* |
|
48
|
|
|
* @Method("POST") |
|
49
|
|
|
* |
|
50
|
|
|
* @param Request $request |
|
51
|
|
|
* |
|
52
|
|
|
* @return SingleResourceResponse |
|
53
|
|
|
*/ |
|
54
|
|
|
public function createAction(Request $request) |
|
55
|
|
|
{ |
|
56
|
|
|
$tenantContext = $this->get('swp_multi_tenancy.tenant_context'); |
|
57
|
|
|
|
|
58
|
|
|
$this->get('event_dispatcher')->dispatch(MultiTenancyEvents::TENANTABLE_DISABLE); |
|
59
|
|
|
|
|
60
|
|
|
$destination = $this->get('swp.factory.publish_destination')->create(); |
|
61
|
|
|
$form = $this->createForm(PublishDestinationType::class, $destination, ['method' => $request->getMethod()]); |
|
62
|
|
|
$currentOrganization = $tenantContext->getTenant()->getOrganization(); |
|
63
|
|
|
$form->handleRequest($request); |
|
64
|
|
|
|
|
65
|
|
|
if ($form->isValid()) { |
|
66
|
|
|
$repository = $this->get('swp.repository.publish_destination'); |
|
67
|
|
|
/** @var PublishDestinationInterface $publishDestination */ |
|
68
|
|
|
$publishDestination = $repository->findOneByTenant($destination->getTenant()); |
|
69
|
|
|
if (null !== $publishDestination) { |
|
70
|
|
|
$repository->remove($publishDestination); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$currentOrganization->addPublishDestination($destination); |
|
74
|
|
|
$this->get('swp.object_manager.publish_destination')->flush(); |
|
75
|
|
|
|
|
76
|
|
|
return new SingleResourceResponse($destination, new ResponseContext(200)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return new SingleResourceResponse($form, new ResponseContext(400)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Updates existing publish destination. |
|
84
|
|
|
* |
|
85
|
|
|
* @ApiDoc( |
|
86
|
|
|
* resource=true, |
|
87
|
|
|
* description="Update existing publish destination", |
|
88
|
|
|
* statusCodes={ |
|
89
|
|
|
* 201="Returned on success.", |
|
90
|
|
|
* 400="Returned when form have errors" |
|
91
|
|
|
* }, |
|
92
|
|
|
* input="SWP\Bundle\CoreBundle\Form\Type\PublishDestinationType" |
|
93
|
|
|
* ) |
|
94
|
|
|
* |
|
95
|
|
|
* @Route("/api/{version}/organization/destinations/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_publishing_destination_update", requirements={"id"="\d+"}) |
|
96
|
|
|
* |
|
97
|
|
|
* @Method("PATCH") |
|
98
|
|
|
* @ParamConverter("publishDestination", class="SWP\Bundle\CoreBundle\Model\PublishDestination") |
|
99
|
|
|
* |
|
100
|
|
|
* @param Request $request |
|
101
|
|
|
* |
|
102
|
|
|
* @return SingleResourceResponse |
|
103
|
|
|
*/ |
|
104
|
|
View Code Duplication |
public function updateAction(Request $request, PublishDestinationInterface $publishDestination) |
|
|
|
|
|
|
105
|
|
|
{ |
|
106
|
|
|
$objectManager = $this->get('swp.object_manager.publish_destination'); |
|
107
|
|
|
|
|
108
|
|
|
$form = $this->createForm(PublishDestinationType::class, $publishDestination, [ |
|
109
|
|
|
'method' => $request->getMethod(), |
|
110
|
|
|
]); |
|
111
|
|
|
|
|
112
|
|
|
$form->handleRequest($request); |
|
113
|
|
|
if ($form->isValid()) { |
|
114
|
|
|
$objectManager->flush(); |
|
115
|
|
|
$objectManager->refresh($publishDestination); |
|
116
|
|
|
|
|
117
|
|
|
return new SingleResourceResponse($publishDestination); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return new SingleResourceResponse($form, new ResponseContext(400)); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
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.