AbstractEntityConnectionField   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 3 1
A getType() 0 3 1
A resolve() 0 11 1
1
<?php
2
3
namespace Digia\Lumen\GraphQL\Fields;
4
5
use Digia\Lumen\GraphQL\Models\Page;
6
use Digia\Lumen\GraphQL\Models\ResolveContext;
7
use Youshido\GraphQL\Config\Field\FieldConfig;
8
use Youshido\GraphQL\Execution\ResolveInfo;
9
use Youshido\GraphQL\Field\AbstractField;
10
use Youshido\GraphQL\Relay\Connection\ArrayConnection;
11
use Youshido\GraphQL\Relay\Connection\Connection;
12
use Youshido\GraphQL\Type\AbstractType;
13
14
/**
15
 * Class AbstractEntityConnectionField
16
 * @package Digia\Lumen\GraphQL\Fields
17
 */
18
abstract class AbstractEntityConnectionField extends AbstractField
19
{
20
21
    /**
22
     * @return AbstractType
23
     */
24
    abstract protected function getEntityType();
25
26
    /**
27
     * @param ResolveContext $context
28
     *
29
     * @return Page
30
     */
31
    abstract protected function createPage(ResolveContext $context);
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function getType()
37
    {
38
        return Connection::connectionDefinition($this->getEntityType());
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function build(FieldConfig $config)
45
    {
46
        $config->addArguments(Connection::connectionArgs());
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function resolve($value, array $args, ResolveInfo $info)
53
    {
54
        $context = new ResolveContext($value, $args, $info);
55
56
        $page = $this->createPage($context);
57
58
        return ArrayConnection::connectionFromArraySlice(
59
            $page->getData(),
60
            $context->getArguments(),
61
            $page->getFrom(),
62
            $page->getTotal()
63
        );
64
    }
65
}
66