DeleteBatchNode::process()   A
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 18
ccs 0
cts 12
cp 0
rs 9.6111
cc 5
nc 9
nop 1
crap 30
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);
0 ignored issues
show
Bug introduced by
It seems like $node can also be of type null; however, parameter $type of Ynlo\GraphQLBundle\Defin...oint::getClassForType() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

70
        $class = $this->context->getEndpoint()->getClassForType(/** @scrutinizer ignore-type */ $node);
Loading history...
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)
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

86
    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...
87
    {
88
        //override
89
    }
90
91
    /**
92
     * @param NodeInterface $node
93
     */
94
    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

94
    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...
95
    {
96
        //override
97
    }
98
}
99