Completed
Pull Request — master (#338)
by Ingo
02:03
created

DeleteFileMutationCreator::resolve()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 14

Duplication

Lines 7
Ratio 29.17 %

Importance

Changes 0
Metric Value
dl 7
loc 24
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 14
nc 3
nop 4
1
<?php
2
3
namespace SilverStripe\AssetAdmin\GraphQL;
4
5
use GraphQL\Type\Definition\ResolveInfo;
6
use SilverStripe\Assets\File;
7
use GraphQL\Type\Definition\Type;
8
use SilverStripe\GraphQL\MutationCreator;
9
use SilverStripe\GraphQL\OperationResolver;
10
use SilverStripe\ORM\Versioning\Versioned;
11
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 View Code Duplication
        if (!$file->canArchive($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...
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