Passed
Push — master ( c991d8...c23f05 )
by Sebastian
09:48
created

OperationResult::isNotice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * File containing the {@link OperationResult} class.
4
 *
5
 * @package Application Utils
6
 * @subpackage OperationResult
7
 * @see OperationResult
8
 */
9
10
declare(strict_types=1);
11
12
namespace AppUtils;
13
14
/**
15
 * Operation result container: can be used to store 
16
 * details on the results of an operation, and its
17
 * status. It is intended to be used if the operation 
18
 * failing is not critical (not worthy of an exception).
19
 * 
20
 * For example, this can be used as return value of
21
 * a validation operation, or any other process that
22
 * can return error information.
23
 *
24
 * @package Application Utils
25
 * @subpackage OperationResult
26
 * @author Sebastian Mordziol <[email protected]>
27
 */
28
class OperationResult
29
{
30
    const TYPE_NOTICE = 'notice';
31
    const TYPE_WARNING = 'warning';
32
    const TYPE_ERROR = 'error';
33
    const TYPE_SUCCESS = 'success';
34
    
35
   /**
36
    * @var string
37
    */
38
    protected $message = '';
39
    
40
   /**
41
    * @var bool
42
    */
43
    protected $valid = true;
44
  
45
   /**
46
    * @var object
47
    */
48
    protected $subject;
49
    
50
   /**
51
    * @var integer
52
    */
53
    protected $code = 0;
54
    
55
   /**
56
    * @var string
57
    */
58
    protected $type = '';
59
    
60
   /**
61
    * @var integer
62
    */
63
    private static $counter = 0;
64
    
65
   /**
66
    * @var int
67
    */
68
    private $id;
69
    
70
   /**
71
    * The subject being validated.
72
    * 
73
    * @param object $subject
74
    */
75
    public function __construct(object $subject)
76
    {
77
        $this->subject = $subject;
78
        
79
        self::$counter++;
80
        
81
        $this->id = self::$counter;
82
    }
83
    
84
   /**
85
    * Retrieves the ID of the result, which is unique within a request.
86
    * 
87
    * @return int
88
    */
89
    public function getID() : int
90
    {
91
        return $this->id;
92
    }
93
    
94
   /**
95
    * Whether the validation was successful.
96
    * 
97
    * @return bool
98
    */
99
    public function isValid() : bool
100
    {
101
        return $this->valid;
102
    }
103
    
104
    public function isError() : bool
105
    {
106
        return $this->isType(self::TYPE_ERROR);
107
    }
108
    
109
    public function isWarning() : bool
110
    {
111
        return $this->isType(self::TYPE_WARNING);
112
    }
113
    
114
    public function isNotice() : bool
115
    {
116
        return $this->isType(self::TYPE_NOTICE);
117
    }
118
    
119
    public function isSuccess() : bool
120
    {
121
        return $this->isType(self::TYPE_SUCCESS);
122
    }
123
    
124
    public function isType(string $type) : bool
125
    {
126
        return $this->type === $type;
127
    }
128
    
129
   /**
130
    * Retrieves the subject that was validated.
131
    * 
132
    * @return object
133
    */
134
    public function getSubject() : object
135
    {
136
        return $this->subject;
137
    }
138
    
139
   /**
140
    * Makes the result a succes, with the specified message.
141
    * 
142
    * @param string $message Should not contain a date, just the system specific info.
143
    * @return OperationResult
144
    */
145
    public function makeSuccess(string $message, int $code=0) : OperationResult
146
    {
147
        return $this->setMessage(self::TYPE_SUCCESS, $message, $code, true);
148
    }
149
    
150
   /**
151
    * Sets the result as an error.
152
    * 
153
    * @param string $message Should be as detailed as possible.
154
    * @return OperationResult
155
    */
156
    public function makeError(string $message, int $code=0) : OperationResult
157
    {
158
        return $this->setMessage(self::TYPE_ERROR, $message, $code, false);
159
    }
160
    
161
    public function makeNotice(string $message, int $code) : OperationResult
162
    {
163
        return $this->setMessage(self::TYPE_NOTICE, $message, $code, true);
164
    }
165
    
166
    public function makeWarning(string $message, int $code) : OperationResult
167
    {
168
        return $this->setMessage(self::TYPE_WARNING, $message, $code, true);
169
    }
170
    
171
    protected function setMessage(string $type, string $message, int $code, bool $valid) : OperationResult
172
    {
173
        $this->type = $type;
174
        $this->valid = $valid;
175
        $this->message = $message;
176
        $this->code = $code;
177
        
178
        return $this;
179
    }
180
    
181
    public function getType() : string
182
    {
183
        return $this->type;
184
    }
185
    
186
   /**
187
    * Retrieves the error message, if an error occurred.
188
    * 
189
    * @return string The error message, or an empty string if no error occurred.
190
    */
191
    public function getErrorMessage() : string
192
    {
193
        return $this->getMessage(self::TYPE_ERROR);
194
    }
195
    
196
   /**
197
    * Retrieves the success message, if one has been provided.
198
    * 
199
    * @return string
200
    */
201
    public function getSuccessMessage() : string
202
    {
203
        return $this->getMessage(self::TYPE_SUCCESS);
204
    }
205
    
206
    public function getNoticeMessage() : string
207
    {
208
        return $this->getMessage(self::TYPE_NOTICE);
209
    }
210
 
211
    public function getWarningMessage() : string
212
    {
213
        return $this->getMessage(self::TYPE_WARNING);
214
    }
215
    
216
   /**
217
    * Whether a specific error/success code has been specified.
218
    * 
219
    * @return bool
220
    */
221
    public function hasCode() : bool
222
    {
223
        return $this->code > 0;
224
    }
225
    
226
   /**
227
    * Retrieves the error/success code, if any. 
228
    * 
229
    * @return int The error code, or 0 if none.
230
    */
231
    public function getCode() : int
232
    {
233
        return $this->code;
234
    }
235
    
236
    public function getMessage(string $type='') : string
237
    {
238
        if(!empty($type))
239
        {
240
            if($this->type === $type)
241
            {
242
                return $this->message;
243
            }
244
            
245
            return '';
246
        }
247
        
248
        return $this->message;
249
    }
250
}
251