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

OperationResult_Collection::makeWarning()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * File containing the {@link OperationResult_Collection} class.
4
 *
5
 * @package Application Utils
6
 * @subpackage OperationResult
7
 * @see OperationResult_Collection
8
 */
9
10
declare(strict_types=1);
11
12
namespace AppUtils;
13
14
/**
15
 * Storage for several operation result instances, that acts
16
 * as a regular operation result. 
17
 * 
18
 * Can be used as replacement result object, which will catch 
19
 * all makeError() and makeSuccess() calls as separate error
20
 * or success instances. Adding a collection to a collection
21
 * will make it inherit all results the target collection contains.
22
 *
23
 * @package Application Utils
24
 * @subpackage OperationResult
25
 * @author Sebastian Mordziol <[email protected]>
26
 */
27
class OperationResult_Collection extends OperationResult
28
{
29
   /**
30
    * @var OperationResult[]
31
    */
32
    protected $results = array();
33
    
34
    public function makeError(string $message, int $code=0) : OperationResult
35
    {
36
        return $this->add('makeError', $message, $code);
37
    }
38
    
39
    public function makeSuccess(string $message, int $code=0) : OperationResult
40
    {
41
        return $this->add('makeSuccess', $message, $code);
42
    }
43
44
    public function makeWarning(string $message, int $code=0) : OperationResult
45
    {
46
        return $this->add('makeWarning', $message, $code);
47
    }
48
    
49
    public function makeNotice(string $message, int $code=0) : OperationResult
50
    {
51
        return $this->add('makeNotice', $message, $code);
52
    }
53
    
54
    protected function add(string $method, string $message, int $code=0) : OperationResult
55
    {
56
        $result = new OperationResult($this->subject);
57
        $result->$method($message, $code);
58
        
59
        $this->results[] = $result;
60
        
61
        return $this;
62
    }
63
    
64
    public function addResult(OperationResult $result) : OperationResult_Collection
65
    {
66
        if($result instanceof OperationResult_Collection)
67
        {
68
            return $this->importCollection($result);
69
        }
70
71
        return $this->importResult($result);
72
    }
73
    
74
    private function importCollection(OperationResult_Collection $collection) : OperationResult_Collection
75
    {
76
        $results = $collection->getResults();
77
        
78
        foreach($results as $result)
79
        {
80
            $this->addResult($result);
81
        }
82
        
83
        return $this;
84
    }
85
    
86
    private function importResult(OperationResult $result) : OperationResult_Collection
87
    {
88
        // We need to inherit this collection\'s subject for any
89
        // results we add, so we simply create a new instance.
90
        //
91
        // Since there is no easy, well performing way to check if
92
        // the subjects are the same, this is actually the better way.
93
        $new = new OperationResult($this->subject);
94
        
95
        if($result->isValid())
96
        {
97
            $new->makeSuccess($result->getSuccessMessage(), $result->getCode());
98
        }
99
        else
100
        {
101
            $new->makeError($result->getErrorMessage(), $result->getCode());
102
        }
103
        
104
        $this->results[] = $new;
105
        
106
        return $this;
107
    }
108
    
109
   /**
110
    * @return OperationResult[]
111
    */
112
    public function getResults() : array
113
    {
114
        return $this->results;
115
    }
116
    
117
    public function isValid() : bool
118
    {
119
        foreach($this->results as $result)
120
        {
121
            if(!$result->isValid())
122
            {
123
                return false;
124
            }
125
        }
126
        
127
        return true;
128
    }
129
    
130
    public function hasCode() : bool
131
    {
132
        foreach($this->results as $result)
133
        {
134
            if($result->hasCode())
135
            {
136
                return true;
137
            }
138
        }
139
        
140
        return false;
141
    }
142
    
143
    public function getCode() : int
144
    {
145
        foreach($this->results as $result)
146
        {
147
            if($result->hasCode())
148
            {
149
                return $result->getCode();
150
            }
151
        }
152
        
153
        return 0;
154
    }
155
    
156
    public function getMessage(string $type='') : string
157
    {
158
        foreach($this->results as $result)
159
        {
160
            $msg = $result->getMessage($type);
161
            
162
            if(!empty($msg))
163
            {
164
                return $msg;
165
            }
166
        }
167
        
168
        return '';
169
    }
170
    
171
    public function containsCode(int $code) : bool
172
    {
173
        foreach($this->results as $result)
174
        {
175
            if($result->getCode() === $code)
176
            {
177
                return true;
178
            }
179
        }
180
        
181
        return false;
182
    }
183
    
184
    public function countErrors() : int
185
    {
186
        return $this->countByType(self::TYPE_ERROR);
187
    }
188
    
189
    public function countWarnings() : int
190
    {
191
        return $this->countByType(self::TYPE_WARNING);
192
    }
193
    
194
    public function countSuccesses() : int
195
    {
196
        return $this->countByType(self::TYPE_SUCCESS);
197
    }
198
    
199
    public function countNotices() : int
200
    {
201
        return $this->countByType(self::TYPE_NOTICE);
202
    }
203
    
204
    public function countByType(string $type) : int
205
    {
206
        $amount = 0;
207
        
208
        foreach($this->results as $result)
209
        {
210
            if($result->isType($type))
211
            {
212
                $amount++;
213
            }
214
        }
215
        
216
        return $amount;
217
    }
218
    
219
    public function countResults() : int
220
    {
221
        return count($this->results);
222
    }
223
    
224
    public function getErrors() : array
225
    {
226
        return $this->getByType(self::TYPE_ERROR);
227
    }
228
    
229
    public function getSuccesses() : array
230
    {
231
        return $this->getByType(self::TYPE_SUCCESS);
232
    }
233
    
234
    public function getWarnings() : array
235
    {
236
        return $this->getByType(self::TYPE_WARNING);
237
    }
238
    
239
    public function getNotices() : array
240
    {
241
        return $this->getByType(self::TYPE_NOTICE);
242
    }
243
    
244
    public function getByType(string $type) : array
245
    {
246
        $results = array();
247
        
248
        foreach($this->results as $result)
249
        {
250
            if($result->isType($type))
251
            {
252
                $results[] = $result;
253
            }
254
        }
255
        
256
        return $results;
257
    }
258
    
259
    public function isType(string $type) : bool
260
    {
261
        foreach($this->results as $result)
262
        {
263
            if($result->isType($type))
264
            {
265
                return true;
266
            }
267
        }
268
        
269
        return false;
270
    }
271
    
272
    public function getSummary() : string
273
    {
274
        $lines = array();
275
        
276
        $lines[] = 'Collection #'.$this->getID();
277
        $lines[] = 'Subject: '.get_class($this->subject);
278
        
279
        foreach($this->results as $result)
280
        {
281
            $lines[] = ' - '.$result->getType().' #'.$result->getCode().' "'.$result->getMessage($result->getType()).'"';
282
        }
283
        
284
        return implode(PHP_EOL, $lines);    
285
    }
286
}
287