Completed
Push — master ( 40b0e0...7fed89 )
by Rafael
04:30
created

DeleteNode::process()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 1
crap 3
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\Error\NodeNotFoundException;
15
use Ynlo\GraphQLBundle\Model\DeleteNodePayload;
16
use Ynlo\GraphQLBundle\Model\ID;
17
use Ynlo\GraphQLBundle\Model\NodeInterface;
18
use Ynlo\GraphQLBundle\Validator\ConstraintViolationList;
19
20
/**
21
 * Class DeleteNodeMutation
22
 */
23
class DeleteNode extends AbstractMutationResolver
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28 2
    public function process(&$data)
29
    {
30 2
        $this->preDelete($data);
31 2
        foreach ($this->extensions as $extension) {
32 1
            $extension->preDelete($data, $this, $this->context);
33
        }
34
35 2
        $this->getManager()->remove($data);
36 2
        $this->getManager()->flush();
37
38 2
        $this->postDelete($data);
39 2
        foreach ($this->extensions as $extension) {
40 1
            $extension->postDelete($data, $this, $this->context);
41
        }
42 2
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 2
    public function returnPayload($data, ConstraintViolationList $violations, $inputSource)
48
    {
49 2
        return new DeleteNodePayload(
50 2
            $inputSource['id'] ? ID::createFromString($inputSource['id']) : null,
0 ignored issues
show
Bug introduced by
It seems like $inputSource['id'] ? Ynl...putSource['id']) : null can also be of type null; however, parameter $id of Ynlo\GraphQLBundle\Model...ePayload::__construct() does only seem to accept Ynlo\GraphQLBundle\Model\ID, 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

50
            /** @scrutinizer ignore-type */ $inputSource['id'] ? ID::createFromString($inputSource['id']) : null,
Loading history...
51 2
            $inputSource['clientMutationId'] ?? null
52
        );
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     */
58 2
    public function onSubmit(FormEvent $event)
59
    {
60 2
        $node = $this->context->getDefinition()->getNode();
61 2
        $class = $this->context->getEndpoint()->getClassForType($node);
62
63 2
        if (!$event->getData() instanceof NodeInterface || !$event->getData()->getId() || !is_a($event->getData(), $class)) {
64
            throw new NodeNotFoundException();
65
        }
66 2
    }
67
68
    /**
69
     * @param NodeInterface $node
70
     */
71 2
    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

71
    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...
72
    {
73
        //override
74 2
    }
75
76
    /**
77
     * @param NodeInterface $node
78
     */
79 2
    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

79
    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...
80
    {
81
        //override
82 2
    }
83
}
84