Completed
Pull Request — master (#338)
by Ingo
02:03
created

CreateFolderMutationCreator::resolve()   C

Complexity

Conditions 7
Paths 26

Size

Total Lines 35
Code Lines 21

Duplication

Lines 35
Ratio 100 %

Importance

Changes 0
Metric Value
dl 35
loc 35
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 21
nc 26
nop 4
1
<?php
2
namespace SilverStripe\AssetAdmin\GraphQL;
3
4
use GraphQL\Type\Definition\ResolveInfo;
5
use SilverStripe\Assets\Folder;
6
use SilverStripe\GraphQL\MutationCreator;
7
use SilverStripe\GraphQL\OperationResolver;
8
use SilverStripe\ORM\Versioning\Versioned;
9
use SilverStripe\GraphQL\Util\CaseInsensitiveFieldAccessor;
10
use SilverStripe\GraphQL\Manager;
11
12 View Code Duplication
class CreateFolderMutationCreator extends MutationCreator implements OperationResolver
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
13
{
14
    /**
15
     * @var CaseInsensitiveFieldAccessor
16
     */
17
    protected $accessor;
18
19
    public function __construct(Manager $manager = null)
20
    {
21
        $this->accessor = new CaseInsensitiveFieldAccessor();
22
23
        parent::__construct($manager);
24
    }
25
26
    public function attributes()
27
    {
28
        return [
29
            'name' => 'createFolder',
30
        ];
31
    }
32
33
    public function type()
34
    {
35
        return function () {
0 ignored issues
show
Bug Best Practice introduced by
The return type of return function () { ...pe('FileInterface'); }; (Closure) is incompatible with the return type of the parent method SilverStripe\GraphQL\FieldCreator::type of type GraphQL\Type\Definition\Type|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
36
            return $this->manager->getType('FileInterface');
37
        };
38
    }
39
40
    public function args()
41
    {
42
        return [
43
            'folder' => [
44
                'type' => function () {
45
                    return $this->manager->getType('FolderInput');
46
                }
47
            ],
48
        ];
49
    }
50
51
    public function resolve($object, array $args, $context, ResolveInfo $info)
52
    {
53
        $parentID = isset($args['folder']['parentId']) ? intval($args['folder']['parentId']) : 0;
54
        if ($parentID) {
55
            $parent = Versioned::get_by_stage(Folder::class, Versioned::DRAFT)->byID($parentID);
56
            if (!$parent) {
57
                throw new \InvalidArgumentException(sprintf(
58
                    '%s#%s not found',
59
                    Folder::class,
60
                    $parentID
61
                ));
62
            }
63
        }
64
65
        // Check permission
66
        $canCreateContext = [];
67
        foreach ($args['folder'] as $name => $val) {
68
            $canCreateContext[$this->accessor->getObjectFieldName(Folder::singleton(), $name)] = $val;
0 ignored issues
show
Bug introduced by
The method getObjectFieldName() cannot be called from this context as it is declared protected in class SilverStripe\GraphQL\Uti...nsensitiveFieldAccessor.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
69
        }
70
        if (!Folder::singleton()->canCreate($context['currentUser'], $canCreateContext)) {
71
            throw new \InvalidArgumentException(sprintf(
72
                '%s create not allowed',
73
                Folder::class
74
            ));
75
        }
76
77
        $folder = Folder::create();
78
        foreach ($args['folder'] as $name => $val) {
79
            $this->accessor->setValue($folder, $name, $val);
80
        }
81
82
        $folder->writeToStage(Versioned::DRAFT);
83
84
        return $folder;
85
    }
86
}
87