Test Failed
Push — master ( be437e...866f6c )
by Julien
07:25
created

ServiceProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 73.32%

Importance

Changes 0
Metric Value
eloc 32
c 0
b 0
f 0
dl 0
loc 57
ccs 22
cts 30
cp 0.7332
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A register() 0 53 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 112
    public function register(DiInterface $di): void
29
    {
30 112
        $di->setShared($this->getName(), function () use ($di) {
31
            
32 3
            $config = $di->get('config');
33 3
            assert($config instanceof ConfigInterface);
34
            
35 3
            $sessionConfig = $config->pathToArray('session');
36
            
37 3
            $driverName = $sessionConfig['driver'] ?? '';
38
            
39 3
            $defaultOptions = $sessionConfig['default'] ?? [];
40 3
            $driverOptions = $sessionConfig['drivers'][$driverName] ?? [];
41 3
            $options = array_merge($defaultOptions, $driverOptions);
42
            
43
            // Create the new session manager
44 3
            $session = new Manager();
45
            
46
            // this is for unit testing purpose and should not have to happen
47 3
            if ($session->exists()) {
48 2
                $session->destroy();
49
            }
50
            
51
            // ini_set
52 3
            $sessionIniConfig = $sessionConfig['ini'] ?? [];
53 3
            foreach ($sessionIniConfig as $sessionIniKey => $sessionIniValue) {
54 3
                ini_set($sessionIniKey, $sessionIniValue);
55
            }
56
            
57
            // Set the storage adapter
58 3
            $adapter = $options['adapter'];
59 3
            if (in_array($adapter, [Noop::class, Stream::class])) {
60 3
                $adapterInstance = new $adapter($options);
61 3
                assert($adapterInstance instanceof \SessionHandlerInterface);
62 3
                $session->setAdapter($adapterInstance);
63
            }
64
            else {
65
                $serializerFactory = new SerializerFactory();
66
                $adapterFactory = new AdapterFactory($serializerFactory);
0 ignored issues
show
Unused Code introduced by
The assignment to $adapterFactory is dead and can be removed.
Loading history...
67
                $adapterInstance = new $adapter($options);
68
                assert($adapterInstance instanceof \SessionHandlerInterface);
69
                $session->setAdapter($adapterInstance);
70
                
71
                // ini_set save_handler and save_path for redis
72
                if ($adapter instanceof Redis) {
73
                    ini_set('session.save_handler', 'redis');
74
                    ini_set('session.save_path', $options['host'] . ':' . $options['port'] . '?' . http_build_query($options));
75
                }
76
            }
77
            
78
            // Start and return the session
79 3
            $session->start();
80 3
            return $session;
81 112
        });
82
    }
83
}
84