Passed
Push — 1.0.x ( 3a2c37...ccae28 )
by Julien
14:07 queued 06:59
created

ServiceProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 76.92%

Importance

Changes 0
Metric Value
eloc 28
c 0
b 0
f 0
dl 0
loc 53
ccs 20
cts 26
cp 0.7692
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A register() 0 49 5
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Provider\Session;
13
14
use Phalcon\Di\DiInterface;
15
use Phalcon\Session\Manager;
16
use Phalcon\Session\Adapter\Redis;
17
use Phalcon\Session\Adapter\Noop;
18
use Phalcon\Session\Adapter\Stream;
19
use Phalcon\Storage\AdapterFactory;
20
use Phalcon\Storage\SerializerFactory;
21
use Zemit\Config\ConfigInterface;
22
use Zemit\Provider\AbstractServiceProvider;
23
24
class ServiceProvider extends AbstractServiceProvider
25
{
26
    protected string $serviceName = 'session';
27
    
28 45
    public function register(DiInterface $di): void
29
    {
30 45
        $di->setShared($this->getName(), function () use ($di) {
31
            
32 2
            $config = $di->get('config');
33 2
            assert($config instanceof ConfigInterface);
34
            
35 2
            $sessionConfig = $config->pathToArray('session');
36
            
37 2
            $driverName = $sessionConfig['driver'] ?? '';
38
            
39 2
            $defaultOptions = $sessionConfig['default'] ?? [];
40 2
            $driverOptions = $sessionConfig['drivers'][$driverName] ?? [];
41 2
            $options = array_merge($defaultOptions, $driverOptions);
42
            
43
            // Create the new session manager
44 2
            $session = new Manager();
45
            
46
            // this is for unit testing purpose and should not have to happen
47 2
            if ($session->exists()) {
48 1
                $session->destroy();
49
            }
50
            
51
            // ini_set
52 2
            $sessionIniConfig = $sessionConfig['ini'] ?? [];
53 2
            foreach ($sessionIniConfig as $sessionIniKey => $sessionIniValue) {
54 2
                ini_set($sessionIniKey, $sessionIniValue);
55
            }
56
            
57
            // Set the storage adapter
58 2
            $adapter = $options['adapter'];
59 2
            if (in_array($adapter, [Noop::class, Stream::class])) {
60 2
                $session->setAdapter(new $adapter($options));
61
            }
62
            else {
63
                $serializerFactory = new SerializerFactory();
64
                $adapterFactory = new AdapterFactory($serializerFactory);
65
                $session->setAdapter(new $adapter($adapterFactory, $options));
66
                
67
                // ini_set save_handler and save_path for redis
68
                if ($adapter instanceof Redis) {
69
                    ini_set('session.save_handler', 'redis');
70
                    ini_set('session.save_path', $options['host'] . ':' . $options['port'] . '?' . http_build_query($options));
71
                }
72
            }
73
            
74
            // Start and return the session
75 2
            $session->start();
76 2
            return $session;
77 45
        });
78
    }
79
}
80