BodyExtractor   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 38
ccs 9
cts 9
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getCredentials() 0 10 3
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