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

src/Event/Subscriber/RedirectSubscriber.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\Event\Subscriber;
13
14
use Ivory\HttpAdapter\Event\Events;
15
use Ivory\HttpAdapter\Event\MultiRequestSentEvent;
16
use Ivory\HttpAdapter\Event\RequestSentEvent;
17
use Ivory\HttpAdapter\Event\Redirect\Redirect;
18
use Ivory\HttpAdapter\Event\Redirect\RedirectInterface;
19
use Ivory\HttpAdapter\HttpAdapterException;
20
use Ivory\HttpAdapter\MultiHttpAdapterException;
21
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
22
23
/**
24
 * Redirect subscriber.
25
 *
26
 * @author GeLo <[email protected]>
27
 */
28
class RedirectSubscriber implements EventSubscriberInterface
29
{
30
    /** @var \Ivory\HttpAdapter\Event\Redirect\RedirectInterface */
31
    private $redirect;
32
33
    /**
34
     * Creates a redirect subscriber.
35
     *
36
     * @param \Ivory\HttpAdapter\Event\Redirect\RedirectInterface $redirect The redirect.
37
     */
38 209
    public function __construct(RedirectInterface $redirect = null)
39
    {
40 209
        $this->redirect = $redirect ?: new Redirect();
41 209
    }
42
43
    /**
44
     * Gets the redirect.
45
     *
46
     * @return \Ivory\HttpAdapter\Event\Redirect\RedirectInterface The redirect.
47
     */
48 38
    public function getRedirect()
49
    {
50 38
        return $this->redirect;
51
    }
52
53
    /**
54
     * On request sent event.
55
     *
56
     * @param \Ivory\HttpAdapter\Event\RequestSentEvent $event The request sent event.
57
     */
58 76
    public function onRequestSent(RequestSentEvent $event)
59
    {
60
        try {
61 76
            $redirectRequest = $this->redirect->createRedirectRequest(
62 76
                $event->getResponse(),
63 76
                $event->getRequest(),
64 76
                $event->getHttpAdapter()
65 72
            );
66 73
        } catch (HttpAdapterException $e) {
67 19
            $event->setException($e);
68
69 19
            return;
70
        }
71
72 57
        if ($redirectRequest === false) {
73 19
            $event->setResponse($this->redirect->prepareResponse($event->getResponse(), $event->getRequest()));
74
75 19
            return;
76
        }
77
78
        try {
79 38
            $event->setResponse($event->getHttpAdapter()->sendRequest($redirectRequest));
80 37
        } catch (HttpAdapterException $e) {
81 19
            $event->setException($e);
82
        }
83 38
    }
84
85
    /**
86
     * On multi request sent event.
87
     *
88
     * @param \Ivory\HttpAdapter\Event\MultiRequestSentEvent $event The multi request sent event.
89
     */
90 76
    public function onMultiRequestSent(MultiRequestSentEvent $event)
91
    {
92 76
        $redirectRequests = array();
93
94 76
        foreach ($event->getResponses() as $response) {
95
            try {
96 76
                $redirectRequest = $this->redirect->createRedirectRequest(
97 72
                    $response,
98 76
                    $response->getParameter('request'),
99 76
                    $event->getHttpAdapter()
100 72
                );
101 73
            } catch (HttpAdapterException $e) {
102 19
                $event->removeResponse($response);
103 19
                $event->addException($e);
104 19
                continue;
105
            }
106
107 57
            if ($redirectRequest === false) {
108 19
                $event->removeResponse($response);
109 19
                $event->addResponse($this->redirect->prepareResponse($response, $response->getParameter('request')));
110 18
            } else {
111 38
                $redirectRequests[] = $redirectRequest;
112 39
                $event->removeResponse($response);
113
            }
114 72
        }
115
116 76
        if (empty($redirectRequests)) {
117 38
            return;
118
        }
119
120
        try {
121 38
            $event->addResponses($event->getHttpAdapter()->sendRequests($redirectRequests));
122 37
        } catch (MultiHttpAdapterException $e) {
123 19
            $event->addResponses($e->getResponses());
124 19
            $event->addExceptions($e->getExceptions());
125
        }
126 38
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131 19 View Code Duplication
    public static function getSubscribedEvents()
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...
132
    {
133
        return array(
134 19
            Events::REQUEST_SENT       => array('onRequestSent', 0),
135 18
            Events::MULTI_REQUEST_SENT => array('onMultiRequestSent', 0),
136 18
        );
137
    }
138
}
139