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

SortBlockMutationCreator::resolve()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 65
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 41
nc 8
nop 4
dl 0
loc 65
rs 8.3306
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\DataObject;
10
use SilverStripe\ORM\DB;
11
use SilverStripe\ORM\Queries\SQLUpdate;
12
13
class SortBlockMutationCreator extends MutationCreator implements OperationResolver
14
{
15
    public function attributes()
16
    {
17
        return [
18
            'name' => 'sortBlock',
19
            'description' => 'Changes the sort position of an element'
20
        ];
21
    }
22
23
    public function type()
24
    {
25
        return $this->manager->getType('Block');
26
    }
27
28
    public function args()
29
    {
30
        return [
31
            'ID' => ['type' => Type::nonNull(Type::id())],
32
            'AfterBlockID' => ['type' => Type::nonNull(Type::id())],
33
        ];
34
    }
35
36
    public function resolve($object, array $args, $context, ResolveInfo $info)
37
    {
38
        $block = BaseElement::get_by_id($args['ID']);
39
40
        if (!$block) {
0 ignored issues
show
introduced by
$block is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
41
            throw new \InvalidArgumentException(sprintf(
42
                '%s#%s not found',
43
                BaseElement::class,
44
                $args['ID']
45
            ));
46
        }
47
48
        if (!$block->canEdit($context['currentUser'])) {
49
            throw new \InvalidArgumentException(
50
                'Changing the sort order of blocks is not allowed'
51
            );
52
        }
53
54
        $parentId = $block->ParentID;
55
        $blockPosition = $block->Sort;
56
57
        $sortAfterPosition = 0;
58
        $afterBlockID = $args['AfterBlockID'];
59
        if ($afterBlockID) {
60
            $afterBlock = BaseElement::get_by_id($afterBlockID);
61
62
            if (!$afterBlock) {
0 ignored issues
show
introduced by
$afterBlock is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
63
                throw new \InvalidArgumentException(
64
                    'Trying to sort block after a block in a different elemental area'
65
                );
66
            }
67
            if ($afterBlock->ParentID !== $parentId) {
68
                throw new \InvalidArgumentException(sprintf(
69
                    '%s#%s not found',
70
                    BaseElement::class,
71
                    $parentId
72
                ));
73
            }
74
75
            $sortAfterPosition = $afterBlock->Sort;
76
        }
77
78
        if ($sortAfterPosition < $blockPosition) {
79
            $operator = '+';
80
            $filter = "Sort > $sortAfterPosition && Sort < $blockPosition";
81
            $newBlockPosition = $sortAfterPosition + 1;
82
        } else {
83
            $operator = '-';
84
            $filter = "Sort <= $sortAfterPosition && Sort > $blockPosition";
85
            $newBlockPosition = $sortAfterPosition;
86
        }
87
88
        $table = DataObject::getSchema()->tableName(BaseElement::class);
89
90
        $query = SQLUpdate::create()
91
           ->setTable($table)
92
           ->assignSQL('"' . $table . '"."Sort"', "\"$table\".\"Sort\" $operator 1")
93
           ->addWhere([$filter, '"ParentId"' => $parentId]);
94
95
        $query->execute();
96
97
        $block->Sort = $newBlockPosition;
98
        $block->write();
99
100
        return $block;
101
    }
102
}
103