Completed
Push — master ( 600451...ae1e12 )
by
unknown
13s
created

SortBlockMutationCreator::resolve()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 4
dl 0
loc 20
rs 9.9
c 0
b 0
f 0
1
<?php
2
namespace DNADesign\Elemental\GraphQL;
3
4
use DNADesign\Elemental\Models\BaseElement;
5
use DNADesign\Elemental\Services\ReorderElements;
6
use GraphQL\Type\Definition\ResolveInfo;
0 ignored issues
show
Bug introduced by Raissa North
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...
7
use GraphQL\Type\Definition\Type;
0 ignored issues
show
Bug introduced by Raissa North
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...
8
use SilverStripe\Core\Convert;
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\GraphQL\MutationCreator;
0 ignored issues
show
Bug introduced by Raissa North
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...
11
use SilverStripe\GraphQL\OperationResolver;
0 ignored issues
show
Bug introduced by Raissa North
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...
12
use SilverStripe\ORM\DataObject;
13
use SilverStripe\ORM\Queries\SQLUpdate;
14
15
/**
16
 * Given a source block ID and the ID of the block to reorder the source block after, update all affected sort
17
 * orders for the block and its siblings. Only the source block will have a new version written, all siblings
18
 * will be updated underneath the ORM to avoid this.
19
 */
20
class SortBlockMutationCreator extends MutationCreator implements OperationResolver
21
{
22
    public function attributes()
23
    {
24
        return [
25
            'name' => 'sortBlock',
26
            'description' => 'Changes the sort position of an element'
27
        ];
28
    }
29
30
    public function type()
31
    {
32
        return $this->manager->getType('Block');
33
    }
34
35
    public function args()
36
    {
37
        return [
38
            'ID' => ['type' => Type::nonNull(Type::id())],
39
            'AfterBlockID' => ['type' => Type::nonNull(Type::id())],
40
        ];
41
    }
42
43
    public function resolve($object, array $args, $context, ResolveInfo $info)
44
    {
45
        $element = BaseElement::get()->byID($args['ID']);
46
        
47
        if (!$element) {
0 ignored issues
show
introduced by Dylan Wagstaff
$element is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
48
            throw new InvalidArgumentException(sprintf(
0 ignored issues
show
Bug introduced by Dylan Wagstaff
The type DNADesign\Elemental\Grap...nvalidArgumentException was not found. Did you mean InvalidArgumentException? If so, make sure to prefix the type with \.
Loading history...
49
                '%s#%s not found',
50
                BaseElement::class,
51
                $args['ID']
52
            ));
53
        }
54
55
        if (!$element->canEdit($context['currentUser'])) {
56
            throw new InvalidArgumentException(
57
                'Changing the sort order of blocks is not allowed for the current user'
58
            );
59
        }
60
61
        $reorderingService = Injector::inst()->create(ReorderElements::class, $element);
62
        return $reorderingService->reorder($args['AfterBlockID']);
63
    }
64
}
65