AbstractCreateEntityField::resolve()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\Lumen\GraphQL\Fields;
4
5
use Digia\Lumen\GraphQL\Models\ResolveContext;
6
use Digia\Lumen\GraphQL\Traits\ResolvesNodesTrait;
7
use Youshido\GraphQL\Config\Field\FieldConfig;
8
use Youshido\GraphQL\Execution\ResolveInfo;
9
use Youshido\GraphQL\Field\AbstractField;
10
use Youshido\GraphQL\Type\InputObject\AbstractInputObjectType;
11
use Youshido\GraphQL\Type\NonNullType;
12
13
/**
14
 * Class AbstractCreateEntityField
15
 * @package Digia\Lumen\GraphQL\Fields
16
 */
17
abstract class AbstractCreateEntityField extends AbstractField
18
{
19
20
    use ResolvesNodesTrait;
21
22
    /**
23
     * @return string
24
     */
25
    abstract protected function getEntityTypeName();
26
27
    /**
28
     * @return AbstractInputObjectType
29
     */
30
    abstract protected function getInputType();
31
32
    /**
33
     * @param ResolveContext $context
34
     * @return mixed
35
     */
36
    abstract protected function createEntity(ResolveContext $context);
37
38
    /**
39
     * @inheritdoc
40
     */
41
    public function getDescription()
42
    {
43
        return 'Creates a new entity.';
44
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49
    public function build(FieldConfig $config)
50
    {
51
        $config->addArgument($this->getEntityKey(), new NonNullType($this->getInputType()));
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function resolve($value, array $args, ResolveInfo $info)
58
    {
59
        $context = new ResolveContext($value, $args, $info);
60
61
        return $this->createEntity($context);
62
    }
63
64
    /**
65
     * @inheritdoc
66
     */
67
    protected function getEntityKey()
68
    {
69
        return camel_case($this->getEntityTypeName());
70
    }
71
72
    /**
73
     * @param ResolveContext $context
74
     * @return array
75
     */
76
    protected function getEntityProperties(ResolveContext $context)
77
    {
78
        return $context->getArgument($this->getEntityKey());
79
    }
80
}
81