Completed
Push — master ( 1d2d63...5f432b )
by Jens
09:03
created

CacheAdapterFactory   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 82.05%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 16
c 2
b 0
f 1
lcom 1
cbo 8
dl 0
loc 101
ccs 32
cts 39
cp 0.8205
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 4
C get() 0 23 7
A getDefaultCache() 0 18 4
A registerCallback() 0 6 1
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 * @created 19.01.15, 17:17
5
 */
6
7
namespace Commercetools\Core\Cache;
8
9
use Cache\Adapter\Filesystem\FilesystemCachePool;
10
use Doctrine\Common\Cache\Cache;
11
use Commercetools\Core\Error\Message;
12
use Commercetools\Core\Error\InvalidArgumentException;
13
use League\Flysystem\Adapter\Local;
14
use League\Flysystem\Filesystem;
15
use Psr\Cache\CacheItemPoolInterface;
16
17
/**
18
 * @package Commercetools\Core\Cache
19
 */
20
class CacheAdapterFactory
21
{
22
    /**
23
     * @var string
24
     */
25
    private $cacheDir;
26
27
    /**
28
     * @var array
29
     */
30
    protected $callbacks = [];
31
32 48
    public function __construct($cacheDir = null)
33
    {
34 48
        $this->cacheDir = !is_null($cacheDir) ? $cacheDir : realpath(__DIR__ . '/../..');
35 48
        $this->registerCallback(
36
            function ($cache) {
37 5
                if ($cache instanceof Cache) {
38 1
                    return new DoctrineCacheAdapter($cache);
0 ignored issues
show
Deprecated Code introduced by
The class Commercetools\Core\Cache\DoctrineCacheAdapter has been deprecated with message: use a PSR-6 cache adapter instead. Will be removed with v2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
39
                }
40 4
                return null;
41 48
            }
42
        )
43 48
        ->registerCallback(
44 48
            function ($cache) {
45 4
                if ($cache instanceof \Redis) {
0 ignored issues
show
Bug introduced by
The class Redis does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
46 1
                    return new PhpRedisCacheAdapter($cache);
0 ignored issues
show
Deprecated Code introduced by
The class Commercetools\Core\Cache\PhpRedisCacheAdapter has been deprecated with message: use a PSR-6 cache adapter instead. Will be removed with v2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
47
                }
48 3
                return null;
49 48
            }
50
        );
51 48
    }
52
53
    /**
54
     * registers a callback to resolve a cache adapter interface
55
     *
56
     * @param callable $callback
57
     * @return $this
58
     */
59 48
    public function registerCallback(callable $callback)
60
    {
61 48
        $this->callbacks[] = $callback;
62
63 48
        return $this;
64
    }
65
66
    /**
67
     * returns the cache adapter interface for the application cache
68
     *
69
     * @param $cache
70
     * @return CacheAdapterInterface|CacheItemPoolInterface
71
     * @throws \InvalidArgumentException
72
     */
73 48
    public function get($cache = null)
74
    {
75 48
        if (is_null($cache)) {
76 34
            $cache = $this->getDefaultCache();
77
        }
78
79 48
        if ($cache instanceof CacheAdapterInterface) {
80 40
            return $cache;
81
        }
82
83 9
        if ($cache instanceof CacheItemPoolInterface) {
84 4
            return $cache;
85
        }
86
87 5
        foreach ($this->callbacks as $callBack) {
88 5
            $result = call_user_func($callBack, $cache);
89 5
            if ($result instanceof CacheAdapterInterface || $result instanceof CacheItemPoolInterface) {
90 5
                return $result;
91
            }
92
        }
93
94 2
        throw new InvalidArgumentException(Message::INVALID_CACHE_ADAPTER);
95
    }
96
97
    /**
98
     * creates a default cache adapter if no cache has been provided
99
     *
100
     * @return CacheAdapterInterface|null
101
     */
102 34
    protected function getDefaultCache()
103
    {
104 34
        if (extension_loaded('apcu')) {
105 34
            return new ApcuCacheAdapter();
0 ignored issues
show
Deprecated Code introduced by
The class Commercetools\Core\Cache\ApcuCacheAdapter has been deprecated with message: use a PSR-6 cache adapter instead. Will be removed with v2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
106
        }
107
108
        if (extension_loaded('apc')) {
109
            return new ApcCacheAdapter();
0 ignored issues
show
Deprecated Code introduced by
The class Commercetools\Core\Cache\ApcCacheAdapter has been deprecated with message: use a PSR-6 cache adapter instead. Will be removed with v2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
110
        }
111
112
        if (class_exists('\Cache\Adapter\Filesystem\FilesystemCachePool')) {
113
            $filesystemAdapter = new Local($this->cacheDir);
114
            $filesystem        = new Filesystem($filesystemAdapter);
115
            return new FilesystemCachePool($filesystem);
116
        }
117
118
        return null;
119
    }
120
}
121