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

ReadFileQueryCreator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 90
Duplicated Lines 10 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 11
dl 9
loc 90
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A attributes() 0 6 1
B connection() 9 33 3
D resolveConnection() 0 45 9

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Versioning\Versioned;
12
13
class ReadFileQueryCreator extends PaginatedQueryCreator
14
{
15
16
    public function attributes()
17
    {
18
        return [
19
            'name' => 'readFiles'
20
        ];
21
    }
22
23
    public function connection()
24
    {
25
        return Connection::create('readFiles')
26
            ->setConnectionType(function () {
27
                $unionType = new UnionType([
28
                    'name' => 'Result',
29
                    'types' => [
30
                        $this->manager->getType('File'),
31
                        $this->manager->getType('Folder')
32
                    ],
33 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...
34
                        if ($object instanceof Folder) {
35
                            return $this->manager->getType('Folder');
36
                        }
37
                        if ($object instanceof File) {
38
                            return $this->manager->getType('File');
39
                        }
40
                        return null;
41
                    }
42
                ]);
43
                return $unionType;
44
            })
45
            ->setArgs([
46
                'id' => [
47
                    'type' => Type::id(),
48
                ],
49
                'parentId' => [
50
                    'type' => Type::id(),
51
                ],
52
            ])
53
            ->setSortableFields(['ID', 'Title', 'Created', 'LastEdited'])
54
            ->setConnectionResolver(array($this, 'resolveConnection'));
55
    }
56
57
    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...
58
    {
59
        // Permission checks
60
        $parent = Folder::singleton();
61
        if (isset($args['parentId'])) {
62
            $parent = Folder::get()->byID($args['parentId']);
63
            if (!$parent) {
64
                throw new \InvalidArgumentException(sprintf(
65
                    '%s#%s not found',
66
                    Folder::class,
67
                    $args['parentId']
68
                ));
69
            }
70
        }
71
        if (!$parent->canView($context['currentUser'])) {
72
            throw new \InvalidArgumentException(sprintf(
73
                '%s#%s view access not permitted',
74
                Folder::class,
75
                $parent->ID
76
            ));
77
        }
78
79
        $list = Versioned::get_by_stage(File::class, Versioned::DRAFT);
80
81
        if (isset($args['parentId'])) {
82
            $list = $list->filter('ParentID', $args['parentId']);
83
        }
84
85
        if (isset($args['id']) && (int)$args['id'] > 0) {
86
            $list = $list->filter('ID', $args['id']);
87
        } elseif (isset($args['id']) && (int)$args['id'] === 0) {
88
            // Special case for root folder
89
            $list = new ArrayList([new Folder([
90
                'ID' => 0,
91
            ])]);
92
        }
93
94
        $list = $list->filterByCallback(function (File $file) use ($context) {
95
            return $file->canView($context['currentUser']);
96
        });
97
98
        // TODO Sorting
99
100
        return $list;
101
    }
102
}
103