Passed
Pull Request — master (#380)
by
unknown
02:50
created

SortBlockMutationCreator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 82
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
B resolve() 0 59 6
A attributes() 0 5 1
A args() 0 5 1
A type() 0 3 1
1
<?php
2
namespace DNADesign\Elemental\GraphQL;
3
4
use DNADesign\Elemental\Models\BaseElement;
5
use GraphQL\Type\Definition\ResolveInfo;
0 ignored issues
show
Bug introduced by
The type GraphQL\Type\Definition\ResolveInfo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use GraphQL\Type\Definition\Type;
0 ignored issues
show
Bug introduced by
The type GraphQL\Type\Definition\Type was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use SilverStripe\GraphQL\MutationCreator;
0 ignored issues
show
Bug introduced by
The type SilverStripe\GraphQL\MutationCreator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use SilverStripe\GraphQL\OperationResolver;
0 ignored issues
show
Bug introduced by
The type SilverStripe\GraphQL\OperationResolver was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use SilverStripe\ORM\DB;
10
11
class SortBlockMutationCreator extends MutationCreator implements OperationResolver
12
{
13
    public function attributes()
14
    {
15
        return [
16
            'name' => 'sortBlock',
17
            'description' => 'Changes the sort position of an element'
18
        ];
19
    }
20
21
    public function type()
22
    {
23
        return $this->manager->getType('Block');
24
    }
25
26
    public function args()
27
    {
28
        return [
29
            'ID' => ['type' => Type::nonNull(Type::id())],
30
            'AfterBlockID' => ['type' => Type::nonNull(Type::id())],
31
        ];
32
    }
33
34
    public function resolve($object, array $args, $context, ResolveInfo $info)
35
    {
36
        $block = BaseElement::get_by_id($args['ID']);
37
38
        if (!$block) {
0 ignored issues
show
introduced by
$block is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
39
            throw new \InvalidArgumentException(sprintf(
40
                '%s# not found',
41
                ElementArea::class,
0 ignored issues
show
Bug introduced by
The type DNADesign\Elemental\GraphQL\ElementArea was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
42
                $args['ID']
43
            ));
44
        }
45
46
        $parentId = $block->ParentID;
47
        $blockPosition = $block->Sort;
48
49
        $sortAfterPosition = 0;
50
        $afterBlockID = $args['AfterBlockID'];
51
        if ($afterBlockID) {
52
            $afterBlock = BaseElement::get_by_id($afterBlockID);
53
54
            if (!$afterBlock) {
0 ignored issues
show
introduced by
$afterBlock is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
55
                throw new \InvalidArgumentException(sprintf(
56
                    '%s#%s not found',
57
                    BaseElement::class,
58
                    $afterBlockID
59
                ));
60
            }
61
            if ($afterBlock->ParentID !== $parentId) {
62
                throw new \InvalidArgumentException(sprintf(
63
                    '%s# not found',
64
                    BaseElement::class,
65
                    $parentId
66
                ));
67
            }
68
69
            $sortAfterPosition = $afterBlock->Sort;
70
        }
71
72
        if ($sortAfterPosition < $blockPosition) {
73
            $operator = '+';
74
            $filter = "Sort > $sortAfterPosition && Sort < $blockPosition";
75
            $newBlockPosition = $sortAfterPosition + 1;
76
        }
77
        else {
78
            $operator = '-';
79
            $filter = "Sort <= $sortAfterPosition && Sort > $blockPosition";
80
            $newBlockPosition = $sortAfterPosition;
81
        }
82
83
        DB::query("
84
            UPDATE Element
85
            SET Sort = (Sort $operator 1)
86
            WHERE $filter && ParentId = $parentId 
87
        ");
88
89
        $block->Sort = $newBlockPosition;
90
        $block->write();
91
92
        return $block;
93
    }
94
}
95