Completed
Push — master ( 8c19ea...8f4b0a )
by Portey
03:08
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 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
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\Exception\ConfigurationException;
14
use Youshido\GraphQL\Validator\Exception\ValidationException;
15
16
/**
17
 * Class Config
18
 *
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
    /**
36
     * TypeConfig constructor.
37
     *
38
     * @param array $configData
39
     * @param mixed $contextObject
40
     * @param bool  $finalClass
41
     *
42
     * @throws ConfigurationException
43
     * @throws ValidationException
44
     */
45 110
    public function __construct(array $configData, $contextObject = null, $finalClass = false)
46
    {
47 110
        if (empty($configData)) {
48 3
            throw new ConfigurationException('Config for Type should be an array');
49
        }
50
51 107
        $this->contextObject = $contextObject;
52 107
        $this->data          = $configData;
53 107
        $this->finalClass    = $finalClass;
54
55 107
        $this->build();
56 107
    }
57
58
    public function validate()
59
    {
60
        $validator = ConfigValidator::getInstance();
61
62
        if (!$validator->validate($this->data, $this->getContextRules(), $this->extraFieldsAllowed)) {
63
            throw new ConfigurationException('Config is not valid for ' . ($this->contextObject ? get_class($this->contextObject) : null) . "\n" . implode("\n", $validator->getErrorsArray(false)));
64
        }
65
    }
66
67 1
    public function getContextRules()
68
    {
69 1
        $rules = $this->getRules();
70 1 View Code Duplication
        if ($this->finalClass) {
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...
71 1
            foreach ($rules as $name => $info) {
72 1
                if (!empty($info['final'])) {
73 1
                    $rules[$name]['required'] = true;
74 1
                }
75 1
            }
76 1
        }
77
78 1
        return $rules;
79
    }
80
81
    abstract public function getRules();
82
83 62
    public function getName()
84
    {
85 62
        return $this->get('name');
86
    }
87
88 3
    public function getType()
89
    {
90 3
        return $this->get('type');
91
    }
92
93 60
    public function getData()
94
    {
95 60
        return $this->data;
96
    }
97
98 20
    public function getContextObject()
99
    {
100 20
        return $this->contextObject;
101
    }
102
103 60
    public function isFinalClass()
104
    {
105 60
        return $this->finalClass;
106
    }
107
108 60
    public function isExtraFieldsAllowed()
109
    {
110 60
        return $this->extraFieldsAllowed;
111
    }
112
113
114
    /**
115
     * @return null|callable
116
     */
117 31
    public function getResolveFunction()
118
    {
119 31
        return $this->get('resolve', null);
120
    }
121
122 71
    protected function build()
123
    {
124 71
    }
125
126
    /**
127
     * @param      $key
128
     * @param null $defaultValue
129
     *
130
     * @return mixed|null|callable
131
     */
132 76
    public function get($key, $defaultValue = null)
133
    {
134 76
        return $this->has($key) ? $this->data[$key] : $defaultValue;
135
    }
136
137 17
    public function set($key, $value)
138
    {
139 17
        $this->data[$key] = $value;
140
141 17
        return $this;
142
    }
143
144 76
    public function has($key)
145
    {
146 76
        return array_key_exists($key, $this->data);
147
    }
148
149 2
    public function __call($method, $arguments)
150
    {
151 2
        if (substr($method, 0, 3) == 'get') {
152 1
            $propertyName = lcfirst(substr($method, 3));
153 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...
154 1
            $propertyName = lcfirst(substr($method, 3));
155 1
            $this->set($propertyName, $arguments[0]);
156
157 1
            return $this;
158 2
        } elseif (substr($method, 0, 2) == 'is') {
159 1
            $propertyName = lcfirst(substr($method, 2));
160 1
        } else {
161 1
            throw new \Exception('Call to undefined method ' . $method);
162
        }
163
164 1
        return $this->get($propertyName);
165
    }
166
167
168
}
169