ConnectionField   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 53
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A pageInfoResolver() 0 4 1
A edgeResolver() 0 34 4
1
<?php
2
3
namespace DutchCodingCompany\CursorPagination\Pagination;
4
5
use GraphQL\Type\Definition\ResolveInfo;
6
use Illuminate\Support\Collection;
7
use DutchCodingCompany\CursorPagination\CursorPaginator;
8
use Nuwave\Lighthouse\Pagination\Cursor;
9
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
10
11
class ConnectionField
12
{
13
    /**
14
     * Resolve page info for connection.
15
     *
16
     * @return array<string, mixed>
0 ignored issues
show
Documentation introduced by
The doc-type array<string, could not be parsed: Expected ">" at position 5, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
17
     */
18
    public function pageInfoResolver(CursorPaginator $paginator): array
19
    {
20
        return $paginator->toArray();
21
    }
22
23
    /**
24
     * Resolve edges for connection.
25
     *
26
     * @param  \Illuminate\Pagination\LengthAwarePaginator<mixed>  $paginator
0 ignored issues
show
Documentation introduced by
The doc-type \Illuminate\Pagination\L...thAwarePaginator<mixed> could not be parsed: Expected "|" or "end of type", but got "<" at position 43. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
27
     * @param  array<string, mixed>  $args
28
     */
29
    public function edgeResolver(CursorPaginator $paginator, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Collection
0 ignored issues
show
Unused Code introduced by
The parameter $args 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 $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...
30
    {
31
        // We know this must be a list, as it is constructed this way during schema manipulation
32
        /** @var \GraphQL\Type\Definition\ListOfType $listOfType */
33
        $listOfType = $resolveInfo->returnType;
34
35
        // We also know this is one of those two return types
36
        /** @var \GraphQL\Type\Definition\ObjectType|\GraphQL\Type\Definition\InterfaceType $objectLikeType */
37
        $objectLikeType = $listOfType->ofType;
38
        $returnTypeFields = $objectLikeType->getFields();
39
40
        // @phpstan-ignore-next-line static refers to the wrong class because it is a proxied method call
41
        return $paginator->values()
42
            ->map(function ($item, $index) use ($returnTypeFields): array {
0 ignored issues
show
Unused Code introduced by
The parameter $index 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...
43
                $data = [];
44
45
                foreach ($returnTypeFields as $field) {
46
                    switch ($field->name) {
47
                        case 'node':
48
                            $data['node'] = $item;
49
                            break;
50
51
                        default:
52
                            // All other fields on the return type are assumed to be part
53
                            // of the edge, so we try to locate them in the pivot attribute
54
                            if (isset($item->pivot->{$field->name})) {
55
                                $data[$field->name] = $item->pivot->{$field->name};
56
                            }
57
                    }
58
                }
59
60
                return $data;
61
            });
62
    }
63
}
64