Completed
Pull Request — master (#441)
by Michael
02:32
created

UnpublishFileMutationCreator::attributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\AssetAdmin\GraphQL;
4
5
use SilverStripe\Assets\File;
6
use GraphQL\Type\Definition\ResolveInfo;
7
use GraphQL\Type\Definition\Type;
8
use SilverStripe\GraphQL\MutationCreator;
9
use SilverStripe\GraphQL\OperationResolver;
10
11
class UnpublishFileMutationCreator extends MutationCreator implements OperationResolver
12
{
13
    public function attributes() {
14
        return [
15
            'name '=> 'unpublishFile',
16
        ];
17
    }
18
19
    public function type()
20
    {
21
        return Type::id();
22
    }
23
24
    public function args()
25
    {
26
        return [
27
            'id' => [
28
                'type' => Type::nonNull(Type::id()),
29
            ],
30
        ];
31
    }
32
33
    public function resolve($object, array $args, $context, ResolveInfo $info)
34
    {
35
        $file = File::get()->byId($args['id']);
36
        if(!$file) {
37
            throw new \InvalidArgumentException(sprintf(
38
                '%s#%s not found',
39
                File::class,
40
                $args['id']
41
            ));
42
        }
43
44
        if(!$file->isPublished()) {
45
            throw new \InvalidArgumentException(sprintf(
46
                '%s#%s not published',
47
                File::class,
48
                $args['id']
49
            ));
50
        }
51
52
        if(!$file->canPublish($context['currentUser'])) {
53
            throw new \InvalidArgumentException(sprintf(
54
                '%s#%s unpublish not allowed',
55
                File::class,
56
                $args['id']
57
            ));
58
        }
59
60
        $file->doUnpublish();
61
62
        return $args['id'];
63
    }
64
}
65