AbstractConfig::isFinalClass()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
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\Exception\ConfigurationException;
13
use Youshido\GraphQL\Exception\ValidationException;
14
use Youshido\GraphQL\Validator\ConfigValidator\ConfigValidator;
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 149
    public function __construct(array $configData, $contextObject = null, $finalClass = false)
46
    {
47 149
        if (empty($configData)) {
48 3
            throw new ConfigurationException('Config for Type should be an array');
49
        }
50
51 146
        $this->contextObject = $contextObject;
52 146
        $this->data          = $configData;
53 146
        $this->finalClass    = $finalClass;
54
55 146
        $this->build();
56 146
    }
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 101
    public function getName()
84
    {
85 101
        return $this->get('name');
86
    }
87
88 6
    public function getType()
89
    {
90 6
        return $this->get('type');
91
    }
92
93 99
    public function getData()
94
    {
95 99
        return $this->data;
96
    }
97
98 21
    public function getContextObject()
99
    {
100 21
        return $this->contextObject;
101
    }
102
103 99
    public function isFinalClass()
104
    {
105 99
        return $this->finalClass;
106
    }
107
108 99
    public function isExtraFieldsAllowed()
109
    {
110 99
        return $this->extraFieldsAllowed;
111
    }
112
113
114
    /**
115
     * @return null|callable
116
     */
117 64
    public function getResolveFunction()
118
    {
119 64
        return $this->get('resolve', null);
120
    }
121
122 108
    protected function build()
123
    {
124 108
    }
125
126
    /**
127
     * @param      $key
128
     * @param null $defaultValue
129
     *
130
     * @return mixed|null|callable
131
     */
132 115
    public function get($key, $defaultValue = null)
133
    {
134 115
        return $this->has($key) ? $this->data[$key] : $defaultValue;
135
    }
136
137 31
    public function set($key, $value)
138
    {
139 31
        $this->data[$key] = $value;
140
141 31
        return $this;
142
    }
143
144 115
    public function has($key)
145
    {
146 115
        return array_key_exists($key, $this->data);
147
    }
148
149 3
    public function __call($method, $arguments)
150
    {
151 3
        if (substr($method, 0, 3) == 'get') {
152 2
            $propertyName = lcfirst(substr($method, 3));
153 3
        } elseif (substr($method, 0, 3) == 'set') {
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 2
        return $this->get($propertyName);
165
    }
166
167
168
}
169