AuthSessionMiddleware   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 45
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B __invoke() 0 24 4
1
<?php
2
namespace Staticus\Auth;
3
4
use Staticus\Acl\Roles;
5
use Staticus\Config\ConfigInterface;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Zend\Session\ManagerInterface;
9
use Zend\Session\SessionManager;
10
use Zend\Stratigility\MiddlewareInterface;
11
12
class AuthSessionMiddleware implements MiddlewareInterface
13
{
14
    protected $config;
15
16
    /**
17
     * @var ManagerInterface|SessionManager
18
     */
19
    protected $manager;
20
21
    /**
22
     * @var UserInterface|User
23
     */
24
    protected $user;
25
26
    public function __construct(ConfigInterface $config, ManagerInterface $manager, UserInterface $user)
27
    {
28
        $this->config = $config->get('auth.session');
29
        $this->manager = $manager;
30
        $this->user = $user;
31
    }
32
    public function __invoke(
0 ignored issues
show
Coding Style introduced by
__invoke uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
33
        ServerRequestInterface $request,
34
        ResponseInterface $response,
35
        callable $next = null
36
    )
37
    {
38
        /** @link http://framework.zend.com/manual/current/en/modules/zend.authentication.intro.html */
39
        if (array_key_exists('Zend_Auth', $_SESSION)) {
40
41
            /** @var \Zend\Stdlib\ArrayObject $auth */
42
            $auth = $_SESSION['Zend_Auth'];
43
            if ($auth->offsetExists('storage')) {
44
45
                /** @var StdClass $storage */
46
                $storage = $auth->storage;
47
                if (property_exists($storage, 'user_id')) {
48
                    $this->user->login($storage->user_id, [Roles::USER]);
49
                    $this->user->setNamespace(UserInterface::NAMESPACES . DIRECTORY_SEPARATOR . $storage->user_id);
50
                }
51
            }
52
        }
53
54
        return $next($request, $response);
55
    }
56
}
57