QueryParam   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 30
ccs 10
cts 10
cp 1
rs 10
c 1
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A authenticate() 0 15 1
A __construct() 0 3 1
1
<?php
2
3
namespace Http\Message\Authentication;
4
5
use Http\Message\Authentication;
6
use Psr\Http\Message\RequestInterface;
7
8
/**
9
 * Authenticate a PSR-7 Request by adding parameters to its query.
10
 *
11
 * Note: Although in some cases it can be useful, we do not recommend using query parameters for authentication.
12
 * Credentials in the URL is generally unsafe as they are not encrypted, anyone can see them.
13
 *
14
 * @author Márk Sági-Kazár <[email protected]>
15
 */
16
final class QueryParam implements Authentication
17
{
18
    /**
19
     * @var array
20
     */
21
    private $params = [];
22
23
    public function __construct(array $params)
24
    {
25
        $this->params = $params;
26 3
    }
27
28 3
    /**
29 3
     * {@inheritdoc}
30
     */
31
    public function authenticate(RequestInterface $request)
32
    {
33
        $uri = $request->getUri();
34 1
        $query = $uri->getQuery();
35
        $params = [];
36 1
37 1
        parse_str($query, $params);
38 1
39
        $params = array_merge($params, $this->params);
40 1
41
        $query = http_build_query($params, null, '&');
42 1
43
        $uri = $uri->withQuery($query);
44 1
45
        return $request->withUri($uri);
46 1
    }
47
}
48