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 () { |
|
|
|
|
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
|
|
View Code Duplication |
if (!$file->canEdit($context['currentUser'])) { |
|
|
|
|
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
|
|
|
|
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.