AttributeMapper   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 44
rs 10
c 0
b 0
f 0

2 Methods

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