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

UnpublishFileMutationCreator::args()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\AssetAdmin\GraphQL;
4
5
use SilverStripe\Assets\File;
6
use SilverStripe\Versioned\Versioned;
7
use SilverStripe\GraphQL\MutationCreator;
8
use SilverStripe\GraphQL\OperationResolver;
9
use GraphQL\Type\Definition\ResolveInfo;
10
use GraphQL\Type\Definition\Type;
11
12 View Code Duplication
class UnpublishFileMutationCreator extends MutationCreator implements OperationResolver
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

Loading history...
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