SessionManagerFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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