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

UnpublishFileMutationCreator::attributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
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
class UnpublishFileMutationCreator extends MutationCreator implements OperationResolver
13
{
14
    public function attributes() {
15
        return [
16
            'name '=> 'unpublishFile',
17
            'description' => 'Unpublishes a file',
18
        ];
19
    }
20
21
    public function type()
22
    {
23
        return function () {
24
            return $this->manager->getType('FileInterface');
25
        };
26
    }
27
28
    public function args()
29
    {
30
        return [
31
            'id' => [
32
                'type' => Type::nonNull(Type::id()),
33
            ],
34
        ];
35
    }
36
37
    public function resolve($object, array $args, $context, ResolveInfo $info)
38
    {
39
        if(!isset($args['id']) || !ctype_digit($args['id'])) {
40
            throw new \InvalidArgumentException('Invalid id');
41
        }
42
43
        $file = Versioned::get_by_stage(File::class, Versioned::LIVE)
44
            ->byId($args['id']);
45
46
        if(!$file) {
47
            throw new \InvalidArgumentException(sprintf(
48
                '%s#%s not published or doesn\'t exist',
49
                File::class,
50
                $args['id']
51
            ));
52
        }
53
54 View Code Duplication
        if(!$file->canUnpublish($context['currentUser'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
55
            throw new \InvalidArgumentException(sprintf(
56
                '%s#%s unpublish not allowed',
57
                File::class,
58
                $args['id']
59
            ));
60
        }
61
62
        $file->doUnpublish();
63
64
        return $file;
65
    }
66
}
67