Completed
Pull Request — master (#45)
by Daniel
04:14
created

AbstractConfig::getData()   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
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\Exception\ConfigurationException;
14
use Youshido\GraphQL\Validator\Exception\ValidationException;
15
16
/**
17
 * Class Config
18
 * @package Youshido\GraphQL\Config
19
 */
20
abstract class AbstractConfig
21
{
22
23
    /**
24
     * @var array
25
     */
26
    protected $data = [];
27
28
    protected $contextObject;
29
30
    protected $finalClass = false;
31
32
    protected $extraFieldsAllowed = null;
33
34
    /**
35
     * TypeConfig constructor.
36
     * @param array $configData
37
     * @param mixed $contextObject
38
     * @param bool  $finalClass
39
     *
40
     * @throws ConfigurationException
41
     * @throws ValidationException
42
     */
43 112
    public function __construct(array $configData, $contextObject = null, $finalClass = false)
44
    {
45 112
        if (empty($configData)) {
46 3
            throw new ConfigurationException('Config for Type should be an array');
47
        }
48
49 109
        $this->contextObject = $contextObject;
50 109
        $this->data          = $configData;
51 109
        $this->finalClass    = $finalClass;
52
53
//        $this->validate();
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
54 109
        $this->build();
55 109
    }
56
57
    public function validate()
58
    {
59
        $validator = ConfigValidator::getInstance();
60
61
        if (!$validator->validate($this->data, $this->getContextRules(), $this->extraFieldsAllowed)) {
62
            throw new ConfigurationException('Config is not valid for ' . ($this->contextObject ? get_class($this->contextObject) : null) . "\n" . implode("\n", $validator->getErrorsArray(false)));
63
        }
64
    }
65
66 1
    public function getContextRules()
67
    {
68 1
        $rules = $this->getRules();
69 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...
70 1
            foreach ($rules as $name => $info) {
71 1
                if (!empty($info['final'])) {
72 1
                    $rules[$name]['required'] = true;
73
                }
74
            }
75
        }
76
77 1
        return $rules;
78
    }
79
80
    abstract public function getRules();
81
82 58
    public function getName()
83
    {
84 58
        return $this->get('name');
85
    }
86
87 3
    public function getType()
88
    {
89 3
        return $this->get('type');
90
    }
91
92 52
    public function getData()
93
    {
94 52
        return $this->data;
95
    }
96
97 20
    public function getContextObject()
98
    {
99 20
        return $this->contextObject;
100
    }
101
102 52
    public function isFinalClass()
103
    {
104 52
        return $this->finalClass;
105
    }
106
107 52
    public function isExtraFieldsAllowed()
108
    {
109 52
        return $this->extraFieldsAllowed;
110
    }
111
112
113
    /**
114
     * @return null|callable
115
     */
116 26
    public function getResolveFunction()
117
    {
118 26
        return $this->get('resolve', null);
119
    }
120
121 67
    protected function build()
122
    {
123 67
    }
124
125
    /**
126
     * @param      $key
127
     * @param null $defaultValue
128
     * @return mixed|null|callable
129
     */
130 76
    public function get($key, $defaultValue = null)
131
    {
132 76
        return array_key_exists($key, $this->data) ? $this->data[$key] : $defaultValue;
133
    }
134
135 9
    public function set($key, $value)
136
    {
137 9
        $this->data[$key] = $value;
138
139 9
        return $this;
140
    }
141
142 2
    public function __call($method, $arguments)
143
    {
144 2
        if (substr($method, 0, 3) == 'get') {
145 1
            $propertyName = lcfirst(substr($method, 3));
146 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...
147 1
            $propertyName = lcfirst(substr($method, 3));
148 1
            $this->set($propertyName, $arguments[0]);
149
150 1
            return $this;
151 2
        } elseif (substr($method, 0, 2) == 'is') {
152 1
            $propertyName = lcfirst(substr($method, 2));
153
        } else {
154 1
            throw new \Exception('Call to undefined method ' . $method);
155
        }
156
157 1
        return $this->get($propertyName);
158
    }
159
160
161
}
162