Completed
Push — master ( 96f6be...864599 )
by Chad
9s
created

ResponseBridge::fromOauth2()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 17
rs 9.4285
cc 3
eloc 11
nc 4
nop 1
1
<?php
2
namespace Chadicus\Slim\OAuth2\Http;
3
4
use Slim\Http\Headers;
5
use Slim\Http\Response;
6
use Slim\Http\Stream;
7
use OAuth2;
8
9
/**
10
 * Static utility class for bridging OAuth2 responses to PSR-7 responses.
11
 */
12
class ResponseBridge
13
{
14
    /**
15
     * Copies values from the given Oauth2\Response to a Slim Response.
16
     *
17
     * @param OAuth2\Response $oauth2Response The OAuth2 server response.
18
     *
19
     * @return Response
20
     */
21
    final public static function fromOauth2(OAuth2\Response $oauth2Response)
22
    {
23
        $headers = new Headers();
24
        foreach ($oauth2Response->getHttpHeaders() as $key => $value) {
25
            $headers->add($key, explode(', ', $value));
26
        }
27
28
        $body = new Stream(fopen('php://temp', 'r'));
29
        if (!empty($oauth2Response->getParameters())) {
30
            $stream = fopen('php://memory','r+');
31
            fwrite($stream, $oauth2Response->getResponseBody());
32
            rewind($stream);
33
            $body = new Stream($stream);
34
        }
35
36
        return new Response($oauth2Response->getStatusCode(), $headers, $body);
37
    }
38
}
39