BodyExtractor::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Equip\Auth\Credentials;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use Equip\Auth\Credentials;
7
8
/**
9
 * Extracts credentials from top-level properties of a request body.
10
 */
11
class BodyExtractor implements ExtractorInterface
12
{
13
    /**
14
     * @var string
15
     */
16
    private $identifier;
17
18
    /**
19
     * @var string
20
     */
21
    private $password;
22
23
    /**
24
     * @param string $identifier
25
     *  Name of the property that identifies the user
26
     * @param string $password
27
     *  Name of the property that contains the user password
28
     */
29 5
    public function __construct($identifier = 'username', $password = 'password')
30
    {
31 5
        $this->identifier = $identifier;
32 5
        $this->password = $password;
33 5
    }
34
35
    /**
36
     * @inheritDoc
37
     */
38 5
    public function getCredentials(ServerRequestInterface $request)
39
    {
40 5
        $body = $request->getParsedBody();
41
42 5
        if (empty($body[$this->identifier]) || empty($body[$this->password])) {
43 4
            return null;
44
        }
45
46 2
        return new Credentials($body[$this->identifier], $body[$this->password]);
47
    }
48
}
49