| @@ 15-63 (lines=49) @@ | ||
| 12 | /** |
|
| 13 | * Handles create and update |
|
| 14 | */ |
|
| 15 | class DeleteFileMutationCreator extends MutationCreator implements OperationResolver |
|
| 16 | { |
|
| 17 | ||
| 18 | public function attributes() |
|
| 19 | { |
|
| 20 | return [ |
|
| 21 | 'name' => 'deleteFile' |
|
| 22 | ]; |
|
| 23 | } |
|
| 24 | ||
| 25 | public function type() |
|
| 26 | { |
|
| 27 | return Type::id(); |
|
| 28 | } |
|
| 29 | ||
| 30 | public function args() |
|
| 31 | { |
|
| 32 | return [ |
|
| 33 | 'id' => [ |
|
| 34 | 'type' => Type::nonNull(Type::id()), |
|
| 35 | ], |
|
| 36 | ]; |
|
| 37 | } |
|
| 38 | ||
| 39 | public function resolve($object, array $args, $context, ResolveInfo $info) |
|
| 40 | { |
|
| 41 | /** @var File $file */ |
|
| 42 | $file = Versioned::get_by_stage(File::class, Versioned::DRAFT)->byID($args['id']); |
|
| 43 | if (!$file) { |
|
| 44 | throw new \InvalidArgumentException(sprintf( |
|
| 45 | '%s#%s not found', |
|
| 46 | File::class, |
|
| 47 | $args['id'] |
|
| 48 | )); |
|
| 49 | } |
|
| 50 | ||
| 51 | if (!$file->canArchive($context['currentUser'])) { |
|
| 52 | throw new \InvalidArgumentException(sprintf( |
|
| 53 | '%s#%s delete not allowed', |
|
| 54 | File::class, |
|
| 55 | $args['id'] |
|
| 56 | )); |
|
| 57 | } |
|
| 58 | ||
| 59 | $file->doArchive(); |
|
| 60 | ||
| 61 | return $args['id']; |
|
| 62 | } |
|
| 63 | } |
|
| 64 | ||
| @@ 12-58 (lines=47) @@ | ||
| 9 | use GraphQL\Type\Definition\ResolveInfo; |
|
| 10 | use GraphQL\Type\Definition\Type; |
|
| 11 | ||
| 12 | class UnpublishFileMutationCreator extends MutationCreator implements OperationResolver |
|
| 13 | { |
|
| 14 | public function attributes() { |
|
| 15 | return [ |
|
| 16 | 'name '=> 'unpublishFile', |
|
| 17 | ]; |
|
| 18 | } |
|
| 19 | ||
| 20 | public function type() |
|
| 21 | { |
|
| 22 | return Type::id(); |
|
| 23 | } |
|
| 24 | ||
| 25 | public function args() |
|
| 26 | { |
|
| 27 | return [ |
|
| 28 | 'id' => [ |
|
| 29 | 'type' => Type::nonNull(Type::id()), |
|
| 30 | ], |
|
| 31 | ]; |
|
| 32 | } |
|
| 33 | ||
| 34 | public function resolve($object, array $args, $context, ResolveInfo $info) |
|
| 35 | { |
|
| 36 | $file = Versioned::get_by_stage(File::class, Versioned::LIVE) |
|
| 37 | ->byId($args['id']); |
|
| 38 | ||
| 39 | if(!$file) { |
|
| 40 | throw new \InvalidArgumentException(sprintf( |
|
| 41 | '%s#%s not published or doesn\'t exist', |
|
| 42 | File::class, |
|
| 43 | $args['id'] |
|
| 44 | )); |
|
| 45 | } |
|
| 46 | ||
| 47 | if(!$file->canUnpublish($context['currentUser'])) { |
|
| 48 | throw new \InvalidArgumentException(sprintf( |
|
| 49 | '%s#%s unpublish not allowed', |
|
| 50 | File::class, |
|
| 51 | $args['id'] |
|
| 52 | )); |
|
| 53 | } |
|
| 54 | ||
| 55 | $file->doUnpublish(); |
|
| 56 | ||
| 57 | return $args['id']; |
|
| 58 | } |
|
| 59 | } |
|
| 60 | ||