Completed
Push — master ( d01340...7e7fa7 )
by Portey
07:36
created

AbstractEnumType::resolve()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0417

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
rs 9.4286
cc 3
eloc 5
nc 3
nop 1
crap 3.0417
1
<?php
2
/**
3
 * Date: 07.12.15
4
 *
5
 * @author Portey Vasil <[email protected]>
6
 */
7
8
namespace Youshido\GraphQL\Type\Object;
9
10
11
use Youshido\GraphQL\Type\AbstractType;
12
use Youshido\GraphQL\Type\Config\Object\EnumTypeConfig;
13
use Youshido\GraphQL\Type\TypeMap;
14
15
abstract class AbstractEnumType extends AbstractType
16
{
17
18
    /**
19
     * ObjectType constructor.
20
     * @param $config
21
     */
22 7
    public function __construct($config = [])
23
    {
24 7
        if (empty($config)) {
25 7
            $config['name'] = $this->getName();
26 7
            $config['values'] = $this->getValues();
27 7
        }
28
29 7
        $this->config = new EnumTypeConfig($config, $this);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Youshido\GraphQL\Ty...eConfig($config, $this) of type object<Youshido\GraphQL\...\Object\EnumTypeConfig> is incompatible with the declared type object<Youshido\GraphQL\...\InputObjectTypeConfig> of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
30 7
    }
31
32
    /**
33
     * @return String predefined type kind
34
     */
35 3
    public function getKind()
36
    {
37 3
        return TypeMap::KIND_ENUM;
38
    }
39
40
    /**
41
     * @param $value mixed
42
     *
43
     * @return bool
44
     */
45
    public function isValidValue($value)
46
    {
47
        return in_array($value, array_map(function ($item) { return $item['value']; }, $this->getConfig()->get('values')));
48
    }
49
50
    abstract public function getValues();
51
52 3
    public function checkBuild()
53
    {
54 3
    }
55
56 1
    public function resolve($value)
57
    {
58 1
        foreach($this->getConfig()->get('values') as $enumValue => $valueItem) {
59 1
            if($value == $valueItem['value']){
60 1
                return $enumValue;
61
            }
62 1
        }
63
64
        return null;
65
    }
66
}
67