Completed
Push — master ( 882899...a41010 )
by Oscar
02:34
created

AttributeMapper   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 45
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 14 2
1
<?php
2
namespace Psr7Middlewares\Middleware;
3
4
use Psr\Http\Message\ResponseInterface;
5
use Psr\Http\Message\ServerRequestInterface;
6
use Psr7Middlewares\Middleware;
7
8
/**
9
 * Provides ability to route Psr7Middlewares specific attributes into scalar attributes.
10
 *
11
 * @todo Raise an exception if no attribute found?
12
 */
13
class AttributeMapper
14
{
15
    /**
16
     * @var array
17
     */
18
    private $mapping = [];
19
20
    /**
21
     * Example:
22
     *
23
     * [
24
     *      BasicAuthentication::KEY => 'basic.username'
25
     * ]
26
     *
27
     * @param array $mapping
28
     */
29
    public function __construct(array $mapping)
30
    {
31
        $this->mapping = $mapping;
32
    }
33
34
    /**
35
     * Execute the middleware.
36
     *
37
     * @param ServerRequestInterface $request
38
     * @param ResponseInterface      $response
39
     * @param callable               $next
40
     *
41
     * @return ResponseInterface
42
     */
43
    public function __invoke(
44
        ServerRequestInterface $request,
45
        ResponseInterface $response,
46
        callable $next
47
    ) {
48
        foreach ($this->mapping as $middleware => $attribute) {
49
            $request = $request->withAttribute(
50
                $attribute,
51
                Middleware::getAttribute($request, $middleware)
52
            );
53
        }
54
55
        return $next($request, $response);
56
    }
57
}