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

CacheServiceProvider::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\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