Code Duplication    Length = 75-75 lines in 2 locations

code/GraphQL/CreateFileMutationCreator.php 1 location

@@ 16-90 (lines=75) @@
13
/**
14
 * @todo Allow file upload (https://github.com/silverstripe/silverstripe-graphql/issues/19)
15
 */
16
class CreateFileMutationCreator extends MutationCreator implements OperationResolver
17
{
18
    /**
19
     * @var CaseInsensitiveFieldAccessor
20
     */
21
    protected $accessor;
22
23
    public function __construct(Manager $manager = null)
24
    {
25
        $this->accessor = new CaseInsensitiveFieldAccessor();
26
27
        parent::__construct($manager);
28
    }
29
30
    public function attributes()
31
    {
32
        return [
33
            'name' => 'createFile',
34
            'description' => 'Creates files and images'
35
        ];
36
    }
37
38
    public function type()
39
    {
40
        return function () {
41
            return $this->manager->getType('FileInterface');
42
        };
43
    }
44
45
    public function args()
46
    {
47
        return [
48
            'file' => [
49
                'type' => function () {
50
                    return $this->manager->getType('FileInput');
51
                }
52
            ],
53
        ];
54
    }
55
56
    public function resolve($object, array $args, $context, ResolveInfo $info)
57
    {
58
        $parentID = isset($args['file']['parentId']) ? intval($args['file']['parentId']) : 0;
59
        if ($parentID) {
60
            $parent = Versioned::get_by_stage(Folder::class, Versioned::DRAFT)->byID($parentID);
61
            if (!$parent) {
62
                throw new \InvalidArgumentException(sprintf(
63
                    '%s#%s not found',
64
                    Folder::class,
65
                    $parentID
66
                ));
67
            }
68
        }
69
70
        $canCreateContext = [];
71
        foreach ($args['file'] as $name => $val) {
72
            $canCreateContext[$this->accessor->getObjectFieldName(File::singleton(), $name)] = $val;
73
        }
74
        if (!File::singleton()->canCreate($context['currentUser'], $canCreateContext)) {
75
            throw new \InvalidArgumentException(sprintf(
76
                '%s# create not allowed',
77
                File::class
78
            ));
79
        }
80
81
        $file = File::create();
82
        foreach ($args['file'] as $name => $val) {
83
            $this->accessor->setValue($file, $name, $val);
84
        }
85
86
        $file->writeToStage(Versioned::DRAFT);
87
88
        return $file;
89
    }
90
}
91

code/GraphQL/CreateFolderMutationCreator.php 1 location

@@ 12-86 (lines=75) @@
9
use SilverStripe\GraphQL\Util\CaseInsensitiveFieldAccessor;
10
use SilverStripe\GraphQL\Manager;
11
12
class CreateFolderMutationCreator extends MutationCreator implements OperationResolver
13
{
14
    /**
15
     * @var CaseInsensitiveFieldAccessor
16
     */
17
    protected $accessor;
18
19
    public function __construct(Manager $manager = null)
20
    {
21
        $this->accessor = new CaseInsensitiveFieldAccessor();
22
23
        parent::__construct($manager);
24
    }
25
26
    public function attributes()
27
    {
28
        return [
29
            'name' => 'createFolder',
30
        ];
31
    }
32
33
    public function type()
34
    {
35
        return function () {
36
            return $this->manager->getType('FileInterface');
37
        };
38
    }
39
40
    public function args()
41
    {
42
        return [
43
            'folder' => [
44
                'type' => function () {
45
                    return $this->manager->getType('FolderInput');
46
                }
47
            ],
48
        ];
49
    }
50
51
    public function resolve($object, array $args, $context, ResolveInfo $info)
52
    {
53
        $parentID = isset($args['folder']['parentId']) ? intval($args['folder']['parentId']) : 0;
54
        if ($parentID) {
55
            $parent = Versioned::get_by_stage(Folder::class, Versioned::DRAFT)->byID($parentID);
56
            if (!$parent) {
57
                throw new \InvalidArgumentException(sprintf(
58
                    '%s#%s not found',
59
                    Folder::class,
60
                    $parentID
61
                ));
62
            }
63
        }
64
65
        // Check permission
66
        $canCreateContext = [];
67
        foreach ($args['folder'] as $name => $val) {
68
            $canCreateContext[$this->accessor->getObjectFieldName(Folder::singleton(), $name)] = $val;
69
        }
70
        if (!Folder::singleton()->canCreate($context['currentUser'], $canCreateContext)) {
71
            throw new \InvalidArgumentException(sprintf(
72
                '%s create not allowed',
73
                Folder::class
74
            ));
75
        }
76
77
        $folder = Folder::create();
78
        foreach ($args['folder'] as $name => $val) {
79
            $this->accessor->setValue($folder, $name, $val);
80
        }
81
82
        $folder->writeToStage(Versioned::DRAFT);
83
84
        return $folder;
85
    }
86
}
87