Completed
Pull Request — master (#338)
by Damian
02:44 queued 48s
created

ReadFileQueryCreator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 83
Duplicated Lines 10.84 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 12
c 0
b 0
f 0
lcom 1
cbo 10
dl 9
loc 83
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A attributes() 0 6 1
B connection() 9 33 3
C resolveConnection() 0 38 8

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
        }
64
        if (!$parent->canView($context['currentUser'])) {
65
            throw new \InvalidArgumentException(sprintf(
66
                '%s#%s view access not permitted',
67
                Folder::class,
68
                $parent->ID
69
            ));
70
        }
71
72
        $list = Versioned::get_by_stage(File::class, Versioned::DRAFT);
73
74
        if (isset($args['parentId'])) {
75
            $list = $list->filter('ParentID', $args['parentId']);
76
        }
77
78
        if (isset($args['id']) && (int)$args['id'] > 0) {
79
            $list = $list->filter('ID', $args['id']);
80
        } elseif (isset($args['id']) && (int)$args['id'] === 0) {
81
            // Special case for root folder
82
            $list = new ArrayList([new Folder([
83
                'ID' => 0,
84
            ])]);
85
        }
86
87
        $list = $list->filterByCallback(function (File $file) use ($context) {
88
                return $file->canView($context['currentUser']);
89
            });
90
91
        // TODO Sorting
92
93
        return $list;
94
    }
95
}
96