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

AbstractField::getDeprecationReason()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 1
b 1
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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