HttpRequestAdapter   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 0
loc 109
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A toPsr7() 0 21 3
A fromPsr7() 0 14 1
B getUri() 0 26 6
A getServerVars() 0 4 1
A setServerVars() 0 5 1
1
<?php
2
3
namespace Robbie\Psr7;
4
5
use GuzzleHttp\Psr7\ServerRequest;
6
use SilverStripe\Control\HTTPRequest;
7
8
/**
9
 * @package psr7-adapters
10
 */
11
class HttpRequestAdapter extends AbstractHttpAdapter
12
{
13
    /**
14
     * @var array
15
     */
16
    protected $serverVars;
17
18
    /**
19
     * Set up the server vars - they can be overridden if required
20
     */
21
    public function __construct()
22
    {
23
        $this->setServerVars($_SERVER);
24
    }
25
26
    /**
27
     * {@inheritDoc}
28
     */
29
    public function toPsr7($input)
30
    {
31
        $request = new ServerRequest(
32
            $input->httpMethod(),
0 ignored issues
show
Bug introduced by Robbie Averill
The method httpMethod does only exist in SilverStripe\Control\HTTPRequest, but not in SilverStripe\Control\HTTPResponse.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
33
            $this->getUri($input->getURL()),
0 ignored issues
show
Bug introduced by Robbie Averill
The method getURL does only exist in SilverStripe\Control\HTTPRequest, but not in SilverStripe\Control\HTTPResponse.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
34
            $input->getHeaders(),
35
            $input->getBody(),
36
            $this->getProtocolVersion(),
37
            $this->getServerVars()
38
        );
39
40
        if (!empty($input->getVars())) {
41
            $request = $request->withQueryParams($input->getVars());
0 ignored issues
show
Bug introduced by Robbie Averill
The method getVars does only exist in SilverStripe\Control\HTTPRequest, but not in SilverStripe\Control\HTTPResponse.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
42
        }
43
44
        if (!empty($input->postVars())) {
45
            $request = $request->withParsedBody($input->postVars());
0 ignored issues
show
Bug introduced by Robbie Averill
The method postVars does only exist in SilverStripe\Control\HTTPRequest, but not in SilverStripe\Control\HTTPResponse.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
46
        }
47
48
        return $request;
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54
    public function fromPsr7($input)
55
    {
56
        $adapted = new HTTPRequest(
57
            $input->getMethod(),
0 ignored issues
show
Bug introduced by Robbie Averill
It seems like you code against a concrete implementation and not the interface Psr\Http\Message\MessageInterface as the method getMethod() does only exist in the following implementations of said interface: GuzzleHttp\Psr7\Request, GuzzleHttp\Psr7\ServerRequest.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
58
            (string) $input->getUri()->getPath(),
0 ignored issues
show
Bug introduced by Robbie Averill
It seems like you code against a concrete implementation and not the interface Psr\Http\Message\MessageInterface as the method getUri() does only exist in the following implementations of said interface: GuzzleHttp\Psr7\Request, GuzzleHttp\Psr7\ServerRequest.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
59
            $input->getQueryParams(),
0 ignored issues
show
Bug introduced by Robbie Averill
It seems like you code against a concrete implementation and not the interface Psr\Http\Message\MessageInterface as the method getQueryParams() does only exist in the following implementations of said interface: GuzzleHttp\Psr7\ServerRequest.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
60
            $input->getParsedBody(),
0 ignored issues
show
Bug introduced by Robbie Averill
It seems like you code against a concrete implementation and not the interface Psr\Http\Message\MessageInterface as the method getParsedBody() does only exist in the following implementations of said interface: GuzzleHttp\Psr7\ServerRequest.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
61
            (string) $input->getBody()
62
        );
63
64
        $this->importHeaders($input, $adapted);
65
66
        return $adapted;
67
    }
68
69
    /**
70
     * Get the full request URI (can be empty, but probably won't be)
71
     *
72
     * @param  string $path
73
     * @return string
74
     */
75
    public function getUri($path)
76
    {
77
        $vars = $this->getServerVars();
78
79
        $uri = '';
80
        $protocol = (isset($vars['HTTPS']) || $vars['SERVER_PORT'] === '443') ? 'https' : 'http';
81
        $uri .= $protocol . '://';
82
83
        if (!empty($vars['PHP_AUTH_USER'])) {
84
            $uri .= $vars['PHP_AUTH_USER'];
85
86
            if (!empty($vars['PHP_AUTH_PW'])) {
87
                $uri .= ':' . $vars['PHP_AUTH_PW'];
88
            }
89
90
            $uri .= '@';
91
        }
92
93
        if (!empty($vars['HTTP_HOST'])) {
94
            $uri .= $vars['HTTP_HOST'];
95
        }
96
97
        $uri .= '/' . ltrim($path, '/');
98
99
        return $uri;
100
    }
101
102
    /**
103
     * @return array
104
     */
105
    public function getServerVars()
106
    {
107
        return $this->serverVars;
108
    }
109
110
    /**
111
     * @param  array $vars
112
     * @return $this
113
     */
114
    public function setServerVars(array $vars)
115
    {
116
        $this->serverVars = $vars;
117
        return $this;
118
    }
119
}
120