Passed
Push — master ( e8ba28...a594cd )
by Sebastian
03:48
created

OperationResult_Collection::importResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 5
c 2
b 0
f 0
dl 0
loc 16
rs 10
cc 1
nc 1
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A OperationResult_Collection::getResults() 0 3 1
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
        $this->results[] = $result;
72
        
73
        return $this;
74
    }
75
    
76
    private function importCollection(OperationResult_Collection $collection) : OperationResult_Collection
77
    {
78
        $results = $collection->getResults();
79
        
80
        foreach($results as $result)
81
        {
82
            $this->addResult($result);
83
        }
84
        
85
        return $this;
86
    }
87
    
88
   /**
89
    * @return OperationResult[]
90
    */
91
    public function getResults() : array
92
    {
93
        return $this->results;
94
    }
95
    
96
    public function isValid() : bool
97
    {
98
        foreach($this->results as $result)
99
        {
100
            if(!$result->isValid())
101
            {
102
                return false;
103
            }
104
        }
105
        
106
        return true;
107
    }
108
    
109
    public function hasCode() : bool
110
    {
111
        foreach($this->results as $result)
112
        {
113
            if($result->hasCode())
114
            {
115
                return true;
116
            }
117
        }
118
        
119
        return false;
120
    }
121
    
122
    public function getCode() : int
123
    {
124
        foreach($this->results as $result)
125
        {
126
            if($result->hasCode())
127
            {
128
                return $result->getCode();
129
            }
130
        }
131
        
132
        return 0;
133
    }
134
    
135
    public function getMessage(string $type='') : string
136
    {
137
        foreach($this->results as $result)
138
        {
139
            $msg = $result->getMessage($type);
140
            
141
            if(!empty($msg))
142
            {
143
                return $msg;
144
            }
145
        }
146
        
147
        return '';
148
    }
149
    
150
    public function containsCode(int $code) : bool
151
    {
152
        foreach($this->results as $result)
153
        {
154
            if($result->getCode() === $code)
155
            {
156
                return true;
157
            }
158
        }
159
        
160
        return false;
161
    }
162
    
163
    public function countErrors() : int
164
    {
165
        return $this->countByType(self::TYPE_ERROR);
166
    }
167
    
168
    public function countWarnings() : int
169
    {
170
        return $this->countByType(self::TYPE_WARNING);
171
    }
172
    
173
    public function countSuccesses() : int
174
    {
175
        return $this->countByType(self::TYPE_SUCCESS);
176
    }
177
    
178
    public function countNotices() : int
179
    {
180
        return $this->countByType(self::TYPE_NOTICE);
181
    }
182
    
183
    public function countByType(string $type) : int
184
    {
185
        $amount = 0;
186
        
187
        foreach($this->results as $result)
188
        {
189
            if($result->isType($type))
190
            {
191
                $amount++;
192
            }
193
        }
194
        
195
        return $amount;
196
    }
197
    
198
    public function countResults() : int
199
    {
200
        return count($this->results);
201
    }
202
    
203
    public function getErrors() : array
204
    {
205
        return $this->getByType(self::TYPE_ERROR);
206
    }
207
    
208
    public function getSuccesses() : array
209
    {
210
        return $this->getByType(self::TYPE_SUCCESS);
211
    }
212
    
213
    public function getWarnings() : array
214
    {
215
        return $this->getByType(self::TYPE_WARNING);
216
    }
217
    
218
    public function getNotices() : array
219
    {
220
        return $this->getByType(self::TYPE_NOTICE);
221
    }
222
    
223
    public function getByType(string $type) : array
224
    {
225
        $results = array();
226
        
227
        foreach($this->results as $result)
228
        {
229
            if($result->isType($type))
230
            {
231
                $results[] = $result;
232
            }
233
        }
234
        
235
        return $results;
236
    }
237
    
238
    public function isType(string $type) : bool
239
    {
240
        foreach($this->results as $result)
241
        {
242
            if($result->isType($type))
243
            {
244
                return true;
245
            }
246
        }
247
        
248
        return false;
249
    }
250
    
251
    public function getSummary() : string
252
    {
253
        $lines = array();
254
        
255
        $lines[] = 'Collection #'.$this->getID();
256
        $lines[] = 'Subject: '.get_class($this->subject);
257
        
258
        foreach($this->results as $result)
259
        {
260
            $lines[] = ' - '.$result->getType().' #'.$result->getCode().' "'.$result->getMessage($result->getType()).'"';
261
        }
262
        
263
        return implode(PHP_EOL, $lines);    
264
    }
265
}
266