1
|
|
|
<?php |
2
|
|
|
namespace SilverStripe\AssetAdmin\GraphQL; |
3
|
|
|
|
4
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
5
|
|
|
use SilverStripe\Assets\File; |
6
|
|
|
use SilverStripe\Assets\Folder; |
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
|
|
|
* @todo Allow file upload (https://github.com/silverstripe/silverstripe-graphql/issues/19) |
15
|
|
|
*/ |
16
|
|
View Code Duplication |
class CreateFileMutationCreator extends MutationCreator implements OperationResolver |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var CaseInsensitiveFieldAccessor |
20
|
|
|
*/ |
21
|
|
|
protected $accessor; |
22
|
|
|
|
23
|
|
|
public function __construct(Manager $manager = null) |
24
|
|
|
{ |
25
|
|
|
$this->accessor = new CaseInsensitiveFieldAccessor(); |
26
|
|
|
|
27
|
|
|
parent::__construct($manager); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function attributes() |
31
|
|
|
{ |
32
|
|
|
return [ |
33
|
|
|
'name' => 'createFile', |
34
|
|
|
'description' => 'Creates files and images' |
35
|
|
|
]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function type() |
39
|
|
|
{ |
40
|
|
|
return function () { |
|
|
|
|
41
|
|
|
return $this->manager->getType('FileInterface'); |
42
|
|
|
}; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function args() |
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
|
|
'file' => [ |
49
|
|
|
'type' => function () { |
50
|
|
|
return $this->manager->getType('FileInput'); |
51
|
|
|
} |
52
|
|
|
], |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function resolve($object, array $args, $context, ResolveInfo $info) |
57
|
|
|
{ |
58
|
|
|
$parentID = isset($args['file']['parentId']) ? intval($args['file']['parentId']) : 0; |
59
|
|
|
if ($parentID) { |
60
|
|
|
$parent = Versioned::get_by_stage(Folder::class, Versioned::DRAFT)->byID($parentID); |
61
|
|
|
if (!$parent) { |
62
|
|
|
throw new \InvalidArgumentException(sprintf( |
63
|
|
|
'%s#%s not found', |
64
|
|
|
Folder::class, |
65
|
|
|
$parentID |
66
|
|
|
)); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$canCreateContext = []; |
71
|
|
|
foreach ($args['file'] as $name => $val) { |
72
|
|
|
$canCreateContext[$this->accessor->getObjectFieldName(File::singleton(), $name)] = $val; |
|
|
|
|
73
|
|
|
} |
74
|
|
|
if (!File::singleton()->canCreate($context['currentUser'], $canCreateContext)) { |
75
|
|
|
throw new \InvalidArgumentException(sprintf( |
76
|
|
|
'%s# create not allowed', |
77
|
|
|
File::class |
78
|
|
|
)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$file = File::create(); |
82
|
|
|
foreach ($args['file'] as $name => $val) { |
83
|
|
|
$this->accessor->setValue($file, $name, $val); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$file->writeToStage(Versioned::DRAFT); |
87
|
|
|
|
88
|
|
|
return $file; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
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.