Completed
Push — master ( d52ee7...e7842c )
by Alexandr
03:54
created

AbstractConfig::getResolveFunction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/*
3
* This file is a part of graphql-youshido project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 11/27/15 2:31 AM
7
*/
8
9
namespace Youshido\GraphQL\Config;
10
11
12
use Youshido\GraphQL\Validator\ConfigValidator\ConfigValidator;
13
use Youshido\GraphQL\Validator\ConfigValidator\ConfigValidatorInterface;
14
use Youshido\GraphQL\Validator\Exception\ConfigurationException;
15
use Youshido\GraphQL\Validator\Exception\ValidationException;
16
17
/**
18
 * Class Config
19
 * @package Youshido\GraphQL\Config
20
 */
21
abstract class AbstractConfig
22
{
23
24
    /**
25
     * @var array
26
     */
27
    protected $data = [];
28
29
    protected $contextObject;
30
31
    protected $finalClass = false;
32
33
    protected $extraFieldsAllowed = null;
34
35
    /** @var ConfigValidatorInterface */
36
    protected $validator;
37
38
    /**
39
     * TypeConfig constructor.
40
     * @param array $configData
41
     * @param mixed $contextObject
42
     * @param bool  $finalClass
43
     *
44
     * @throws ConfigurationException
45
     * @throws ValidationException
46
     */
47 101
    public function __construct(array $configData, $contextObject = null, $finalClass = false)
48
    {
49 101
        if (empty($configData)) {
50 4
            throw new ConfigurationException('Config for Type should be an array');
51
        }
52
53 98
        $this->contextObject = $contextObject;
54 98
        $this->data          = $configData;
55 98
        $this->finalClass    = $finalClass;
56 98
        $this->validator     = new ConfigValidator($contextObject);
57
58 98
        if (!$this->validator->validate($this->data, $this->getContextRules(), $this->extraFieldsAllowed)) {
59 20
            throw new ConfigurationException('Config is not valid for ' . ($contextObject ? get_class($contextObject) : null) . "\n" . implode("\n", $this->validator->getErrorsArray(false)));
60
        }
61
62 78
        $this->build();
63 78
    }
64
65 98
    public function getContextRules()
66
    {
67 98
        $rules = $this->getRules();
68 98
        if ($this->finalClass) {
69 69
            foreach ($rules as $name => $info) {
70 69
                if (!empty($info['final'])) {
71 69
                    $rules[$name]['required'] = true;
72 69
                }
73 69
            }
74 69
        }
75
76 98
        return $rules;
77
    }
78
79
    abstract public function getRules();
80
81 52
    public function getName()
82
    {
83 52
        return $this->get('name');
84
    }
85
86 3
    public function getType()
87
    {
88 3
        return $this->get('type');
89
    }
90
91
    /**
92
     * @return null|callable
93
     */
94 21
    public function getResolveFunction()
95
    {
96 21
        return $this->get('resolve', null);
97
    }
98
99 46
    protected function build()
100
    {
101 46
    }
102
103
    /**
104
     * @param      $key
105
     * @param null $defaultValue
106
     * @return mixed|null|callable
107
     */
108 66
    public function get($key, $defaultValue = null)
109
    {
110 66
        return array_key_exists($key, $this->data) ? $this->data[$key] : $defaultValue;
111
    }
112
113 9
    public function set($key, $value)
114
    {
115 9
        $this->data[$key] = $value;
116
117 9
        return $this;
118
    }
119
120 2
    public function __call($method, $arguments)
121
    {
122 2
        if (substr($method, 0, 3) == 'get') {
123 1
            $propertyName = lcfirst(substr($method, 3));
124 2 View Code Duplication
        } elseif (substr($method, 0, 3) == 'set') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
125 1
            $propertyName = lcfirst(substr($method, 3));
126 1
            $this->set($propertyName, $arguments[0]);
127
128 1
            return $this;
129 2
        } elseif (substr($method, 0, 2) == 'is') {
130 1
            $propertyName = lcfirst(substr($method, 2));
131 1
        } else {
132 1
            throw new \Exception('Call to undefined method ' . $method);
133
        }
134
135 1
        return $this->get($propertyName);
136
    }
137
138
139
}
140