Passed
Push — develop ( ca4128...18e2e5 )
by Nikolay
04:24
created

SessionProvider   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 22
c 0
b 0
f 0
dl 0
loc 33
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A register() 0 22 1
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright (C) 2017-2020 Alexey Portnov and Nikolay Beketov
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with this program.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
declare(strict_types=1);
21
22
namespace MikoPBX\Common\Providers;
23
24
25
use Phalcon\Di\DiInterface;
26
use Phalcon\Di\ServiceProviderInterface;
27
use Phalcon\Session\Adapter\Redis;
28
use Phalcon\Session\Manager;
29
use Phalcon\Storage\AdapterFactory;
30
use Phalcon\Storage\SerializerFactory;
31
32
/**
33
 * Start the session the first time some component request the session service
34
 */
35
class SessionProvider implements ServiceProviderInterface
36
{
37
    public const SERVICE_NAME = 'session';
38
39
    public const CACHE_PREFIX = '_PHCM_SESS:';
40
41
    /**
42
     * Register session service provider
43
     *
44
     * @param \Phalcon\Di\DiInterface $di
45
     */
46
    public function register(DiInterface $di): void
47
    {
48
        $config = $di->getShared(ConfigProvider::SERVICE_NAME);
49
        $di->setShared(
50
            self::SERVICE_NAME,
51
            function () use ($config) {
52
                $options = [
53
                    'defaultSerializer' => 'Php',
54
                    'lifetime'          => 3600,
55
                    'host'              => $config->path('redis.host'),
56
                    'port'              => $config->path('redis.port'),
57
                    'index'             => 1,
58
                    'prefix'            => self::CACHE_PREFIX
59
                ];
60
                $session           = new Manager();
61
                $serializerFactory = new SerializerFactory();
62
                $factory           = new AdapterFactory($serializerFactory);
63
                $redis             = new Redis($factory, $options);
64
                $session
65
                    ->setAdapter($redis)
66
                    ->start();
67
                return $session;
68
            }
69
        );
70
    }
71
}