Completed
Push — master ( 6c95d8...49bfd8 )
by Christoffer
03:47 queued 01:30
created

PropertyFieldResolver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 10
c 0
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B resolve() 0 22 6
1
<?php
2
3
namespace Digia\GraphQL\Execution\Resolver;
4
5
use Digia\GraphQL\Execution\ExecutionEnvironment;
6
7
class PropertyFieldResolver implements ResolverInterface
8
{
9
    /**
10
     * @inheritdoc
11
     */
12
    public function resolve(ExecutionEnvironment $environment)
13
    {
14
        $value     = $environment->getValue();
15
        $fieldName = $environment->getInfo()->getFieldName();
16
17
        if (\is_array($value) || $value instanceof \ArrayAccess) {
18
            return $value[$fieldName] ?? null;
19
        }
20
21
        if (\is_object($value)) {
22
            $getter = 'get' . ucfirst($fieldName);
23
24
            if (method_exists($value, $getter)) {
25
                return $value->{$getter}();
26
            }
27
28
            if (property_exists($value, $fieldName)) {
29
                return $value->{$fieldName};
30
            }
31
        }
32
33
        throw new \Exception(sprintf('Could not resolve value for field "%s".', $fieldName));
34
    }
35
}
36