QueryParam::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 1
b 0
f 0
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