Completed
Push — develop ( 8b2ab3...f876c4 )
by
unknown
25:57 queued 17:57
created

CacheAdapterFactory::getDefaultCache()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.125

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 12
ccs 3
cts 6
cp 0.5
rs 9.4286
cc 3
eloc 6
nc 3
nop 0
crap 4.125
1
<?php
2
/**
3
 * @author @ct-jensschulze <[email protected]>
4
 * @created 19.01.15, 17:17
5
 */
6
7
namespace Commercetools\Core\Cache;
8
9
use Doctrine\Common\Cache\Cache;
10
use Commercetools\Core\Error\Message;
11
use Commercetools\Core\Error\InvalidArgumentException;
12
13
/**
14
 * @package Commercetools\Core\Cache
15
 */
16
class CacheAdapterFactory
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $callbacks = [];
22
23 32
    public function __construct()
24
    {
25 32
        $this->registerCallback(
26
            function ($cache) {
27 5
                if ($cache instanceof Cache) {
28 1
                    return new DoctrineCacheAdapter($cache);
29
                }
30 4
                return null;
31
            }
32 32
        )
33 32
        ->registerCallback(
34 4
            function ($cache) {
35 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...
36 1
                    return new PhpRedisCacheAdapter($cache);
37
                }
38 3
                return null;
39
            }
40 32
        );
41 32
    }
42
43
    /**
44
     * registers a callback to resolve a cache adapter interface
45
     *
46
     * @param callable $callback
47
     * @return $this
48
     */
49 32
    public function registerCallback(callable $callback)
50
    {
51 32
        $this->callbacks[] = $callback;
52
53 32
        return $this;
54
    }
55
56
    /**
57
     * returns the cache adapter interface for the application cache
58
     *
59
     * @param $cache
60
     * @return CacheAdapterInterface
61
     * @throws \InvalidArgumentException
62
     */
63 32
    public function get($cache = null)
64
    {
65 32
        if (is_null($cache)) {
66 26
            $cache = $this->getDefaultCache();
67 26
        }
68
69 32
        if ($cache instanceof CacheAdapterInterface) {
70 27
            return $cache;
71
        }
72
73 5
        foreach ($this->callbacks as $callBack) {
74 5
            $result = call_user_func($callBack, $cache);
75 5
            if ($result instanceof CacheAdapterInterface) {
76 3
                return $result;
77
            }
78 4
        }
79
80 2
        throw new InvalidArgumentException(Message::INVALID_CACHE_ADAPTER);
81
    }
82
83
    /**
84
     * creates a default cache adapter if no cache has been provided
85
     *
86
     * @return CacheAdapterInterface|null
87
     */
88 26
    protected function getDefaultCache()
89
    {
90 26
        if (extension_loaded('apcu')) {
91 26
            return new ApcuCacheAdapter();
92
        }
93
94
        if (extension_loaded('apc')) {
95
            return new ApcCacheAdapter();
96
        }
97
98
        return null;
99
    }
100
}
101