Completed
Push — master ( 495169...b31514 )
by Changwan
10:11
created

HttpServiceProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 42.11%

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 8
cts 19
cp 0.4211
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 3 1
B register() 0 23 4
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
            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 1
        });
32 1
        $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 1
        });
42 1
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 1
    public function boot(ContainerInterface $app)
48
    {
49 1
    }
50
}
51