Passed
Push — main ( 146938...afdae3 )
by Peter
02:51
created

Authentication::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Http\Middleware;
6
7
use AbterPhp\Admin\Config\Routes as RoutesConfig;
8
use AbterPhp\Framework\Constant\Session as SessionConstants;
9
use AbterPhp\Framework\Http\Middleware\Session;
10
use Closure;
11
use Opulence\Http\Requests\Request;
12
use Opulence\Http\Responses\RedirectResponse;
13
use Opulence\Http\Responses\Response;
14
use Opulence\Http\Responses\ResponseHeaders;
15
use Opulence\Sessions\ISession;
16
use SessionHandlerInterface;
17
18
class Authentication extends Session
19
{
20
    protected RoutesConfig $routesConfig;
21
22
    /**
23
     * Authentication constructor.
24
     *
25
     * @param ISession                $session
26
     * @param SessionHandlerInterface $sessionHandler
27
     * @param RoutesConfig            $routesConfig
28
     */
29
    public function __construct(ISession $session, SessionHandlerInterface $sessionHandler, RoutesConfig $routesConfig)
30
    {
31
        parent::__construct($session, $sessionHandler);
32
33
        $this->routesConfig = $routesConfig;
34
    }
35
36
    // $next consists of the next middleware in the pipeline
37
    public function handle(Request $request, Closure $next): Response
38
    {
39
        if (!$this->session->has(SessionConstants::USERNAME)) {
40
            return new RedirectResponse($this->routesConfig->getLoginPath(), ResponseHeaders::HTTP_TEMPORARY_REDIRECT);
41
        }
42
43
        return $next($request);
44
    }
45
}
46