Completed
Push — master ( b31514...603a9f )
by Changwan
06:16
created

HttpServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Http;
3
4
use Predis\Client;
5
use SessionHandlerInterface;
6
use Wandu\Config\Contracts\Config;
7
use Wandu\DI\ContainerInterface;
8
use Wandu\DI\ServiceProviderInterface;
9
use Wandu\Http\Factory\ResponseFactory;
10
use Wandu\Http\Session\Configuration;
11
use Wandu\Http\Session\Handler\FileHandler;
12
use Wandu\Http\Session\Handler\GlobalHandler;
13
use Wandu\Http\Session\Handler\RedisHandler;
14
15
class HttpServiceProvider implements ServiceProviderInterface
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function register(ContainerInterface $app)
21
    {
22
        $app->closure(ResponseFactory::class, function () {
23
            return response(); // singleton
24
        });
25
        $app->closure(Configuration::class, function (Config $config) {
26
            return new Configuration([
27
                'timeout' => $config->get('session.timeout', 3600),
28
                'name' => $config->get('session.name', ini_get('session.name') ?: 'WdSessId'),
29
                'gc_frequency' => $config->get('session.gc_frequency', 100),
30
            ]);
31
        });
32
        $app->closure(SessionHandlerInterface::class, function (Config $config, ContainerInterface $app) {
33
            switch ($config->get('session.type')) {
34
                case 'file':
35
                    return new FileHandler($config->get('session.path', 'cache/sessions'));
36
                case 'redis':
37
                    return new RedisHandler($app[Client::class], $config->get('session.timeout', 3600));
38
                default:
39
                    return new GlobalHandler();
40
            }
41
        });
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function boot(ContainerInterface $app)
48
    {
49
    }
50
}
51