1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the SmsGate package |
7
|
|
|
* |
8
|
|
|
* (c) SmsGate |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace SmsGate; |
15
|
|
|
|
16
|
|
|
use SmsGate\Exception\ResultNotFoundException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The collection for store results of send sms. |
20
|
|
|
* |
21
|
|
|
* @author Vitaliy Zhuk <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class ResultCollection implements \Iterator, \Countable |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var Result[] |
27
|
|
|
*/ |
28
|
|
|
private $results; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructor. |
32
|
|
|
* |
33
|
|
|
* @param Result[] ...$results |
34
|
|
|
*/ |
35
|
6 |
|
public function __construct(Result ...$results) |
36
|
|
|
{ |
37
|
6 |
|
$this->results = $results; |
38
|
6 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Find the result by phone |
42
|
|
|
* |
43
|
|
|
* @param Phone $phone |
44
|
|
|
* |
45
|
|
|
* @return Result |
46
|
|
|
* |
47
|
|
|
* @throws ResultNotFoundException |
48
|
|
|
*/ |
49
|
2 |
|
public function findByPhone(Phone $phone): Result |
50
|
|
|
{ |
51
|
2 |
|
foreach ($this->results as $result) { |
52
|
2 |
|
if ($result->getPhone()->getValue() === $phone->getValue()) { |
53
|
1 |
|
return $result; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
throw new ResultNotFoundException(sprintf( |
58
|
1 |
|
'Not found result for phone "%s".', |
59
|
1 |
|
$phone |
60
|
|
|
)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get successfully results |
65
|
|
|
* |
66
|
|
|
* @return ResultCollection |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function getSuccessfullyResults(): ResultCollection |
|
|
|
|
69
|
|
|
{ |
70
|
1 |
|
$successfullyResults = array_filter($this->results, function (Result $result) { |
71
|
1 |
|
return $result->isSuccessfully(); |
72
|
1 |
|
}); |
73
|
|
|
|
74
|
1 |
|
return new self(...$successfullyResults); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get failed results |
79
|
|
|
* |
80
|
|
|
* @return ResultCollection |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
public function getFailedResults(): ResultCollection |
|
|
|
|
83
|
|
|
{ |
84
|
1 |
|
$failedResults = array_filter($this->results, function (Result $result) { |
85
|
1 |
|
return $result->isFailed(); |
86
|
1 |
|
}); |
87
|
|
|
|
88
|
1 |
|
return new self(...$failedResults); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
2 |
|
public function current(): Result |
95
|
|
|
{ |
96
|
2 |
|
return current($this->results); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
2 |
|
public function next(): void |
103
|
|
|
{ |
104
|
2 |
|
next($this->results); |
105
|
2 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
1 |
|
public function key() |
111
|
|
|
{ |
112
|
1 |
|
return key($this->results); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
2 |
|
public function valid(): bool |
119
|
|
|
{ |
120
|
2 |
|
return key($this->results) !== null; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
2 |
|
public function rewind(): void |
127
|
|
|
{ |
128
|
2 |
|
reset($this->results); |
129
|
2 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
3 |
|
public function count(): int |
135
|
|
|
{ |
136
|
3 |
|
return count($this->results); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.