Test Failed
Push — master ( f574df...435265 )
by Florian
04:27 queued 35s
created

SessionMiddleware::process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Phauthentic\Infrastructure\Http\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
     */
38
    public function __construct(
39
        SessionInterface $session
40
    ) {
41
        if (!$session->hasStarted()) {
42
            $session->start();
43
        }
44
45
        $this->session = $session;
46
    }
47
48
    /**
49
     * Sets the identity attribute
50
     *
51
     * @param string $attribute Attribute name
52
     * @return $this
53
     */
54
    public function setSessionAttribute(string $attribute): self
55
    {
56
        $this->sessionAttribute = $attribute;
57
58
        return $this;
59
    }
60
61
    /**
62
     * Adds an attribute to the request and returns a modified request.
63
     *
64
     * @param ServerRequestInterface $request Request.
65
     * @param string $name Attribute name.
66
     * @param mixed $value Attribute value.
67
     * @return ServerRequestInterface
68
     * @throws RuntimeException When attribute is present.
69
     */
70
    protected function addAttribute(
71
        ServerRequestInterface $request,
72
        string $name,
73
        $value
74
    ): ServerRequestInterface {
75
        if ($request->getAttribute($name)) {
76
            throw new RuntimeException(sprintf(
77
                'Request attribute `%s` is already in use.',
78
                $name
79
            ));
80
        }
81
82
        return $request->withAttribute($name, $value);
83
    }
84
85
    /**
86
     * {@inheritDoc}
87
     *
88
     * @throws RuntimeException When request attribute exists.
89
     */
90
    public function process(
91
        ServerRequestInterface $request,
92
        RequestHandlerInterface $handler
93
    ): ResponseInterface {
94
        $request = $this->addAttribute(
95
            $request,
96
            $this->sessionAttribute,
97
            $this->session
98
        );
99
100
        return $handler->handle($request);
101
    }
102
}
103