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

src/EventDispatcherHttpAdapter.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
/*
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\Event\Events;
15
use Ivory\HttpAdapter\Event\RequestErroredEvent;
16
use Ivory\HttpAdapter\Event\MultiRequestErroredEvent;
17
use Ivory\HttpAdapter\Event\MultiRequestSentEvent;
18
use Ivory\HttpAdapter\Event\MultiRequestCreatedEvent;
19
use Ivory\HttpAdapter\Event\RequestSentEvent;
20
use Ivory\HttpAdapter\Event\RequestCreatedEvent;
21
use Ivory\HttpAdapter\Message\InternalRequestInterface;
22
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
23
24
/**
25
 * Event dispatcher http adapter.
26
 *
27
 * @author GeLo <[email protected]>
28
 */
29
class EventDispatcherHttpAdapter extends PsrHttpAdapterDecorator
30
{
31
    /** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface */
32
    private $eventDispatcher;
33
34
    /**
35
     * @param \Ivory\HttpAdapter\PsrHttpAdapterInterface                  $httpAdapter
36
     * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
37
     */
38 228
    public function __construct(PsrHttpAdapterInterface $httpAdapter, EventDispatcherInterface $eventDispatcher)
39
    {
40 228
        parent::__construct($httpAdapter);
41
42 228
        $this->eventDispatcher = $eventDispatcher;
43 228
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 133
    protected function doSendInternalRequest(InternalRequestInterface $internalRequest)
49
    {
50
        try {
51 133
            $this->eventDispatcher->dispatch(
52 133
                Events::REQUEST_CREATED,
53 133
                $requestCreatedEvent = new RequestCreatedEvent($this, $internalRequest)
54 126
            );
55
56 133
            if ($requestCreatedEvent->hasException()) {
57 19
                throw $requestCreatedEvent->getException();
58
            }
59
60 114
            $response = $requestCreatedEvent->hasResponse()
61 109
                ? $requestCreatedEvent->getResponse()
62 114
                : parent::doSendInternalRequest($requestCreatedEvent->getRequest());
63
64 76
            $this->eventDispatcher->dispatch(
65 76
                Events::REQUEST_SENT,
66 76
                $requestSentEvent = new RequestSentEvent($this, $requestCreatedEvent->getRequest(), $response)
67 72
            );
68
69 76
            if ($requestSentEvent->hasException()) {
70 19
                throw $requestSentEvent->getException();
71
            }
72
73 57
            $response = $requestSentEvent->getResponse();
74 130
        } catch (HttpAdapterException $e) {
75 76
            $e->setRequest($internalRequest);
76 76
            $e->setResponse(isset($response) ? $response : null);
77
78 76
            $this->eventDispatcher->dispatch(
79 76
                Events::REQUEST_ERRORED,
80 76
                $exceptionEvent = new RequestErroredEvent($this, $e)
81 72
            );
82
83 76
            if ($exceptionEvent->hasResponse()) {
84 19
                return $exceptionEvent->getResponse();
85
            }
86
87 57
            throw $exceptionEvent->getException();
88
        }
89
90 57
        return $response;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 95
    protected function doSendInternalRequests(array $internalRequests)
97
    {
98 95
        $responses = [];
99 95
        $exceptions = [];
100
101 95 View Code Duplication
        if (!empty($internalRequests)) {
0 ignored issues
show
This code seems to be duplicated across 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...
102 95
            $this->eventDispatcher->dispatch(
103 95
                Events::MULTI_REQUEST_CREATED,
104 95
                $multiRequestCreatedEvent = new MultiRequestCreatedEvent($this, $internalRequests)
105 90
            );
106
107 95
            $internalRequests = $multiRequestCreatedEvent->getRequests();
108 95
            $responses = $multiRequestCreatedEvent->getResponses();
109 95
            $exceptions = $multiRequestCreatedEvent->getExceptions();
110 90
        }
111
112
        try {
113 95
            $responses = array_merge($responses, parent::doSendInternalRequests($internalRequests));
114 92
        } catch (MultiHttpAdapterException $e) {
115 38
            $responses = array_merge($responses, $e->getResponses());
116 38
            $exceptions = array_merge($exceptions, $e->getExceptions());
117
        }
118
119 95 View Code Duplication
        if (!empty($responses)) {
0 ignored issues
show
This code seems to be duplicated across 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...
120 57
            $this->eventDispatcher->dispatch(
121 57
                Events::MULTI_REQUEST_SENT,
122 57
                $requestSentEvent = new MultiRequestSentEvent($this, $responses)
123 54
            );
124
125 57
            $exceptions = array_merge($exceptions, $requestSentEvent->getExceptions());
126 57
            $responses = $requestSentEvent->getResponses();
127 54
        }
128
129 95
        if (!empty($exceptions)) {
130 57
            $this->eventDispatcher->dispatch(
131 57
                Events::MULTI_REQUEST_ERRORED,
132 57
                $exceptionEvent = new MultiRequestErroredEvent($this, $exceptions)
133 54
            );
134
135 57
            $responses = array_merge($responses, $exceptionEvent->getResponses());
136 57
            $exceptions = $exceptionEvent->getExceptions();
137
138 57
            if (!empty($exceptions)) {
139 38
                throw new MultiHttpAdapterException($exceptions, $responses);
140
            }
141 18
        }
142
143 57
        return $responses;
144
    }
145
}
146