Passed
Push — master ( 5cb917...3c1566 )
by Sergey
02:41
created

ValidationError   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 21.74%
Metric Value
wmc 14
lcom 1
cbo 4
dl 0
loc 145
ccs 10
cts 46
cp 0.2174
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A makeNodeError() 0 9 1
A makeRecordError() 0 11 1
A toArray() 0 18 3
A getZone() 0 4 1
A getNode() 0 7 2
A isHasNode() 0 4 1
A getRecord() 0 7 2
A isHasRecord() 0 4 1
A makeZoneError() 0 8 1
1
<?php
2
/**
3
 * @author: Viskov Sergey
4
 * @date  : 4/12/16
5
 * @time  : 1:00 PM
6
 */
7
8
namespace LTDBeget\dns\configurator\errors;
9
10
use LTDBeget\dns\configurator\Zone;
11
use LTDBeget\dns\configurator\zoneEntities\Node;
12
use LTDBeget\dns\configurator\zoneEntities\record\base\Record;
13
use LTDBeget\dns\enums\eErrorCode;
14
15
/**
16
 * Class ValidationError
17
 *
18
 * @package LTDBeget\dns\configurator\errors
19
 */
20
class ValidationError
21
{
22
    /**
23
     * @var Zone
24
     */
25
    private $zone = NULL;
26
    /**
27
     * @var Node
28
     */
29
    private $node = NULL;
30
    /**
31
     * @var Record
32
     */
33
    private $record = NULL;
34
    /**
35
     * @var eErrorCode
36
     */
37
    private $errorCode;
38
    /**
39
     * @var String
40
     */
41
    private $checkedAttribute = NULL;
42
43
    /**
44
     * closed. Make instance only via static methods
45
     */
46 1
    private function __construct()
47
    {
48 1
    }
49
50
    /**
51
     * @param Zone       $zone
52
     * @param eErrorCode $errorCode
53
     * @return ValidationError
54
     */
55
    public static function makeZoneError(Zone $zone, eErrorCode $errorCode) : ValidationError
56
    {
57
        $error            = new self;
58
        $error->zone      = $zone;
59
        $error->errorCode = $errorCode;
60
61
        return $error;
62
    }
63
64
    /**
65
     * @param Node       $node
66
     * @param eErrorCode $errorCode
67
     * @return ValidationError
68
     */
69
    public static function makeNodeError(Node $node, eErrorCode $errorCode) : ValidationError
70
    {
71
        $error            = new self;
72
        $error->zone      = $node->getZone();
73
        $error->node      = $node;
74
        $error->errorCode = $errorCode;
75
76
        return $error;
77
    }
78
79
    /**
80
     * @param Record     $record
81
     * @param eErrorCode $errorCode
82
     * @param            $checked_atribute
83
     * @return ValidationError
84
     */
85 1
    public static function makeRecordError(Record $record, eErrorCode $errorCode, $checked_atribute) : ValidationError
86
    {
87 1
        $error                   = new self;
88 1
        $error->zone             = $record->getNode()->getZone();
89 1
        $error->node             = $record->getNode();
90 1
        $error->record           = $record;
91 1
        $error->errorCode        = $errorCode;
92 1
        $error->checkedAttribute = $checked_atribute;
93
94 1
        return $error;
95
    }
96
97
    /**
98
     * @return array
99
     */
100
    public function toArray() : array
101
    {
102
        $errorArray              = [];
103
        $errorArray["code"]      = $this->errorCode->getValue();
104
        $errorArray["errorText"] = $this->errorCode->getText();
105
        $errorArray["origin"]    = $this->zone->getOrigin();
106
107
        if (!is_null($this->node)) {
108
            $errorArray["node"] = $this->node->getName();
109
        }
110
111
        if (!is_null($this->record)) {
112
            $errorArray["recordData"]       = $this->record->toArray();
113
            $errorArray["checkedAttribute"] = $this->checkedAttribute;
114
        }
115
116
        return $errorArray;
117
    }
118
119
    /**
120
     * @return Zone
121
     */
122
    public function getZone()
123
    {
124
        return $this->zone;
125
    }
126
127
    /**
128
     * @return Node
129
     */
130
    public function getNode() : Node
131
    {
132
        if(! $this->isHasNode()) {
133
            throw new \RuntimeException("ValidationError has no Node");
134
        }
135
        return $this->node;
136
    }
137
138
    /**
139
     * @return bool
140
     */
141
    public function isHasNode() : bool
142
    {
143
        return !is_null($this->node);
144
    }
145
146
    /**
147
     * @return Record
148
     */
149
    public function getRecord() : Record
150
    {
151
        if(! $this->isHasRecord()) {
152
            throw new \RuntimeException("ValidationError has no Record");
153
        }
154
        return $this->record;
155
    }
156
157
    /**
158
     * @return bool
159
     */
160
    public function isHasRecord() : bool
161
    {
162
        return !is_null($this->record);
163
    }
164
}