Completed
Push — master ( 08f0e0...238323 )
by Changwan
03:09
created

CacheServiceProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 0
cts 25
cp 0
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
B register() 0 22 4
A boot() 0 3 1
1
<?php
2
namespace Wandu\DI\Providers\Doctrine;
3
4
use Doctrine\Common\Cache\ApcuCache;
5
use Doctrine\Common\Cache\Cache;
6
use Doctrine\Common\Cache\FilesystemCache;
7
use Doctrine\Common\Cache\MemcachedCache;
8
use Doctrine\Common\Cache\RedisCache;
9
use Memcached;
10
use Predis\Client;
11
use Wandu\Config\Contracts\Config;
12
use Wandu\DI\ContainerInterface;
13
use Wandu\DI\ServiceProviderInterface;
14
15
class CacheServiceProvider implements ServiceProviderInterface
16
{
17
    public function register(ContainerInterface $app)
18
    {
19
        $app->closure(Cache::class, function (Config $config, ContainerInterface $app) {
20
            switch ($config->get('cache.type')) {
21
                case 'apcu':
22
                    $cache = new ApcuCache();
23
                    break;
24
                case 'memcached':
25
                    $cache = new MemcachedCache();
26
                    $cache->setMemcached($app->get(Memcached::class));
27
                    break;
28
                case 'redis':
29
                    $cache = new RedisCache();
30
                    $cache->setRedis(Client::class);
31
                    break;
32
                default:
33
                    $cache = new FilesystemCache(sys_get_temp_dir());
34
            }
35
            $cache->setNamespace('wandu.doctrine.cache.');
36
            return $cache;
37
        });
38
    }
39
40
    public function boot(ContainerInterface $app)
41
    {
42
    }
43
}
44