Completed
Push — master ( e7d6b1...168e62 )
by Risan Bagja
02:25 queued 45s
created

createTemporaryCredentialsFromResponse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Risan\OAuth1\Credentials;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Risan\OAuth1\Credentials\CredentialsFactoryInterface;
7
8
class CredentialsFactory implements CredentialsFactoryInterface
9
{
10
    /**
11
     * {@inheritDoc}
12
     */
13 5
    public function createTemporaryCredentialsFromResponse(ResponseInterface $response)
14
    {
15 5
        $parameters = $this->getParametersFromResponse($response);
16
17 5
        $missingParameterKey = $this->getMissingParameterKey($parameters, [
0 ignored issues
show
Bug introduced by
It seems like $parameters defined by $this->getParametersFromResponse($response) on line 15 can also be of type null; however, Risan\OAuth1\Credentials...etMissingParameterKey() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
18 5
            'oauth_token',
19
            'oauth_token_secret',
20
            'oauth_callback_confirmed',
21
        ]);
22
23 5
        if (null !== $missingParameterKey) {
24 3
            throw new CredentialsException("Unable to parse temporary credentials response. Missing parameter: {$missingParameterKey}.");
25
        }
26
27 2
        if ($parameters['oauth_callback_confirmed'] !== 'true') {
28 1
            throw new CredentialsException('Unable to parse temporary credentials response. Callback URI is not valid.');
29
        }
30
31 1
        return new TemporaryCredentials($parameters['oauth_token'], $parameters['oauth_token_secret']);
32
    }
33
34
    /**
35
     * {@inheritDoc}
36
     */
37 3
    public function createTokenCredentialsFromResponse(ResponseInterface $response)
38
    {
39 3
        $parameters = $this->getParametersFromResponse($response);
40
41 3
        $missingParameterKey = $this->getMissingParameterKey($parameters, [
0 ignored issues
show
Bug introduced by
It seems like $parameters defined by $this->getParametersFromResponse($response) on line 39 can also be of type null; however, Risan\OAuth1\Credentials...etMissingParameterKey() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
42 3
            'oauth_token',
43
            'oauth_token_secret',
44
        ]);
45
46 3
        if (null !== $missingParameterKey) {
47 2
            throw new CredentialsException("Unable to parse token credentials response. Missing parameter: {$missingParameterKey}.");
48
        }
49
50 1
        return new TokenCredentials($parameters['oauth_token'], $parameters['oauth_token_secret']);
51
    }
52
53
    /**
54
     * Get parameters from response.
55
     *
56
     * @param  \Psr\Http\Message\ResponseInterface $response
57
     * @return array
58
     */
59 9
    public function getParametersFromResponse(ResponseInterface $response)
60
    {
61 9
        $contents = $response->getBody()->getContents();
62
63 9
        parse_str($contents, $parameters);
64
65 9
        return $parameters;
66
    }
67
68
    /**
69
     * Get missing parameter's key.
70
     *
71
     * @param  array  $parameters
72
     * @param  array  $requiredKeys
73
     * @return string|null
74
     */
75 9
    public function getMissingParameterKey(array $parameters, array $requiredKeys = [])
76
    {
77 9
        foreach ($requiredKeys as $key) {
78 9
            if (! isset($parameters[$key])) {
79 9
                return $key;
80
            }
81
        }
82 4
    }
83
}
84