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

ReadFileQueryCreator::resolveConnection()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 27
rs 6.7272
cc 7
eloc 13
nc 12
nop 4
1
<?php
2
namespace SilverStripe\AssetAdmin\GraphQL;
3
4
use SilverStripe\Assets\File;
5
use SilverStripe\Assets\Folder;
6
use SilverStripe\ORM\ArrayList;
7
use GraphQL\Type\Definition\Type;
8
use GraphQL\Type\Definition\UnionType;
9
use SilverStripe\GraphQL\Pagination\PaginatedQueryCreator;
10
use SilverStripe\GraphQL\Pagination\Connection;
11
use SilverStripe\ORM\DataList;
12
use SilverStripe\ORM\Versioning\Versioned;
13
14
class ReadFileQueryCreator extends PaginatedQueryCreator
15
{
16
17
    public function attributes()
18
    {
19
        return [
20
            'name' => 'readFiles'
21
        ];
22
    }
23
24
    public function connection()
25
    {
26
        return Connection::create('readFiles')
27
            ->setConnectionType(function () {
28
                $unionType = new UnionType([
29
                    'name' => 'Result',
30
                    'types' => [
31
                        $this->manager->getType('File'),
32
                        $this->manager->getType('Folder')
33
                    ],
34 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...
35
                        if ($object instanceof Folder) {
36
                            return $this->manager->getType('Folder');
37
                        }
38
                        if ($object instanceof File) {
39
                            return $this->manager->getType('File');
40
                        }
41
                        return null;
42
                    }
43
                ]);
44
                return $unionType;
45
            })
46
            ->setArgs([
47
                'id' => [
48
                    'type' => Type::id(),
49
                ],
50
                'parentId' => [
51
                    'type' => Type::id(),
52
                ],
53
            ])
54
            ->setSortableFields(['ID', 'Title', 'Created', 'LastEdited'])
55
            ->setConnectionResolver(array($this, 'resolveConnection'));
56
    }
57
58
    public function resolveConnection($object, array $args, $context, $info)
0 ignored issues
show
Unused Code introduced by
The parameter $context 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...
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...
59
    {
60
        $list = Versioned::get_by_stage(File::class, Versioned::DRAFT);
61
62
        if (isset($args['parentId'])) {
63
            $list = $list->filter('ParentID', $args['parentId']);
64
        }
65
66
        if (isset($args['id']) && (int)$args['id'] > 0) {
67
            $list = $list->filter('ID', $args['id']);
68
        } elseif (isset($args['id']) && (int)$args['id'] === 0) {
69
            // Special case for root folder
70
            $list = new ArrayList([new Folder([
71
                'ID' => 0,
72
            ])]);
73
        }
74
75
        if ($list instanceof DataList) {
76
            $list = $list->filterByCallback(function ($file) {
77
                return $file->canView();
78
            });
79
        }
80
81
        // TODO Sorting
82
83
        return $list;
84
    }
85
}
86