Completed
Pull Request — master (#385)
by
unknown
02:06
created

MoveFilesMutationCreator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

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 3
nc 1
nop 1
1
<?php
2
namespace SilverStripe\AssetAdmin\GraphQL;
3
4
use GraphQL\Type\Definition\Type;
5
use GraphQL\Type\Definition\ResolveInfo;
6
use SilverStripe\Assets\File;
7
use SilverStripe\Assets\Folder;
8
use SilverStripe\GraphQL\MutationCreator;
9
use SilverStripe\GraphQL\OperationResolver;
10
use SilverStripe\ORM\Versioning\Versioned;
11
use SilverStripe\GraphQL\Util\CaseInsensitiveFieldAccessor;
12
use SilverStripe\GraphQL\Manager;
13
14
class MoveFilesMutationCreator extends MutationCreator implements OperationResolver
15
{
16
    /**
17
     * @var CaseInsensitiveFieldAccessor
18
     */
19
    protected $accessor;
20
21
    public function __construct(Manager $manager = null)
22
    {
23
        $this->accessor = new CaseInsensitiveFieldAccessor();
24
25
        parent::__construct($manager);
26
    }
27
28
    public function attributes()
29
    {
30
        return [
31
            'name' => 'moveFiles',
32
        ];
33
    }
34
35
    public function type()
36
    {
37
        return function () {
38
            return $this->manager->getType('FileInterface');
39
        };
40
    }
41
42
    public function args()
43
    {
44
        return [
45
            'folderId' => [
46
                'type' => Type::nonNull(Type::id())
47
            ],
48
            'fileIds' => [
49
                'type' => Type::listOf(Type::id())
50
            ],
51
        ];
52
    }
53
54
    public function resolve($object, array $args, $context, ResolveInfo $info)
55
    {
56
        $folderId = (isset($args['folderId'])) ? $args['folderId'] : 0;
57
        
58
        if ($folderId) {
59
            /** @var Folder $folder */
60
            $folder = Versioned::get_by_stage(Folder::class, Versioned::DRAFT)
61
                ->byID($folderId);
62
            if (!$folder) {
63
                throw new \InvalidArgumentException(sprintf(
64
                    '%s#%s not found',
65
                    Folder::class,
66
                    $folderId
67
                ));
68
            }
69
    
70
            // Check permission
71
            if (!$folder->canEdit($context['currentUser'])) {
72
                throw new \InvalidArgumentException(sprintf(
73
                    '%s edit not allowed',
74
                    Folder::class
75
                ));
76
            }
77
        }
78
        $files = Versioned::get_by_stage(File::class, Versioned::DRAFT)
79
            ->byIDs($args['fileIds']);
80
        $errorFiles = [];
81
        
82
        foreach($files as $file) {
83
            if ($file->canEdit($context['currentUser'])) {
84
                $this->accessor->setValue($file, 'ParentID', $folderId);
85
                $file->writeToStage(Versioned::DRAFT);
86
            } else {
87
                $errorFiles[] = $file->ID;
88
            }
89
        }
90
        
91
        if ($errorFiles) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $errorFiles of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
92
            throw new \InvalidArgumentException(sprintf(
93
                '%s (%s) edit not allowed',
94
                File::class,
95
                implode(', ', $errorFiles)
96
            ));
97
        }
98
99
        if (!isset($folder)) {
100
            return Folder::singleton();
101
        }
102
        return $folder;
103
    }
104
}
105