Completed
Pull Request — master (#129)
by Eric
257:01 queued 192:06
created

src/MultiHttpAdapterException.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the Ivory Http Adapter package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\HttpAdapter;
13
14
use Ivory\HttpAdapter\Message\ResponseInterface;
15
16
/**
17
 * Multi http adapter exception.
18
 *
19
 * @author GeLo <[email protected]>
20
 */
21 View Code Duplication
class MultiHttpAdapterException extends \Exception
0 ignored issues
show
This class seems to be duplicated in your project.

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.

Loading history...
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 751
    public function __construct(array $exceptions = array(), array $responses = array())
36
    {
37 751
        parent::__construct('An error occurred when sending multiple requests.');
38
39 751
        $this->setExceptions($exceptions);
40 751
        $this->setResponses($responses);
41 751
    }
42
43
    /**
44
     * Clears the exceptions.
45
     */
46 742
    public function clearExceptions()
47
    {
48 742
        $this->exceptions = array();
49 742
    }
50
51
    /**
52
     * Checks if there are exceptions.
53
     *
54
     * @return boolean TRUE if there are exceptions else FALSE.
55
     */
56 114
    public function hasExceptions()
57
    {
58 114
        return !empty($this->exceptions);
59
    }
60
61
    /**
62
     * Gets the exceptions.
63
     *
64
     * @return array The exceptions.
65
     */
66 590
    public function getExceptions()
67
    {
68 590
        return $this->exceptions;
69
    }
70
71
    /**
72
     * Sets the exceptions.
73
     *
74
     * @param array $exceptions The exceptions.
75
     */
76 742
    public function setExceptions(array $exceptions)
77
    {
78 742
        $this->clearExceptions();
79 742
        $this->addExceptions($exceptions);
80 742
    }
81
82
    /**
83
     * Adds the exceptions.
84
     *
85
     * @param array $exceptions The exceptions.
86
     */
87 742
    public function addExceptions(array $exceptions)
88
    {
89 742
        foreach ($exceptions as $exception) {
90 571
            $this->addException($exception);
91 703
        }
92 742
    }
93
94
    /**
95
     * Removes the exceptions.
96
     *
97
     * @param array $exceptions The exceptions.
98
     */
99 19
    public function removeExceptions(array $exceptions)
100
    {
101 19
        foreach ($exceptions as $exception) {
102 19
            $this->removeException($exception);
103 18
        }
104 19
    }
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 95
    public function hasException(HttpAdapterException $exception)
114
    {
115 95
        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 609
    public function addException(HttpAdapterException $exception)
124
    {
125 609
        $this->exceptions[] = $exception;
126 609
    }
127
128
    /**
129
     * Removes an exception.
130
     *
131
     * @param \Ivory\HttpAdapter\HttpAdapterException $exception The exception.
132
     */
133 38
    public function removeException(HttpAdapterException $exception)
134
    {
135 38
        unset($this->exceptions[array_search($exception, $this->exceptions, true)]);
136 38
        $this->exceptions = array_values($this->exceptions);
137 38
    }
138
139
    /**
140
     * Clears the responses.
141
     */
142 742
    public function clearResponses()
143
    {
144 742
        $this->responses = array();
145 742
    }
146
147
    /**
148
     * Checks if there are exceptions.
149
     *
150
     * @return boolean TRUE if there are exceptions else FALSE.
151
     */
152 171
    public function hasResponses()
153
    {
154 171
        return !empty($this->responses);
155
    }
156
157
    /**
158
     * Gets the responses.
159
     *
160
     * @return array The responses.
161
     */
162 571
    public function getResponses()
163
    {
164 571
        return $this->responses;
165
    }
166
167
    /**
168
     * Sets the responses.
169
     *
170
     * @param array $responses The responses.
171
     */
172 742
    public function setResponses(array $responses)
173
    {
174 742
        $this->clearResponses();
175 742
        $this->addResponses($responses);
176 742
    }
177
178
    /**
179
     * Adds the responses.
180
     *
181
     * @param array $responses The responses.
182
     */
183 742
    public function addResponses(array $responses)
184
    {
185 742
        foreach ($responses as $response) {
186 533
            $this->addResponse($response);
187 703
        }
188 742
    }
189
190
    /**
191
     * Removes the responses.
192
     *
193
     * @param array $responses The responses.
194
     */
195 19
    public function removeResponses(array $responses)
196
    {
197 19
        foreach ($responses as $response) {
198 19
            $this->removeResponse($response);
199 18
        }
200 19
    }
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 95
    public function hasResponse(ResponseInterface $response)
210
    {
211 95
        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 571
    public function addResponse(ResponseInterface $response)
220
    {
221 571
        $this->responses[] = $response;
222 571
    }
223
224
    /**
225
     * Removes a response.
226
     *
227
     * @param \Ivory\HttpAdapter\Message\ResponseInterface $response The response.
228
     */
229 38
    public function removeResponse(ResponseInterface $response)
230
    {
231 38
        unset($this->responses[array_search($response, $this->responses, true)]);
232 38
        $this->responses = array_values($this->responses);
233 38
    }
234
}
235