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 GraphQL\Error\UserError; |
14
|
|
|
use Symfony\Component\Form\FormEvent; |
15
|
|
|
use Ynlo\GraphQLBundle\Exception\Controlled\NotFoundError; |
16
|
|
|
use Ynlo\GraphQLBundle\Model\NodeInterface; |
17
|
|
|
use Ynlo\GraphQLBundle\Validator\ConstraintViolationList; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class DeleteBatchNodeMutation |
21
|
|
|
*/ |
22
|
|
|
class DeleteBatchNode extends AbstractMutationResolver |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $deletedRecords = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
public function process(&$data) |
33
|
|
|
{ |
34
|
|
|
foreach ($data['ids'] as $item) { |
35
|
|
|
$this->preDelete($item); |
36
|
|
|
foreach ($this->extensions as $extension) { |
37
|
|
|
$extension->preDelete($item, $this, $this->context); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->deletedRecords[] = clone $item; //clone required to avoid node without id after delete |
41
|
|
|
$this->getManager()->remove($item); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->getManager()->flush(); |
45
|
|
|
|
46
|
|
|
foreach ($data['ids'] as $item) { |
47
|
|
|
$this->postDelete($item); |
48
|
|
|
foreach ($this->extensions as $extension) { |
49
|
|
|
$extension->postDelete($item, $this, $this->context); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function returnPayload($data, ConstraintViolationList $violations, $inputSource) |
58
|
|
|
{ |
59
|
|
|
$class = $this->getPayloadClass(); |
60
|
|
|
|
61
|
|
|
return new $class($this->deletedRecords, $inputSource['clientMutationId'] ?? null); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritDoc} |
66
|
|
|
*/ |
67
|
|
|
public function onSubmit(FormEvent $event) |
68
|
|
|
{ |
69
|
|
|
$node = $this->context->getDefinition()->getNode(); |
70
|
|
|
$class = $this->context->getEndpoint()->getClassForType($node); |
|
|
|
|
71
|
|
|
|
72
|
|
|
if ($event->getForm()->get('ids') && is_array($event->getForm()->get('ids')->getData())) { |
73
|
|
|
foreach ($event->getForm()->get('ids')->getData() as $node) { |
74
|
|
|
if (!$node instanceof NodeInterface || !$node->getId() || !is_a($node, $class)) { |
75
|
|
|
throw new NotFoundError(); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} else { |
79
|
|
|
throw new UserError('Batch error, invalid data'); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param NodeInterface $node |
85
|
|
|
*/ |
86
|
|
|
protected function preDelete(NodeInterface $node) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
//override |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param NodeInterface $node |
93
|
|
|
*/ |
94
|
|
|
protected function postDelete(NodeInterface $node) |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
//override |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|