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

UpdateFileMutationCreator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 2
cbo 7
dl 0
loc 75
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A attributes() 0 6 1
A type() 0 6 1
A args() 0 13 1
B resolve() 0 31 4
1
<?php
2
namespace SilverStripe\AssetAdmin\GraphQL;
3
4
use GraphQL\Type\Definition\ResolveInfo;
5
use SilverStripe\Assets\File;
6
use GraphQL\Type\Definition\Type;
7
use SilverStripe\GraphQL\MutationCreator;
8
use SilverStripe\GraphQL\OperationResolver;
9
use SilverStripe\ORM\Versioning\Versioned;
10
use SilverStripe\GraphQL\Util\CaseInsensitiveFieldAccessor;
11
use SilverStripe\GraphQL\Manager;
12
13
/**
14
 * Handles create and update
15
 */
16
class UpdateFileMutationCreator extends MutationCreator implements OperationResolver
17
{
18
19
    /**
20
     * @var CaseInsensitiveFieldAccessor
21
     */
22
    protected $accessor;
23
24
    public function __construct(Manager $manager = null)
25
    {
26
        $this->accessor = new CaseInsensitiveFieldAccessor();
27
28
        parent::__construct($manager);
29
    }
30
31
    public function attributes()
32
    {
33
        return [
34
            'name' => 'updateFile'
35
        ];
36
    }
37
38
    public function type()
39
    {
40
        return function () {
0 ignored issues
show
Bug Best Practice introduced by
The return type of return function () { ...er->getType('File'); }; (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...
41
            return $this->manager->getType('File');
42
        };
43
    }
44
45
    public function args()
46
    {
47
        return [
48
            'id' => [
49
                'type' => Type::nonNull(Type::id()),
50
            ],
51
            'file' => [
52
                'type' => function () {
53
                    return $this->manager->getType('FileInput');
54
                }
55
            ],
56
        ];
57
    }
58
59
    public function resolve($object, array $args, $context, ResolveInfo $info)
60
    {
61
        /** @var File $file */
62
        $file = Versioned::get_by_stage(File::class, Versioned::DRAFT)
63
            ->byID($args['id']);
64
65
        if (!$file) {
66
            throw new \InvalidArgumentException(sprintf(
67
                '%s#%s not found',
68
                File::class,
69
                $args['id']
70
            ));
71
        }
72
73
        if (!$file->canEdit()) {
74
            throw new \InvalidArgumentException(sprintf(
75
                '%s#%s update not allowed',
76
                File::class,
77
                $args['id']
78
            ));
79
        }
80
81
        // TODO Use input type (and don't allow setting ID)
82
        foreach ($args['file'] as $name => $val) {
83
            $this->accessor->setValue($file, $name, $val);
84
        }
85
        $file->update($args);
86
        $file->writeToStage(Versioned::DRAFT);
87
88
        return $file;
89
    }
90
}
91