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

CacheAdapterFactory::get()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
ccs 12
cts 12
cp 1
rs 6.7272
cc 7
eloc 12
nc 10
nop 1
crap 7
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