Test Failed
Push — master ( 13c8bf...f549ef )
by Florian
11:18
created

SessionMiddleware::process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Phauthentic\Infrastructure\Http\Session\Middleware;
6
7
use Phauthentic\Session\SessionInterface;
0 ignored issues
show
Bug introduced by
The type Phauthentic\Session\SessionInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\MiddlewareInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
use RuntimeException;
13
14
/**
15
 * PSR 15 Middleware
16
 */
17
class SessionMiddleware implements MiddlewareInterface
18
{
19
    /**
20
     * Session Attribute in the request object
21
     *
22
     * @var string
23
     */
24
    protected $sessionAttribute = 'session';
25
26
    /**
27
     * Session Object
28
     *
29
     * @var \Phauthentic\Session\SessionInterface
30
     */
31
    protected $session;
32
33
    /**
34
     * Constructor.
35
     *
36
     * @param \Phauthentic\Session\SessionInterface
37
     * @param \Psr\Http\Message\ResponseFactoryInterface $responseFactory Factory.
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ResponseFactoryInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
38
     */
39
    public function __construct(
40
        SessionInterface $session
41
    ) {
42
        $this->session = $session;
43
    }
44
45
    /**
46
     * Sets the identity attribute
47
     *
48
     * @param string $attribute Attribute name
49
     * @return $this
50
     */
51
    public function setSessionAttribute(string $attribute): self
52
    {
53
        $this->sessionAttribute = $attribute;
54
55
        return $this;
56
    }
57
58
    /**
59
     * Adds an attribute to the request and returns a modified request.
60
     *
61
     * @param ServerRequestInterface $request Request.
62
     * @param string $name Attribute name.
63
     * @param mixed $value Attribute value.
64
     * @return ServerRequestInterface
65
     * @throws RuntimeException When attribute is present.
66
     */
67
    protected function addAttribute(ServerRequestInterface $request, string $name, $value): ServerRequestInterface
68
    {
69
        if ($request->getAttribute($name)) {
70
            throw new RuntimeException(sprintf(
71
                'Request attribute `%s` is already in use.',
72
                $name
73
            ));
74
        }
75
76
        return $request->withAttribute($name, $value);
77
    }
78
79
    /**
80
     * {@inheritDoc}
81
     *
82
     * @throws RuntimeException When request attribute exists.
83
     */
84
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
85
    {
86
        $request = $this->addAttribute(
87
            $request,
88
            $this->sessionAttribute,
89
            $this->session
90
        );
91
92
        return $handler->handle($request);
93
    }
94
}
95