Code Duplication    Length = 213-214 lines in 3 locations

src/Event/MultiRequestErroredEvent.php 1 location

@@ 23-235 (lines=213) @@
20
 *
21
 * @author GeLo <[email protected]>
22
 */
23
class MultiRequestErroredEvent extends AbstractEvent
24
{
25
    /** @var \Ivory\HttpAdapter\HttpAdapterException[] */
26
    private $exceptions;
27
28
    /** @var \Ivory\HttpAdapter\Message\ResponseInterface[] */
29
    private $responses = array();
30
31
    /**
32
     * Creates a multi request errored event.
33
     *
34
     * @param \Ivory\HttpAdapter\HttpAdapterInterface   $httpAdapter The http adapter.
35
     * @param \Ivory\HttpAdapter\HttpAdapterException[] $exceptions  The exceptions.
36
     */
37
    public function __construct(HttpAdapterInterface $httpAdapter, array $exceptions)
38
    {
39
        parent::__construct($httpAdapter);
40
41
        $this->setExceptions($exceptions);
42
    }
43
44
    /**
45
     * Clears the exceptions.
46
     */
47
    public function clearExceptions()
48
    {
49
        $this->exceptions = array();
50
    }
51
52
    /**
53
     * Checks if there are exceptions.
54
     *
55
     * @return boolean TRUE if there are exceptions else FALSE.
56
     */
57
    public function hasExceptions()
58
    {
59
        return !empty($this->exceptions);
60
    }
61
62
    /**
63
     * Gets the exceptions.
64
     *
65
     * @return \Ivory\HttpAdapter\HttpAdapterException[] The exceptions.
66
     */
67
    public function getExceptions()
68
    {
69
        return $this->exceptions;
70
    }
71
72
    /**
73
     * Sets the exceptions.
74
     *
75
     * @param \Ivory\HttpAdapter\HttpAdapterException[] $exceptions The exceptions.
76
     */
77
    public function setExceptions(array $exceptions)
78
    {
79
        $this->clearExceptions();
80
        $this->addExceptions($exceptions);
81
    }
82
83
    /**
84
     * Adds the exceptions.
85
     *
86
     * @param \Ivory\HttpAdapter\HttpAdapterException[] $exceptions The exceptions.
87
     */
88
    public function addExceptions(array $exceptions)
89
    {
90
        foreach ($exceptions as $exception) {
91
            $this->addException($exception);
92
        }
93
    }
94
95
    /**
96
     * Removes the exceptions.
97
     *
98
     * @param \Ivory\HttpAdapter\HttpAdapterException[] $exceptions The exceptions.
99
     */
100
    public function removeExceptions(array $exceptions)
101
    {
102
        foreach ($exceptions as $exception) {
103
            $this->removeException($exception);
104
        }
105
    }
106
107
    /**
108
     * Checks if there is an exception.
109
     *
110
     * @param \Ivory\HttpAdapter\HttpAdapterException $exception The exception.
111
     *
112
     * @return boolean TRUE if there is the exception else FALSE.
113
     */
114
    public function hasException(HttpAdapterException $exception)
115
    {
116
        return array_search($exception, $this->exceptions, true) !== false;
117
    }
118
119
    /**
120
     * Adds an exception.
121
     *
122
     * @param \Ivory\HttpAdapter\HttpAdapterException $exception The exception
123
     */
124
    public function addException(HttpAdapterException $exception)
125
    {
126
        $this->exceptions[] = $exception;
127
    }
128
129
    /**
130
     * Removes an exception.
131
     *
132
     * @param \Ivory\HttpAdapter\HttpAdapterException $exception The exception.
133
     */
134
    public function removeException(HttpAdapterException $exception)
135
    {
136
        unset($this->exceptions[array_search($exception, $this->exceptions, true)]);
137
        $this->exceptions = array_values($this->exceptions);
138
    }
139
140
    /**
141
     * Clears the responses.
142
     */
143
    public function clearResponses()
144
    {
145
        $this->responses = array();
146
    }
147
148
    /**
149
     * Checks if there are responses.
150
     *
151
     * @return boolean TRUE if there are responses else FALSE.
152
     */
153
    public function hasResponses()
154
    {
155
        return !empty($this->responses);
156
    }
157
158
    /**
159
     * Gets the responses.
160
     *
161
     * @return \Ivory\HttpAdapter\Message\ResponseInterface[] The responses.
162
     */
163
    public function getResponses()
164
    {
165
        return $this->responses;
166
    }
167
168
    /**
169
     * Sets the responses.
170
     *
171
     * @param \Ivory\HttpAdapter\Message\ResponseInterface[] $responses The responses.
172
     */
173
    public function setResponses(array $responses)
174
    {
175
        $this->clearResponses();
176
        $this->addResponses($responses);
177
    }
178
179
    /**
180
     * Adds the responses.
181
     *
182
     * @param \Ivory\HttpAdapter\Message\ResponseInterface[] $responses The responses.
183
     */
184
    public function addResponses(array $responses)
185
    {
186
        foreach ($responses as $response) {
187
            $this->addResponse($response);
188
        }
189
    }
190
191
    /**
192
     * Removes the responses.
193
     *
194
     * @param \Ivory\HttpAdapter\Message\ResponseInterface[] $responses The responses.
195
     */
196
    public function removeResponses(array $responses)
197
    {
198
        foreach ($responses as $response) {
199
            $this->removeResponse($response);
200
        }
201
    }
202
203
    /**
204
     * Checks if there is a response.
205
     *
206
     * @param \Ivory\HttpAdapter\Message\ResponseInterface $response The response.
207
     *
208
     * @return boolean TRUE if there is the response else FALSE.
209
     */
210
    public function hasResponse(ResponseInterface $response)
211
    {
212
        return array_search($response, $this->responses, true) !== false;
213
    }
214
215
    /**
216
     * Adds a response.
217
     *
218
     * @param \Ivory\HttpAdapter\Message\ResponseInterface $response The response.
219
     */
220
    public function addResponse(ResponseInterface $response)
221
    {
222
        $this->responses[] = $response;
223
    }
224
225
    /**
226
     * Removes a response.
227
     *
228
     * @param \Ivory\HttpAdapter\Message\ResponseInterface $response The response.
229
     */
230
    public function removeResponse(ResponseInterface $response)
231
    {
232
        unset($this->responses[array_search($response, $this->responses, true)]);
233
        $this->responses = array_values($this->responses);
234
    }
235
}
236

