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

Config   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 77.42%

Importance

Changes 10
Bugs 1 Features 2
Metric Value
wmc 13
c 10
b 1
f 2
lcom 2
cbo 2
dl 0
loc 81
ccs 24
cts 31
cp 0.7742
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 3
A getRules() 0 4 1
A build() 0 3 1
A get() 0 4 2
A set() 0 4 1
A isValid() 0 4 1
A __call() 0 15 4
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\Type\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\Type\Config
20
 *
21
 * @method string getName()
22
 */
23
class Config
24
{
25
26
    /**
27
     * @var array
28
     */
29
    protected $data = [];
30
31
    protected $contextObject;
32
33
    /** @var ConfigValidatorInterface */
34
    protected $validator;
35
36
    /**
37
     * TypeConfig constructor.
38
     * @param array $configData
39
     * @param mixed $contextObject
40
     *
41
     * @throws ConfigurationException
42
     * @throws ValidationException
43
     */
44 21
    public function __construct($configData, $contextObject = null)
45
    {
46 21
        if (!is_array($configData)) {
47
            throw new ConfigurationException('Config for Type should be an array');
48
        }
49
50 21
        $this->contextObject = $contextObject;
51 21
        $this->data          = $configData;
52 21
        $this->validator     = new ConfigValidator($contextObject);
53 21
        if (!$this->validator->validate($this->data, $this->getRules())) {
54 1
            throw new ConfigurationException('Config is not valid for ' . get_class($contextObject) . "\n" . implode("\n", $this->validator->getErrorsArray()));
55
        }
56 21
        $this->build();
57 21
    }
58
59
    public function getRules()
60
    {
61
        return [];
62
    }
63
64 18
    protected function build()
65
    {
66 18
    }
67
68 18
    public function get($key, $defaultValue = null)
69
    {
70 18
        return array_key_exists($key, $this->data) ? $this->data[$key] : $defaultValue;
71
    }
72
73 1
    public function set($key, $value)
74
    {
75 1
        $this->data[$key] = $value;
76
    }
77
78
    /**
79
     * @return boolean
80
     */
81 10
    public function isValid()
82
    {
83 10
        return $this->validator->isValid();
84
    }
85
86 13
    public function __call($method, $arguments)
87
    {
88 13
        $propertyName = false;
89
90 13
        if (substr($method, 0, 3) == 'get') {
91 13
            $propertyName = lcfirst(substr($method, 3));
92 13
        } elseif (substr($method, 0, 2) == 'is') {
93
            $propertyName = lcfirst(substr($method, 2));
94
        }
95 13
        if ($propertyName !== false) {
96 13
            return $this->get($propertyName);
97
        }
98
99
        throw new \Exception('Call to undefined method ' . $method);
100
    }
101
102
103
}