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

Config::set()   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.037

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 3
cp 0.6667
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1.037
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
}