src/Event/MultiRequestSentEvent.php 1 location

@@ 23-235 (lines=213) @@
20
 *
21
 * @author GeLo <[email protected]>
22
 */
23
class MultiRequestSentEvent extends AbstractEvent
24
{
25
    /** @var \Ivory\HttpAdapter\Message\ResponseInterface[] */
26
    private $responses;
27
28
    /** @var \Ivory\HttpAdapter\HttpAdapterException[] */
29
    private $exceptions = array();
30
31
    /**
32
     * Creates a multi request sent event.
33
     *
34
     * @param \Ivory\HttpAdapter\HttpAdapterInterface        $httpAdapter The http adapter.
35
     * @param \Ivory\HttpAdapter\Message\ResponseInterface[] $responses   The responses.
36
     */
37
    public function __construct(HttpAdapterInterface $httpAdapter, array $responses)
38
    {
39
        parent::__construct($httpAdapter);
40
41
        $this->setResponses($responses);
42
    }
43
44
    /**
45
     * Clears the responses.
46
     */
47
    public function clearResponses()
48
    {
49
        $this->responses = array();
50
    }
51
52
    /**
53
     * Checks if there are responses.
54
     *
55
     * @return boolean TRUE if there are responses else FALSE.
56
     */
57
    public function hasResponses()
58
    {
59
        return !empty($this->responses);
60
    }
61
62
    /**
63
     * Gets the responses.
64
     *
65
     * @return \Ivory\HttpAdapter\Message\ResponseInterface[] The responses.
66
     */
67
    public function getResponses()
68
    {
69
        return $this->responses;
70
    }
71
72
    /**
73
     * Sets the responses.
74
     *
75
     * @param \Ivory\HttpAdapter\Message\ResponseInterface[] $responses The responses.
76
     */
77
    public function setResponses(array $responses)
78
    {
79
        $this->clearResponses();
80
        $this->addResponses($responses);
81
    }
82
83
    /**
84
     * Adds the responses.
85
     *
86
     * @param \Ivory\HttpAdapter\Message\ResponseInterface[] $responses The responses.
87
     */
88
    public function addResponses(array $responses)
89
    {
90
        foreach ($responses as $response) {
91
            $this->addResponse($response);
92
        }
93
    }
94
95
    /**
96
     * Removes the responses.
97
     *
98
     * @param \Ivory\HttpAdapter\Message\ResponseInterface[] $responses The responses.
99
     */
100
    public function removeResponses(array $responses)
101
    {
102
        foreach ($responses as $response) {
103
            $this->removeResponse($response);
104
        }
105
    }
106
107
    /**
108
     * Checks if there is a response.
109
     *
110
     * @param \Ivory\HttpAdapter\Message\ResponseInterface $response The response.
111
     *
112
     * @return boolean TRUE if there is the response else FALSE.
113
     */
114
    public function hasResponse(ResponseInterface $response)
115
    {
116
        return array_search($response, $this->responses, true) !== false;
117
    }
118
119
    /**
120
     * Adds a response.
121
     *
122
     * @param \Ivory\HttpAdapter\Message\ResponseInterface $response The response.
123
     */
124
    public function addResponse(ResponseInterface $response)
125
    {
126
        $this->responses[] = $response;
127
    }
128
129
    /**
130
     * Removes a response.
131
     *
132
     * @param \Ivory\HttpAdapter\Message\ResponseInterface $response The response.
133
     */
134
    public function removeResponse(ResponseInterface $response)
135
    {
136
        unset($this->responses[array_search($response, $this->responses, true)]);
137
        $this->responses = array_values($this->responses);
138
    }
139
140
    /**
141
     * Clears the exceptions.
142
     */
143
    public function clearExceptions()
144
    {
145
        $this->exceptions = array();
146
    }
147
148
    /**
149
     * Checks if there are exceptions.
150
     *
151
     * @return boolean TRUE if there are exceptions else FALSE.
152
     */
153
    public function hasExceptions()
154
    {
155
        return !empty($this->exceptions);
156
    }
157
158
    /**
159
     * Gets the exceptions.
160
     *
161
     * @return \Ivory\HttpAdapter\HttpAdapterException[] The exceptions.
162
     */
163
    public function getExceptions()
164
    {
165
        return $this->exceptions;
166
    }
167
168
    /**
169
     * Sets the exceptions.
170
     *
171
     * @param \Ivory\HttpAdapter\HttpAdapterException[] $exceptions The exceptions.
172
     */
173
    public function setExceptions(array $exceptions)
174
    {
175
        $this->clearExceptions();
176
        $this->addExceptions($exceptions);
177
    }
178
179
    /**
180
     * Adds the exceptions.
181
     *
182
     * @param \Ivory\HttpAdapter\HttpAdapterException[] $exceptions The exceptions.
183
     */
184
    public function addExceptions(array $exceptions)
185
    {
186
        foreach ($exceptions as $exception) {
187
            $this->addException($exception);
188
        }
189
    }
190
191
    /**
192
     * Removes the exceptions.
193
     *
194
     * @param \Ivory\HttpAdapter\HttpAdapterException[] $exceptions The exceptions.
195
     */
196
    public function removeExceptions(array $exceptions)
197
    {
198
        foreach ($exceptions as $exception) {
199
            $this->removeException($exception);
200
        }
201
    }
202
203
    /**
204
     * Checks if there is an exception.
205
     *
206
     * @param \Ivory\HttpAdapter\HttpAdapterException $exception The exception.
207
     *
208
     * @return boolean TRUE if there is the exception else FALSE.
209
     */
210
    public function hasException(HttpAdapterException $exception)
211
    {
212
        return array_search($exception, $this->exceptions, true) !== false;
213
    }
214
215
    /**
216
     * Adds an exception.
217
     *
218
     * @param \Ivory\HttpAdapter\HttpAdapterException $exception The exception
219
     */
220
    public function addException(HttpAdapterException $exception)
221
    {
222
        $this->exceptions[] = $exception;
223
    }
224
225
    /**
226
     * Removes an exception.
227
     *
228
     * @param \Ivory\HttpAdapter\HttpAdapterException $exception The exception.
229
     */
230
    public function removeException(HttpAdapterException $exception)
231
    {
232
        unset($this->exceptions[array_search($exception, $this->exceptions, true)]);
233
        $this->exceptions = array_values($this->exceptions);
234
    }
235
}
236

