Completed
Pull Request — master (#56)
by Frédéric G.
03:07
created

Connection::connectionDefinition()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 24
ccs 13
cts 13
cp 1
rs 8.9713
c 1
b 0
f 0
cc 3
eloc 17
nc 4
nop 3
crap 3
1
<?php
2
/**
3
 * Date: 10.05.16
4
 *
5
 * @author Portey Vasil <[email protected]>
6
 */
7
8
namespace Youshido\GraphQL\Relay\Connection;
9
10
11
use Youshido\GraphQL\Type\AbstractType;
12
use Youshido\GraphQL\Type\ListType\ListType;
13
use Youshido\GraphQL\Type\NonNullType;
14
use Youshido\GraphQL\Type\Object\ObjectType;
15
use Youshido\GraphQL\Type\TypeMap;
16
17
class Connection
18
{
19
20 1
    public static function connectionArgs()
21
    {
22 1
        return array_merge(self::forwardArgs(), self::backwardArgs());
23
    }
24
25 1
    public static function forwardArgs()
26
    {
27
        return [
28 1
            'after' => ['type' => TypeMap::TYPE_STRING],
29
            'first' => ['type' => TypeMap::TYPE_INT]
30
        ];
31
    }
32
33 1
    public static function backwardArgs()
34
    {
35
        return [
36 1
            'before' => ['type' => TypeMap::TYPE_STRING],
37
            'last'   => ['type' => TypeMap::TYPE_INT]
38
        ];
39
    }
40
41
    /**
42
     * @param AbstractType $type
43
     * @param null|string  $name
44
     * @param array        $config
45
     * @option string  edgeFields
46
     *
47
     * @return ObjectType
48
     */
49 2
    public static function edgeDefinition(AbstractType $type, $name = null, $config = [])
50
    {
51 2
        $name       = $name ?: $type->getName();
52 2
        $edgeFields = !empty($config['edgeFields']) ? $config['edgeFields'] : [];
53
54 2
        $edgeType = new ObjectType([
55 2
            'name'        => $name . 'Edge',
56 2
            'description' => 'An edge in a connection.',
57 2
            'fields'      => array_merge([
58
                'node'   => [
59 2
                    'type'        => $type,
60 2
                    'description' => 'The item at the end of the edge',
61
                    'resolve'     => [__CLASS__, 'getNode'],
62 2
                ],
63
                'cursor' => [
64
                    'type'        => TypeMap::TYPE_STRING,
65
                    'description' => 'A cursor for use in pagination'
66
                ]
67
            ], $edgeFields)
68
        ]);
69
70 2
        return $edgeType;
71
    }
72
73
    /**
74
     * @param AbstractType $type
75
     * @param null|string  $name
76
     * @param array        $config
77
     * @option string  connectionFields
78
     *
79
     * @return ObjectType
80
     */
81 1
    public static function connectionDefinition(AbstractType $type, $name = null, $config = [])
82
    {
83 1
        $name             = $name ?: $type->getName();
84 1
        $connectionFields = !empty($config['connectionFields']) ? $config['connectionFields'] : [];
85
86 1
        $connectionType = new ObjectType([
87 1
            'name'        => $name . 'Connection',
88 1
            'description' => 'A connection to a list of items.',
89 1
            'fields'      => array_merge([
90
                'pageInfo' => [
91 1
                    'type'        => new NonNullType(self::getPageInfoType()),
92 1
                    'description' => 'Information to aid in pagination.',
93
                    'resolve'     => [__CLASS__, 'getPageInfo'],
94 1
                ],
95
                'edges'    => [
96 1
                    'type'        => new ListType(self::edgeDefinition($type, $name, $config)),
97 1
                    'description' => 'A list of edges.',
98
                    'resolve'     => [__CLASS__, 'getEdges'],
99
                ]
100
            ], $connectionFields)
101
        ]);
102
103 1
        return $connectionType;
104
    }
105
106 2
    public static function getPageInfoType()
107
    {
108 2
        return new ObjectType([
109 2
            'name'        => 'PageInfo',
110 2
            'description' => 'Information about pagination in a connection.',
111
            'fields'      => [
112
                'hasNextPage'     => [
113 2
                    'type'        => new NonNullType(TypeMap::TYPE_BOOLEAN),
114 2
                    'description' => 'When paginating forwards, are there more items?'
115
                ],
116
                'hasPreviousPage' => [
117 2
                    'type'        => new NonNullType(TypeMap::TYPE_BOOLEAN),
118 2
                    'description' => 'When paginating backwards, are there more items?'
119
                ],
120
                'startCursor'     => [
121
                    'type'        => TypeMap::TYPE_STRING,
122
                    'description' => 'When paginating backwards, the cursor to continue.'
123
                ],
124
                'endCursor'       => [
125
                    'type'        => TypeMap::TYPE_STRING,
126
                    'description' => 'When paginating forwards, the cursor to continue.'
127
                ],
128
            ]
129
        ]);
130
    }
131
132
    public static function getEdges($value)
133
    {
134
      return isset($value['edges']) ? $value['edges'] : null;
135
    }
136
137
    public static function getPageInfo($value) {
138
      return isset($value['pageInfo']) ? $value['pageInfo'] : null;
139
    }
140
141
    public static function getNode($value) {
142
      return isset($value['node']) ? $value['node'] : null;
143
    }
144
}
145