Completed
Push — 1.x ( cf9fb8...ac792b )
by David
10:37 queued 08:14
created

ResultIteratorType::build()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.439
c 0
b 0
f 0
cc 6
eloc 21
nc 1
nop 1
1
<?php
2
3
4
namespace TheCodingMachine\Tdbm\GraphQL;
5
6
use TheCodingMachine\TDBM\ResultIterator;
7
use Youshido\GraphQL\Config\Object\ObjectTypeConfig;
8
use Youshido\GraphQL\Type\ListType\ListType;
9
use Youshido\GraphQL\Type\NonNullType;
10
use Youshido\GraphQL\Type\Object\AbstractObjectType;
11
use Youshido\GraphQL\Type\Scalar\IntType;
12
use Youshido\GraphQL\Type\Scalar\StringType;
13
use Youshido\GraphQL\Type\TypeInterface;
14
15
/**
16
 * Type mapping a TDBM ResultIterator.
17
 * It allows easy pagination and sorting in the iterator.
18
 */
19
class ResultIteratorType extends AbstractObjectType
20
{
21
    /**
22
     * @var TypeInterface
23
     */
24
    private $beanType;
25
26
    public function __construct(TypeInterface $beanType)
27
    {
28
        parent::__construct([]);
29
        $this->beanType = $beanType;
30
    }
31
32
    /**
33
     * @param ObjectTypeConfig $config
34
     */
35
    public function build($config)
36
    {
37
        $config->addField('count', [
38
            'type' => new IntType(),
39
            'description' => 'Returns the total number of items in the collection.',
40
            'resolve' => function (ResultIterator $source, $args, $info) {
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 $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...
41
                return $source->count();
42
            }
43
        ]);
44
        $config->addField('items', [
45
            'type' => new NonNullType(new ListType(new NonNullType($this->beanType))),
0 ignored issues
show
Documentation introduced by
$this->beanType is of type object<Youshido\GraphQL\Type\TypeInterface>, but the function expects a object<Youshido\GraphQL\Type\AbstractType>|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
46
            'description' => 'Returns the list of items in the collection.',
47
            'args' => [
48
                'limit' => new IntType(),
49
                'offset' => new IntType(),
50
                'order' => new StringType(),
51
            ],
52
            'resolve' => function (ResultIterator $source, $args, $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...
53
                if (isset($args['order'])) {
54
                    $source = $source->withOrder($args['order']);
55
                }
56
                if (!isset($args['limit']) && isset($args['offset'])) {
57
                    throw new GraphQLException('In "items" field, you can specify an offset without a limit.');
58
                }
59
                if (isset($args['limit']) || isset($args['offset'])) {
60
                    $source = $source->take($args['offset'] ?? 0, $args['limit']);
61
                }
62
63
                return $source->toArray();
64
            }
65
        ]);
66
    }
67
}
68