Completed
Push — master ( 45d3b1...40b0e0 )
by Rafael
04:30
created

DeleteBatchNode::returnPayload()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 3
crap 6
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\Error\NodeNotFoundException;
16
use Ynlo\GraphQLBundle\Model\DeleteBatchNodePayload;
17
use Ynlo\GraphQLBundle\Model\ID;
18
use Ynlo\GraphQLBundle\Model\NodeInterface;
19
use Ynlo\GraphQLBundle\Validator\ConstraintViolationList;
20
21
/**
22
 * Class DeleteBatchNodeMutation
23
 */
24
class DeleteBatchNode extends AbstractMutationResolver
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function process(&$data)
30
    {
31
        foreach ($data['ids'] as $item) {
32
            $this->preDelete($item);
33
            foreach ($this->extensions as $extension) {
34
                $extension->preDelete($item, $this, $this->context);
35
            }
36
37
            $this->getManager()->remove($item);
38
        }
39
40
        $this->getManager()->flush();
41
42
        foreach ($data['ids'] as $item) {
43
            $this->postDelete($item);
44
            foreach ($this->extensions as $extension) {
45
                $extension->postDelete($item, $this, $this->context);
46
            }
47
        }
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function returnPayload($data, ConstraintViolationList $violations, $inputSource)
54
    {
55
        $ids = [];
56
        foreach ($inputSource['ids'] as $id) {
57
            $ids[] = ID::createFromString($id);
58
        }
59
60
        return new DeleteBatchNodePayload($ids, $inputSource['clientMutationId'] ?? null);
61
    }
62
63
    /**
64
     * {@inheritDoc}
65
     */
66
    public function onSubmit(FormEvent $event)
67
    {
68
        $node = $this->context->getDefinition()->getNode();
69
        $class = $this->context->getEndpoint()->getClassForType($node);
70
71
        if ($event->getForm()->get('ids') && is_array($event->getForm()->get('ids')->getData())) {
72
            foreach ($event->getForm()->get('ids')->getData() as $node) {
73
                if (!$node instanceof NodeInterface || !$node->getId() || !is_a($node, $class)) {
74
                    throw new NodeNotFoundException();
75
                }
76
            }
77
        } else {
78
            throw new UserError('Batch error, invalid data');
79
        }
80
    }
81
82
    /**
83
     * @param NodeInterface $node
84
     */
85
    protected function preDelete(NodeInterface $node)
0 ignored issues
show
Unused Code introduced by
The parameter $node is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

85
    protected function preDelete(/** @scrutinizer ignore-unused */ NodeInterface $node)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
86
    {
87
        //override
88
    }
89
90
    /**
91
     * @param NodeInterface $node
92
     */
93
    protected function postDelete(NodeInterface $node)
0 ignored issues
show
Unused Code introduced by
The parameter $node is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

93
    protected function postDelete(/** @scrutinizer ignore-unused */ NodeInterface $node)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
94
    {
95
        //override
96
    }
97
}
98