DeleteNode::onSubmit()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 10
cc 4
nc 2
nop 1
crap 20
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);
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

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

78
    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...
79
    {
80
        //override
81
    }
82
83
    /**
84
     * @param NodeInterface $node
85
     */
86
    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

86
    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...
87
    {
88
        //override
89
    }
90
}
91