Completed
Push — master ( 955d05...19c28e )
by David
03:37
created

src/BatchResult.php (2 issues)

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
namespace Http\Client\Common;
4
5
use Http\Client\Exception;
6
use Psr\Http\Message\RequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * Responses and exceptions returned from parallel request execution.
11
 *
12
 * @author Márk Sági-Kazár <[email protected]>
13
 */
14
final class BatchResult
15
{
16
    /**
17
     * @var \SplObjectStorage
18
     */
19
    private $responses;
20
21
    /**
22
     * @var \SplObjectStorage
23
     */
24
    private $exceptions;
25
26 10
    public function __construct()
27
    {
28 10
        $this->responses = new \SplObjectStorage();
29 10
        $this->exceptions = new \SplObjectStorage();
30 10
    }
31
32
    /**
33
     * Checks if there are any successful responses at all.
34
     *
35
     * @return bool
36
     */
37 1
    public function hasResponses()
38
    {
39 1
        return $this->responses->count() > 0;
40
    }
41
42
    /**
43
     * Returns all successful responses.
44
     *
45
     * @return ResponseInterface[]
46
     */
47 2
    public function getResponses()
48
    {
49 2
        $responses = [];
50
51 2
        foreach ($this->responses as $request) {
52 2
            $responses[] = $this->responses[$request];
53
        }
54
55 2
        return $responses;
56
    }
57
58
    /**
59
     * Checks if there is a successful response for a request.
60
     *
61
     * @param RequestInterface $request
62
     *
63
     * @return bool
64
     */
65 2
    public function isSuccessful(RequestInterface $request)
66
    {
67 2
        return $this->responses->contains($request);
68
    }
69
70
    /**
71
     * Returns the response for a successful request.
72
     *
73
     * @param RequestInterface $request
74
     *
75
     * @return ResponseInterface
76
     *
77
     * @throws \UnexpectedValueException If request was not part of the batch or failed
78
     */
79 2 View Code Duplication
    public function getResponseFor(RequestInterface $request)
0 ignored issues
show
This method 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...
80
    {
81
        try {
82 2
            return $this->responses[$request];
83 1
        } catch (\UnexpectedValueException $e) {
84 1
            throw new \UnexpectedValueException('Request not found', $e->getCode(), $e);
85
        }
86
    }
87
88
    /**
89
     * Adds a response in an immutable way.
90
     *
91
     * @param RequestInterface  $request
92
     * @param ResponseInterface $response
93
     *
94
     * @return BatchResult the new BatchResult with this request-response pair added to it
95
     */
96 6
    public function addResponse(RequestInterface $request, ResponseInterface $response)
97
    {
98 6
        $new = clone $this;
99 6
        $new->responses->attach($request, $response);
100
101 6
        return $new;
102
    }
103
104
    /**
105
     * Checks if there are any unsuccessful requests at all.
106
     *
107
     * @return bool
108
     */
109 2
    public function hasExceptions()
110
    {
111 2
        return $this->exceptions->count() > 0;
112
    }
113
114
    /**
115
     * Returns all exceptions for the unsuccessful requests.
116
     *
117
     * @return Exception[]
118
     */
119
    public function getExceptions()
120
    {
121
        $exceptions = [];
122
123
        foreach ($this->exceptions as $request) {
124
            $exceptions[] = $this->exceptions[$request];
125
        }
126
127
        return $exceptions;
128
    }
129
130
    /**
131
     * Checks if there is an exception for a request, meaning the request failed.
132
     *
133
     * @param RequestInterface $request
134
     *
135
     * @return bool
136
     */
137 1
    public function isFailed(RequestInterface $request)
138
    {
139 1
        return $this->exceptions->contains($request);
140
    }
141
142
    /**
143
     * Returns the exception for a failed request.
144
     *
145
     * @param RequestInterface $request
146
     *
147
     * @return Exception
148
     *
149
     * @throws \UnexpectedValueException If request was not part of the batch or was successful
150
     */
151 1 View Code Duplication
    public function getExceptionFor(RequestInterface $request)
0 ignored issues
show
This method 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...
152
    {
153
        try {
154 1
            return $this->exceptions[$request];
155
        } catch (\UnexpectedValueException $e) {
156
            throw new \UnexpectedValueException('Request not found', $e->getCode(), $e);
157
        }
158
    }
159
160
    /**
161
     * Adds an exception in an immutable way.
162
     *
163
     * @param RequestInterface $request
164
     * @param Exception        $exception
165
     *
166
     * @return BatchResult the new BatchResult with this request-exception pair added to it
167
     */
168 2
    public function addException(RequestInterface $request, Exception $exception)
169
    {
170 2
        $new = clone $this;
171 2
        $new->exceptions->attach($request, $exception);
172
173 2
        return $new;
174
    }
175
176 6
    public function __clone()
177
    {
178 6
        $this->responses = clone $this->responses;
179 6
        $this->exceptions = clone $this->exceptions;
180 6
    }
181
}
182