Completed
Pull Request — master (#330)
by Ingo
02:13
created

ReadFileQueryCreator::resolveConnection()   C

Complexity

Conditions 8
Paths 14

Size

Total Lines 42
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 42
rs 5.3846
cc 8
eloc 24
nc 14
nop 4
1
<?php
2
namespace SilverStripe\AssetAdmin\GraphQL;
3
4
use SilverStripe\Assets\File;
5
use SilverStripe\Assets\Folder;
6
use GraphQL\Type\Definition\UnionType;
7
use SilverStripe\GraphQL\Pagination\PaginatedQueryCreator;
8
use SilverStripe\GraphQL\Pagination\Connection;
9
use SilverStripe\ORM\Versioning\Versioned;
10
11
class ReadFileQueryCreator extends PaginatedQueryCreator
12
{
13
14
    public function attributes()
15
    {
16
        return [
17
            'name' => 'readFiles'
18
        ];
19
    }
20
21
    public function connection()
22
    {
23
        return Connection::create('readFiles')
24
            ->setConnectionType(function () {
25
                $unionType = new UnionType([
26
                    'name' => 'Result',
27
                    'types' => [
28
                        $this->manager->getType('File'),
29
                        $this->manager->getType('Folder')
30
                    ],
31 View Code Duplication
                    'resolveType' => function ($object) {
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...
32
                        if ($object instanceof Folder) {
33
                            return $this->manager->getType('Folder');
34
                        }
35
                        if ($object instanceof File) {
36
                            return $this->manager->getType('File');
37
                        }
38
                        return null;
39
                    }
40
                ]);
41
                return $unionType;
42
            })
43
            ->setArgs(function () {
44
                return [
45
                    'filter' => [
46
                        'type' => $this->manager->getType('FileFilterInput')
47
                    ]
48
                ];
49
            })
50
            ->setSortableFields(['ID', 'Title', 'Created', 'LastEdited'])
51
            ->setConnectionResolver(array($this, 'resolveConnection'));
52
    }
53
54
    public function resolveConnection($object, array $args, $context, $info)
0 ignored issues
show
Unused Code introduced by
The parameter $info is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
    {
56
        $filter = (!empty($args['filter'])) ? $args['filter'] : [];
57
58
        // Permission checks
59
        $parent = Folder::singleton();
60
        if (isset($filter['parentId']) && $filter['parentId'] !== 0) {
61
            $parent = Folder::get()->byID($filter['parentId']);
62
            if (!$parent) {
63
                throw new \InvalidArgumentException(sprintf(
64
                    '%s#%s not found',
65
                    Folder::class,
66
                    $filter['parentId']
67
                ));
68
            }
69
        }
70
        if (!$parent->canView($context['currentUser'])) {
71
            throw new \InvalidArgumentException(sprintf(
72
                '%s#%s view access not permitted',
73
                Folder::class,
74
                $parent->ID
75
            ));
76
        }
77
78
        if (isset($filter['recursive']) && $filter['recursive']) {
79
            throw new \InvalidArgumentException((
80
               'The "recursive" flag can only be used for the "children" field'
81
            ));
82
        }
83
84
        // Filter list
85
        $list = Versioned::get_by_stage(File::class, Versioned::DRAFT);
86
        $filterInputType = new FileFilterInputTypeCreator($this->manager);
87
        $list = $filterInputType->filterList($list, $filter);
88
89
        // Permission checks
90
        $list = $list->filterByCallback(function (File $file) use ($context) {
91
            return $file->canView($context['currentUser']);
92
        });
93
94
        return $list;
95
    }
96
}
97