CacheServiceProvider   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 51
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B register() 0 26 4
A addConnection() 0 8 1
A boot() 0 3 1
1
<?php
2
3
namespace Dafiti\Silex;
4
5
use Dafiti\Silex\Exception\InvalidCacheConfig as InvalidCacheConfigException;
6
use Doctrine\Common\Cache\CacheProvider;
7
use Silex\ServiceProviderInterface;
8
use Silex\Application;
9
10
class CacheServiceProvider implements ServiceProviderInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    private $prefix = 'cache';
16
17
    public function register(Application $app)
18
    {
19
        $app[$this->prefix] = $app->share(
20
            function () use ($app) {
21
22
                if (!isset($app['config']['cache'])) {
23
                    throw new InvalidCacheConfigException('Cache Config Not Defined');
24
                }
25
26
                $cacheSettings = $app['config']['cache'];
27
                $cacheClassName = sprintf('\Doctrine\Common\Cache\%sCache', $cacheSettings['adapter']);
28
29
                if (!class_exists($cacheClassName)) {
30
                    throw new InvalidCacheConfigException('Cache Adapter Not Supported!');
31
                }
32
33
                $cacheAdapter = new $cacheClassName();
34
35
                if ($cacheSettings['connectable'] === true) {
36
                    $this->addConnection($cacheAdapter, $cacheSettings);
37
                }
38
39
                return $cacheAdapter;
40
            }
41
        );
42
    }
43
44
    /**
45
     * @param CacheProvider $cacheAdapter
46
     * @param array         $cacheSettings
47
     */
48
    private function addConnection(CacheProvider $cacheAdapter, array $cacheSettings)
49
    {
50
        $proxy = new \Dafiti\Silex\Cache\Proxy();
51
        $connection = $proxy->getAdapter($cacheSettings);
52
53
        $setMethod = sprintf('set%s', $cacheSettings['adapter']);
54
        $cacheAdapter->$setMethod($connection);
55
    }
56
57
    public function boot(Application $app)
58
    {
59
    }
60
}
61