Test Failed
Pull Request — master (#147)
by Simone
01:55
created

Resource   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 57.78%

Importance

Changes 0
Metric Value
wmc 18
c 0
b 0
f 0
lcom 2
cbo 3
dl 0
loc 111
ccs 26
cts 45
cp 0.5778
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A mandatory() 0 4 1
A allowed() 0 4 1
A allowedValues() 0 4 1
A rules() 0 4 1
A rewrites() 0 4 1
A ranges() 0 4 1
A isRuleDefinedFor() 0 4 1
A isRuleNotDefinedFor() 0 4 1
A getRule() 0 6 1
A isFieldNumeric() 0 4 1
A isFieldNumericButString() 0 5 2
A getFieldType() 0 4 1
A defaults() 0 4 1
A applyConfiguration() 0 21 3
A isFieldString() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of sensorario/resources repository
5
 *
6
 * (c) Simone Gentili <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sensorario\Resources;
13
14
use Sensorario\Resources\Interfaces\ResourceInterface;
15
use Sensorario\Resources\Rulers\Rule;
16
17
class Resource extends MagicResource implements ResourceInterface
18
{
19
    protected $allowed = [];
20
21
    protected $allowedValues = [];
22
23
    protected $mandatory = [];
24
25
    protected $defaults = [];
26
27
    protected $rules = [];
28
29
    protected $rewrites = [];
30
31
    protected $ranges = [];
32
33 7
    public function mandatory()
34
    {
35 7
        return $this->mandatory;
36
    }
37
38 4
    public function allowed()
39
    {
40 4
        return $this->allowed;
41
    }
42
43 6
    public function allowedValues()
44
    {
45 6
        return $this->allowedValues;
46
    }
47
48 9
    public function rules()
49
    {
50 9
        return $this->rules;
51
    }
52
53
    public function defaults()
54
    {
55
        return $this->defaults;
56
    }
57
58 8
    public function rewrites()
59
    {
60 8
        return $this->rewrites;
61
    }
62
63 8
    public function ranges()
64
    {
65 8
        return $this->ranges;
66
    }
67
68
    public function applyConfiguration(
69
        Configurator $configurator
70
    ) {
71
        $this->allowed       = $configurator->allowed();
72
        $this->mandatory     = $configurator->mandatory();
73
        $this->defaults      = $configurator->defaults();
74
        $this->rules         = $configurator->rules();
75
        $this->allowedValues = $configurator->allowedValues();
76
        $this->rewrites      = $configurator->rewrites();
77
        $this->ranges        = $configurator->ranges();
78
79
        if ($configurator->globals()) {
80
            $globals = $configurator->globals();
81
            if (isset($globals['allowed'])) {
82
                $this->allowed = array_merge(
83
                    $this->allowed,
84
                    $globals['allowed']
85
                );
86
            }
87
        }
88
    }
89
90 12
    public function isRuleDefinedFor($ruleName)
91
    {
92 12
        return isset($this->rules()[$ruleName]);
93
    }
94
95 12
    public function isRuleNotDefinedFor($ruleName)
96
    {
97 12
        return !$this->isRuleDefinedFor($ruleName);
98
    }
99
100 5
    public function getRule($ruleName) : Rule
101
    {
102 5
        return Rule::fromArray(
103 5
            $this->rules()[$ruleName]
104
        );
105
    }
106
107 1
    public function isFieldNumeric($fieldName)
108
    {
109 1
        return is_numeric($this->get($fieldName));
110
    }
111
112
    public function isFieldString($fieldName)
113
    {
114
        return is_string($this->get($fieldName));
115
    }
116
117 1
    public function isFieldNumericButString($fieldName)
118
    {
119 1
        return $this->isFieldNumeric($fieldName)
120 1
            && $this->isFieldString($fieldName);
121
    }
122
123 5
    public function getFieldType($fieldName)
124
    {
125 5
        return gettype($this->get($fieldName));
126
    }
127
}
128