Completed
Push — master ( 180a00...5febbb )
by Alexandr
03:11
created

AbstractField::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 3
Bugs 2 Features 0
Metric Value
dl 14
loc 14
ccs 10
cts 10
cp 1
rs 9.4285
c 3
b 2
f 0
cc 3
eloc 8
nc 4
nop 1
crap 3
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\Object\AbstractObjectType;
13
use Youshido\GraphQL\Type\Traits\AutoNameTrait;
14
use Youshido\GraphQL\Type\Traits\FieldsArgumentsAwareObjectTrait;
15
use Youshido\GraphQL\Type\TypeFactory;
16
use Youshido\GraphQL\Type\TypeMap;
17
use Youshido\GraphQL\Type\TypeService;
18
19
abstract class AbstractField implements FieldInterface
20
{
21
22
    use FieldsArgumentsAwareObjectTrait, AutoNameTrait;
23
24
    protected $isFinal = false;
25
26
    private $resolveCache = null;
27 65
28 View Code Duplication
    public function __construct(array $config = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29 65
    {
30 31
        if (empty($config['type'])) {
31 31
            $config['type'] = $this->getType();
32 31
            $config['name'] = $this->getName();
33
        }
34 65
35 57
        if (TypeService::isScalarType($config['type'])) {
36 57
            $config['type'] = TypeFactory::getScalarType($config['type']);
37
        }
38 65
39 63
        $this->config = new FieldConfig($config, $this, $this->isFinal);
40 63
        $this->build($this->config);
41
    }
42
43
    /**
44
     * @return AbstractObjectType
45
     */
46
    abstract public function getType();
47 62
48
    public function build(FieldConfig $config)
49 62
    {
50
    }
51 1
52
    public function setType($type)
53 1
    {
54 1
        $this->getConfig()->set('type', $type);
55
    }
56
57
    public function resolve($value, array $args, ResolveInfo $info)
58
    {
59
        if ($this->resolveCache === null) {
60
            $this->resolveCache = $this->getConfig()->getResolveFunction();
61
62 3
            if (!$this->resolveCache) {
63
                $this->resolveCache = false;
64 3
            }
65
        }
66
        if ($this->resolveCache) {
67 19
            return ($this->resolveCache)($value, $args, $info);
68
        } else {
69 19
            if (is_array($value) && array_key_exists($this->getName(), $value)) {
70 19
                return $value[$this->getName()];
71
            } elseif (is_object($value)) {
72 19
                return TypeService::getPropertyValue($value, $this->getName());
73 19
            } elseif ($this->getType()->getNamedType()->getKind() == TypeMap::KIND_SCALAR) {
74 19
                return null;
75 19
            } else {
76
                throw new \Exception(sprintf('Property "%s" not found in resolve result', $this->getName()));
77 19
            }
78
        }
79
    }
80 3
81
82 3
    public function isDeprecated()
83
    {
84
        return $this->getConfigValue('isDeprecated');
85 3
    }
86
87 3
    public function getDeprecationReason()
88
    {
89
        return $this->getConfigValue('deprecationReason');
90
    }
91
}
92