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

AddElementToAreaMutation::type()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace DNADesign\Elemental\GraphQL;
4
5
use DNADesign\Elemental\Models\BaseElement;
6
use DNADesign\Elemental\Models\ElementalArea;
7
use DNADesign\Elemental\Services\ReorderElements;
8
use GraphQL\Type\Definition\ResolveInfo;
0 ignored issues
show
Bug introduced by Dylan Wagstaff
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...
9
use GraphQL\Type\Definition\Type;
0 ignored issues
show
Bug introduced by Dylan Wagstaff
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...
10
use InvalidArgumentException;
11
use SilverStripe\Core\Injector\Injector;
12
use SilverStripe\GraphQL\MutationCreator;
0 ignored issues
show
Bug introduced by Dylan Wagstaff
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...
13
use SilverStripe\GraphQL\OperationResolver;
0 ignored issues
show
Bug introduced by Dylan Wagstaff
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...
14
15
class AddElementToAreaMutation extends MutationCreator implements OperationResolver
16
{
17
    public function attributes()
18
    {
19
        return [
20
            'name' => 'addElementToArea',
21
            'description' => 'Adds an Element to an ElementalArea, optionally after another Element'
22
        ];
23
    }
24
25
    public function type()
26
    {
27
        return $this->manager->getType('Block');
28
    }
29
30
    public function args()
31
    {
32
        return [
33
            'ClassName' => ['type' => Type::nonNull(Type::string())],
34
            'ElementalAreaID' => ['type' => Type::nonNull(Type::id())],
35
            'AfterElementID' => ['type' => Type::id()],
36
        ];
37
    }
38
39
    public function resolve($object, array $args, $context, ResolveInfo $info)
40
    {
41
        $elementClass = $args['ClassName'];
42
        $elementalAreaID = $args['ElementalAreaID'];
43
        $afterElementID = isset($args['AfterElementID']) ? $args['AfterElementID'] : null;
44
45
        if (!is_subclass_of($elementClass, BaseElement::class)) {
46
            throw new InvalidArgumentException("$elementClass is not a subclass of " . BaseElement::class);
47
        }
48
49
        $elementalArea = ElementalArea::get()->byID($elementalAreaID);
50
51
        if (!$elementalArea) {
0 ignored issues
show
introduced by Dylan Wagstaff
$elementalArea is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
52
            throw new InvalidArgumentException("Invalid ElementalAreaID: $elementalAreaID");
53
        }
54
55
        if (!$elementalArea->canEdit($context['currentUser'])) {
56
            throw new InvalidArgumentException("The current user has insufficient permission to edit ElementalAreas");
57
        }
58
59
        $newElement = Injector::inst()->create($elementClass);
60
61
        if (!$newElement->canEdit($context['currentUser'])) {
62
            throw new InvalidArgumentException(
63
                'The current user has insufficient permission to edit Elements'
64
            );
65
        }
66
67
        $elementalArea->Elements()->add($newElement);
68
69
        if (!is_null($afterElementID)) {
70
            $reorderer = Injector::inst()->create(ReorderElements::class, $newElement);
71
            $reorderer->reorder($afterElementID);
72
        }
73
74
        return $newElement;
75
    }
76
}
77