Completed
Push — master ( 024bcd...cc7886 )
by Changwan
04:26
created

HttpServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 1
    public function register(ContainerInterface $app)
21
    {
22
        $app->closure(ResponseFactory::class, function () {
23
            return response(); // singleton
24 1
        });
25
        $app->closure(Configuration::class, function (Config $config) {
26 1
            return new Configuration([
27 1
                'timeout' => $config->get('session.timeout', 3600),
28 1
                'name' => $config->get('session.name', ini_get('session.name') ?: 'WdSessId'),
29 1
                'gc_frequency' => $config->get('session.gc_frequency', 100),
30
            ]);
31 1
        });
32 1
        $app->closure(SessionHandlerInterface::class, function (Config $config, ContainerInterface $app) {
33 1
            switch ($config->get('session.type')) {
34 1
                case 'file':
35
                    return new FileHandler($config->get('session.path', 'cache/sessions'));
36 1
                case 'redis':
37
                    return new RedisHandler($app[Client::class], $config->get('session.timeout', 3600));
38
                default:
39 1
                    return new GlobalHandler();
40
            }
41 1
        });
42 1
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 1
    public function boot(ContainerInterface $app)
48
    {
49 1
    }
50
}
51