src/MultiHttpAdapterException.php 1 location

@@ 21-234 (lines=214) @@
18
 *
19
 * @author GeLo <[email protected]>
20
 */
21
class MultiHttpAdapterException extends \Exception
22
{
23
    /** @var array */
24
    private $exceptions;
25
26
    /** @var array */
27
    private $responses;
28
29
    /**
30
     * Creates a multi http adapter exception.
31
     *
32
     * @param array $exceptions The exceptions.
33
     * @param array $responses  The responses.
34
     */
35
    public function __construct(array $exceptions = array(), array $responses = array())
36
    {
37
        parent::__construct('An error occurred when sending multiple requests.');
38
39
        $this->setExceptions($exceptions);
40
        $this->setResponses($responses);
41
    }
42
43
    /**
44
     * Clears the exceptions.
45
     */
46
    public function clearExceptions()
47
    {
48
        $this->exceptions = array();
49
    }
50
51
    /**
52
     * Checks if there are exceptions.
53
     *
54
     * @return boolean TRUE if there are exceptions else FALSE.
55
     */
56
    public function hasExceptions()
57
    {
58
        return !empty($this->exceptions);
59
    }
60
61
    /**
62
     * Gets the exceptions.
63
     *
64
     * @return array The exceptions.
65
     */
66
    public function getExceptions()
67
    {
68
        return $this->exceptions;
69
    }
70
71
    /**
72
     * Sets the exceptions.
73
     *
74
     * @param array $exceptions The exceptions.
75
     */
76
    public function setExceptions(array $exceptions)
77
    {
78
        $this->clearExceptions();
79
        $this->addExceptions($exceptions);
80
    }
81
82
    /**
83
     * Adds the exceptions.
84
     *
85
     * @param array $exceptions The exceptions.
86
     */
87
    public function addExceptions(array $exceptions)
88
    {
89
        foreach ($exceptions as $exception) {
90
            $this->addException($exception);
91
        }
92
    }
93
94
    /**
95
     * Removes the exceptions.
96
     *
97
     * @param array $exceptions The exceptions.
98
     */
99
    public function removeExceptions(array $exceptions)
100
    {
101
        foreach ($exceptions as $exception) {
102
            $this->removeException($exception);
103
        }
104
    }
105
106
    /**
107
     * Checks if there is an exception.
108
     *
109
     * @param \Ivory\HttpAdapter\HttpAdapterException $exception The exception.
110
     *
111
     * @return boolean TRUE if there is the exception else FALSE.
112
     */
113
    public function hasException(HttpAdapterException $exception)
114
    {
115
        return array_search($exception, $this->exceptions, true) !== false;
116
    }
117
118
    /**
119
     * Adds an exception.
120
     *
121
     * @param \Ivory\HttpAdapter\HttpAdapterException $exception The exception.
122
     */
123
    public function addException(HttpAdapterException $exception)
124
    {
125
        $this->exceptions[] = $exception;
126
    }
127
128
    /**
129
     * Removes an exception.
130
     *
131
     * @param \Ivory\HttpAdapter\HttpAdapterException $exception The exception.
132
     */
133
    public function removeException(HttpAdapterException $exception)
134
    {
135
        unset($this->exceptions[array_search($exception, $this->exceptions, true)]);
136
        $this->exceptions = array_values($this->exceptions);
137
    }
138
139
    /**
140
     * Clears the responses.
141
     */
142
    public function clearResponses()
143
    {
144
        $this->responses = array();
145
    }
146
147
    /**
148
     * Checks if there are exceptions.
149
     *
150
     * @return boolean TRUE if there are exceptions else FALSE.
151
     */
152
    public function hasResponses()
153
    {
154
        return !empty($this->responses);
155
    }
156
157
    /**
158
     * Gets the responses.
159
     *
160
     * @return array The responses.
161
     */
162
    public function getResponses()
163
    {
164
        return $this->responses;
165
    }
166
167
    /**
168
     * Sets the responses.
169
     *
170
     * @param array $responses The responses.
171
     */
172
    public function setResponses(array $responses)
173
    {
174
        $this->clearResponses();
175
        $this->addResponses($responses);
176
    }
177
178
    /**
179
     * Adds the responses.
180
     *
181
     * @param array $responses The responses.
182
     */
183
    public function addResponses(array $responses)
184
    {
185
        foreach ($responses as $response) {
186
            $this->addResponse($response);
187
        }
188
    }
189
190
    /**
191
     * Removes the responses.
192
     *
193
     * @param array $responses The responses.
194
     */
195
    public function removeResponses(array $responses)
196
    {
197
        foreach ($responses as $response) {
198
            $this->removeResponse($response);
199
        }
200
    }
201
202
    /**
203
     * Checks if there is a response.
204
     *
205
     * @param \Ivory\HttpAdapter\Message\ResponseInterface $response The response.
206
     *
207
     * @return boolean TRUE if there is the response else FALSE.
208
     */
209
    public function hasResponse(ResponseInterface $response)
210
    {
211
        return array_search($response, $this->responses, true) !== false;
212
    }
213
214
    /**
215
     * Adds a response.
216
     *
217
     * @param \Ivory\HttpAdapter\Message\ResponseInterface $response The response.
218
     */
219
    public function addResponse(ResponseInterface $response)
220
    {
221
        $this->responses[] = $response;
222
    }
223
224
    /**
225
     * Removes a response.
226
     *
227
     * @param \Ivory\HttpAdapter\Message\ResponseInterface $response The response.
228
     */
229
    public function removeResponse(ResponseInterface $response)
230
    {
231
        unset($this->responses[array_search($response, $this->responses, true)]);
232
        $this->responses = array_values($this->responses);
233
    }
234
}
235