Completed
Push — master ( 121747...dcf58a )
by Alexandr
02:37
created

Connection::connectionArgs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
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\Relay\Type\PageInfoType;
12
use Youshido\GraphQL\Type\AbstractType;
13
use Youshido\GraphQL\Type\ListType\ListType;
14
use Youshido\GraphQL\Type\NonNullType;
15
use Youshido\GraphQL\Type\Object\ObjectType;
16
use Youshido\GraphQL\Type\TypeMap;
17
18
class Connection
19
{
20
21 1
    public static function connectionArgs()
22
    {
23 1
        return array_merge(self::forwardArgs(), self::backwardArgs());
24
    }
25
26 1
    public static function forwardArgs()
27
    {
28
        return [
29 1
            'after' => ['type' => TypeMap::TYPE_STRING],
30
            'first' => ['type' => TypeMap::TYPE_INT]
31
        ];
32
    }
33
34 1
    public static function backwardArgs()
35
    {
36
        return [
37 1
            'before' => ['type' => TypeMap::TYPE_STRING],
38
            'last'   => ['type' => TypeMap::TYPE_INT]
39
        ];
40
    }
41
42
    /**
43
     * @param AbstractType $type
44
     * @param null|string  $name
45
     * @param array        $config
46
     * @option string  edgeFields
47
     *
48
     * @return ObjectType
49
     */
50 2
    public static function edgeDefinition(AbstractType $type, $name = null, $config = [])
51
    {
52 2
        $name       = $name ?: $type->getName();
53 2
        $edgeFields = !empty($config['edgeFields']) ? $config['edgeFields'] : [];
54
55 2
        $edgeType = new ObjectType([
56 2
            'name'        => $name . 'Edge',
57 2
            'description' => 'An edge in a connection.',
58 2
            'fields'      => array_merge([
59 2
                'node'   => [
60 2
                    'type'        => $type,
61 2
                    'description' => 'The item at the end of the edge',
62
                    'resolve'     => [__CLASS__, 'getNode'],
63
                ],
64
                'cursor' => [
65
                    'type'        => TypeMap::TYPE_STRING,
66
                    'description' => 'A cursor for use in pagination'
67
                ]
68 2
            ], $edgeFields)
69
        ]);
70
71 2
        return $edgeType;
72
    }
73
74
    /**
75
     * @param AbstractType $type
76
     * @param null|string  $name
77
     * @param array        $config
78
     * @option string  connectionFields
79
     *
80
     * @return ObjectType
81
     */
82 1
    public static function connectionDefinition(AbstractType $type, $name = null, $config = [])
83
    {
84 1
        $name             = $name ?: $type->getName();
85 1
        $connectionFields = !empty($config['connectionFields']) ? $config['connectionFields'] : [];
86
87 1
        $connectionType = new ObjectType([
88 1
            'name'        => $name . 'Connection',
89 1
            'description' => 'A connection to a list of items.',
90 1
            'fields'      => array_merge([
91 1
                'pageInfo' => [
92 1
                    'type'        => new NonNullType(new PageInfoType()),
93 1
                    'description' => 'Information to aid in pagination.',
94
                    'resolve'     => [__CLASS__, 'getPageInfo'],
95
                ],
96
                'edges'    => [
97 1
                    'type'        => new ListType(self::edgeDefinition($type, $name, $config)),
98 1
                    'description' => 'A list of edges.',
99
                    'resolve'     => [__CLASS__, 'getEdges'],
100
                ]
101 1
            ], $connectionFields)
102
        ]);
103
104 1
        return $connectionType;
105
    }
106
107
    public static function getEdges($value)
108
    {
109
        return isset($value['edges']) ? $value['edges'] : null;
110
    }
111
112
    public static function getPageInfo($value)
113
    {
114
        return isset($value['pageInfo']) ? $value['pageInfo'] : null;
115
    }
116
117
    public static function getNode($value)
118
    {
119
        return isset($value['node']) ? $value['node'] : null;
120
    }
121
}
122