manipulateFieldDefinition()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
namespace BBSLab\NovaTranslation\GraphQL\Directives;
4
5
use GraphQL\Language\AST\FieldDefinitionNode;
6
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
7
use GraphQL\Type\Definition\ResolveInfo;
8
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
9
use Laravel\Scout\Builder as ScoutBuilder;
10
use Nuwave\Lighthouse\Pagination\PaginateDirective;
11
use Nuwave\Lighthouse\Pagination\PaginationArgs;
12
use Nuwave\Lighthouse\Pagination\PaginationManipulator;
13
use Nuwave\Lighthouse\Pagination\PaginationType;
14
use Nuwave\Lighthouse\Schema\AST\DocumentAST;
15
use Nuwave\Lighthouse\Schema\Values\FieldValue;
16
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
17
18
class PaginateTranslationsDirective extends PaginateDirective
19
{
20
    use Traits\LocaleFilters;
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function name(): string
26
    {
27
        return 'paginateTranslations';
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public static function definition(): string
34
    {
35
        return /* @lang GraphQL */ <<<'SDL'
36
directive @paginateTranslations(
37
  "Specify the class name of the model to use."
38
  model: String
39
  "Specify the GraphQL type to add the 'locale' field (if GraphQL type is different from model class basename)."
40
  type: String
41
  "Which pagination style to use. Allowed values: paginator, connection."
42
  paginatorType: String = "paginator"
43
  "Apply scopes to the underlying query."
44
  scopes: [String!]
45
  "Overwrite the paginate_max_count setting value to limit the amount of items that a user can request per page."
46
  maxCount: Int
47
  "Use a default value for the amount of returned items in case the client does not request it explicitly?."
48
  defaultCount: Int
49
) on FIELD_DEFINITION
50
SDL;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function manipulateFieldDefinition(DocumentAST &$documentAST, FieldDefinitionNode &$fieldDefinition, ObjectTypeDefinitionNode &$parentType): void
57
    {
58
        $paginationManipulator = new PaginationManipulator($documentAST);
59
60
        $paginationManipulator
61
            ->setModelClass($this->getModelClass())
62
            ->transformToPaginatedField(
63
                $this->paginationType(),
64
                $fieldDefinition,
65
                $parentType,
66
                $this->directiveArgValue('defaultCount'),
67
                $this->paginateMaxCount()
68
            );
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function resolveField(FieldValue $fieldValue): FieldValue
75
    {
76
        return $fieldValue->setResolver(
77
            function ($root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): LengthAwarePaginator {
78
                [$first, $page] = PaginationArgs::extractArgs($args, $this->paginationType(), $this->paginateMaxCount());
0 ignored issues
show
Bug introduced by
The variable $first does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $page does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
79
80
                $query = $resolveInfo
0 ignored issues
show
Bug introduced by
The property argumentSet does not seem to exist in GraphQL\Type\Definition\ResolveInfo.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
81
                    ->argumentSet
82
                    ->enhanceBuilder(
83
                        $this->localeFilters($this->getModelClass(), $args),
84
                        $this->directiveArgValue('scopes', [])
85
                    );
86
87
                if ($query instanceof ScoutBuilder) {
0 ignored issues
show
Bug introduced by
The class Laravel\Scout\Builder does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
88
                    return $query->paginate($first, 'page', $page);
89
                }
90
91
                return $query->paginate($first, ['*'], 'page', $page);
92
            }
93
        );
94
    }
95
96
    protected function paginationType(): PaginationType
97
    {
98
        return new PaginationType(
99
            $this->directiveArgValue('paginatorType', PaginationType::PAGINATOR)
100
        );
101
    }
102
}
103