Completed
Push — master ( b7907c...708c0e )
by Alexandr
15:31 queued 11:30
created

Connection   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 80.95%

Importance

Changes 0
Metric Value
wmc 17
lcom 0
cbo 6
dl 0
loc 114
ccs 34
cts 42
cp 0.8095
rs 10
c 0
b 0
f 0

9 Methods

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