Completed
Push — master ( 7a45d0...64e77b )
by Alexandr
03:12
created

AbstractField::__construct()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 8.8571
c 0
b 0
f 0
ccs 15
cts 15
cp 1
cc 5
eloc 11
nc 12
nop 1
crap 5
1
<?php
2
/**
3
 * Date: 13.05.16
4
 *
5
 * @author Portey Vasil <[email protected]>
6
 */
7
8
namespace Youshido\GraphQL\Field;
9
10
use Youshido\GraphQL\Config\Field\FieldConfig;
11
use Youshido\GraphQL\Execution\ResolveInfo;
12
use Youshido\GraphQL\Type\AbstractType;
13
use Youshido\GraphQL\Type\Object\AbstractObjectType;
14
use Youshido\GraphQL\Type\Traits\AutoNameTrait;
15
use Youshido\GraphQL\Type\Traits\FieldsArgumentsAwareObjectTrait;
16
use Youshido\GraphQL\Type\TypeFactory;
17
use Youshido\GraphQL\Type\TypeMap;
18
use Youshido\GraphQL\Type\TypeService;
19
20
abstract class AbstractField implements FieldInterface
21
{
22
23
    use FieldsArgumentsAwareObjectTrait;
24
    use AutoNameTrait {
25
        getName as getAutoName;
26
    }
27
    protected $isFinal = false;
28
29
    private $resolveFunctionCache = null;
30
    private $nameCache            = null;
31
32 70
    public function __construct(array $config = [])
33
    {
34 70
        if (empty($config['type'])) {
35 42
            $config['type'] = $this->getType();
36 42
            $config['name'] = $this->getName();
37 42
            if (empty($config['name'])) {
38 2
                $config['name'] =$this->getAutoName();
39 2
            }
40 42
        }
41
42 70
        if (TypeService::isScalarType($config['type'])) {
43 62
            $config['type'] = TypeFactory::getScalarType($config['type']);
44 62
        }
45 70
        $this->nameCache = isset($config['name']) ? $config['name'] : $this->getAutoName();
46
47 70
        $this->config = new FieldConfig($config, $this, $this->isFinal);
48 70
        $this->build($this->config);
49 70
    }
50
51
    /**
52
     * @return AbstractObjectType|AbstractType
53
     */
54
    abstract public function getType();
55
56 69
    public function build(FieldConfig $config)
57
    {
58 69
    }
59
60 1
    public function setType($type)
61
    {
62 1
        $this->getConfig()->set('type', $type);
63 1
    }
64
65 25
    public function resolve($value, array $args, ResolveInfo $info)
66
    {
67 25
        if ($this->resolveFunctionCache === null) {
68 25
            $this->resolveFunctionCache = $this->getConfig()->getResolveFunction();
69
70 25
            if (!$this->resolveFunctionCache) {
71 20
                $this->resolveFunctionCache = false;
72 20
            }
73 25
        }
74 25
        if ($this->resolveFunctionCache) {
75 24
            $resolveFunction = $this->resolveFunctionCache;
76
77 24
            return $resolveFunction($value, $args, $info);
78
        } else {
79 20
            if (is_array($value) && array_key_exists($this->getName(), $value)) {
80 14
                return $value[$this->getName()];
81 9
            } elseif (is_object($value)) {
82 6
                return TypeService::getPropertyValue($value, $this->getName());
83 3
            } elseif ($this->getType()->getNamedType()->getKind() == TypeMap::KIND_SCALAR) {
84 3
                return null;
85
            } else {
86
                throw new \Exception(sprintf('Property "%s" not found in resolve result', $this->getName()));
87
            }
88
        }
89
    }
90
91 2
    public function getName()
92
    {
93 2
        return $this->nameCache;
94
    }
95
96 3
    public function isDeprecated()
97
    {
98 3
        return $this->getConfigValue('isDeprecated');
99
    }
100
101 3
    public function getDeprecationReason()
102
    {
103 3
        return $this->getConfigValue('deprecationReason');
104
    }
105
}
106