SessionManagerFactory::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 20
ccs 0
cts 16
cp 0
rs 9.4285
cc 2
eloc 15
nc 2
nop 0
crap 6
1
<?php
2
namespace Staticus\Auth\Factories;
3
4
use Staticus\Auth\SaveHandlers\Redis;
5
use Staticus\Config\ConfigInterface;
6
use Zend\Session\Config\SessionConfig;
7
use Zend\Session\SessionManager;
8
9
class SessionManagerFactory
10
{
11
    protected $config;
12
13
    public function __construct(ConfigInterface $config)
14
    {
15
        $this->config = $config->get('auth.session');
16
    }
17
18
    public function __invoke()
19
    {
20
        $sessionConfig = new SessionConfig();
21
        $sessionConfig->setOptions($this->config['options']);
22
        $sessionManager = new SessionManager($sessionConfig);
23
        if (class_exists(\Redis::class)) {
24
            $saveHandler = new Redis(
25
                $this->config['redis']['host'],
26
                $this->config['redis']['port'],
27
                $this->config['redis']['password']
28
            );
29
            $sessionManager->setSaveHandler($saveHandler);
30
            $sessionManager->start();
31
        } else {
32
            trigger_error('Redis extension is not found. '
33
                . \Staticus\Auth\AuthSessionMiddleware::class . ' will not work.', E_USER_NOTICE);
34
        }
35
36
        return $sessionManager;
37
    }
38
}
39