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

ResponseBridge   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 3
c 2
b 1
f 1
lcom 0
cbo 4
dl 0
loc 27
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A fromOauth2() 0 17 3
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