AdminAuth::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Admin\Middleware;
6
7
use Psr\Http\Message\ResponseInterface as Response;
8
use Psr\Http\Message\ServerRequestInterface as Request;
9
use Zend\Expressive\Router\RouterInterface as Router;
10
use Zend\Session\SessionManager;
11
12
/**
13
 * Class AdminAuth.
14
 */
15
final class AdminAuth
16
{
17
    /**
18
     * @var Router
19
     */
20
    private $router;
21
22
    /**
23
     * @var SessionManager
24
     */
25
    private $session;
26
27
    /**
28
     * AdminAuth constructor.
29
     *
30
     * @param Router         $router  router
31
     * @param SessionManager $session session manager
32
     */
33
    public function __construct(Router $router, SessionManager $session)
34
    {
35
        $this->router = $router;
36
        $this->session = $session;
37
    }
38
39
    /**
40
     * Called when middleware is invoked.
41
     *
42
     * @param Request       $request  request
43
     * @param Response      $response response
44
     * @param callable|null $next     next callable in line
45
     *
46
     * @return mixed
47
     */
48
    public function __invoke(Request $request, Response $response, callable $next = null)
49
    {
50
        /**
51
         * Check if user is logged in.
52
         */
53
        $user = $this->session->getStorage()->user;
0 ignored issues
show
Bug introduced by
Accessing user on the interface Zend\Session\Storage\StorageInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
54
        if (!$user) {
55
            return $response->withStatus(302)->withHeader(
56
                'Location',
57
                $this->router->generateUri('auth', ['action' => 'login'])
58
            );
59
        }
60
61
        /*
62
         * If everything is OK, continue execution middleware
63
         */
64
        return $next($request, $response);
65
    }
66
}
67