1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the GraphQL Bundle package. |
4
|
|
|
* |
5
|
|
|
* (c) YnloUltratech <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
******************************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Mutation; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\Form\FormEvent; |
14
|
|
|
use Ynlo\GraphQLBundle\Exception\Controlled\NotFoundError; |
15
|
|
|
use Ynlo\GraphQLBundle\Model\NodeInterface; |
16
|
|
|
use Ynlo\GraphQLBundle\Validator\ConstraintViolationList; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class DeleteNodeMutation |
20
|
|
|
*/ |
21
|
|
|
class DeleteNode extends AbstractMutationResolver |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var NodeInterface |
25
|
|
|
*/ |
26
|
|
|
protected $deletedNode; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function process(&$data) |
32
|
|
|
{ |
33
|
|
|
$this->preDelete($data); |
34
|
|
|
foreach ($this->extensions as $extension) { |
35
|
|
|
$extension->preDelete($data, $this, $this->context); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$this->deletedNode = clone $data; //clone required to avoid node without id after delete |
39
|
|
|
|
40
|
|
|
$this->getManager()->remove($data); |
41
|
|
|
$this->getManager()->flush(); |
42
|
|
|
|
43
|
|
|
$this->postDelete($this->deletedNode); |
44
|
|
|
foreach ($this->extensions as $extension) { |
45
|
|
|
$extension->postDelete($this->deletedNode, $this, $this->context); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function returnPayload($data, ConstraintViolationList $violations, $inputSource) |
53
|
|
|
{ |
54
|
|
|
$class = $this->getPayloadClass(); |
55
|
|
|
|
56
|
|
|
return new $class( |
57
|
|
|
$this->deletedNode, |
58
|
|
|
$inputSource['clientMutationId'] ?? null |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritDoc} |
64
|
|
|
*/ |
65
|
|
|
public function onSubmit(FormEvent $event) |
66
|
|
|
{ |
67
|
|
|
$node = $this->context->getDefinition()->getNode(); |
68
|
|
|
$class = $this->context->getEndpoint()->getClassForType($node); |
|
|
|
|
69
|
|
|
|
70
|
|
|
if (!$event->getData() instanceof NodeInterface || !$event->getData()->getId() || !is_a($event->getData(), $class)) { |
71
|
|
|
throw new NotFoundError(); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param NodeInterface $node |
77
|
|
|
*/ |
78
|
|
|
protected function preDelete(NodeInterface $node) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
//override |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param NodeInterface $node |
85
|
|
|
*/ |
86
|
|
|
protected function postDelete(NodeInterface $node) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
//override |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|