AdapterFactory   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 23
eloc 53
c 3
b 0
f 0
dl 0
loc 105
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C getPrefixedAdapter() 0 43 12
B createHandler() 0 49 11
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Biurad opensource projects.
7
 *
8
 * PHP version 7.1 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Biurad\Cache;
19
20
use Biurad\Cache\Exceptions\CacheException;
21
use Doctrine\Common\Cache as DoctrineCache;
22
use Doctrine\Common\Cache\Psr6\CacheAdapter;
23
use Psr\Cache\CacheItemPoolInterface;
24
use Symfony\Component\Cache\DoctrineProvider as SymfonyDoctrineProvider;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Cache\DoctrineProvider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
26
/**
27
 * Create a doctrine cache wrapped with PSR-6.
28
 *
29
 * @deprecated Deprecated without replacement since doctrine/cache version 1.11.
30
 *
31
 * @codeCoverageIgnore
32
 *
33
 * @author Divine Niiquaye Ibok <[email protected]>
34
 */
35
class AdapterFactory
36
{
37
    /**
38
     * @param object|string $connection Connection or DSN
39
     */
40
    public static function createHandler($connection): CacheItemPoolInterface
41
    {
42
        static $adapter;
43
44
        switch (true) {
45
            case $connection instanceof DoctrineCache\Cache || $connection instanceof SymfonyDoctrineProvider:
46
                return CacheAdapter::wrap($connection);
0 ignored issues
show
Bug introduced by
It seems like $connection can also be of type string; however, parameter $cache of Doctrine\Common\Cache\Psr6\CacheAdapter::wrap() does only seem to accept Doctrine\Common\Cache\Cache, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
                return CacheAdapter::wrap(/** @scrutinizer ignore-type */ $connection);
Loading history...
47
48
            case \is_string($connection):
49
                if (\in_array($connection, ['array', 'apcu', 'win-cache', 'zend-data'], true)) {
50
                    $adapter = \sprintf('Doctrine\Common\Cache\%sCache', \ucwords($connection, '-'));
51
52
                    return CacheAdapter::wrap(new $adapter());
53
                }
54
55
                $connection = self::getPrefixedAdapter($connection, $adapter);
56
57
                if ($connection instanceof DoctrineCache\Cache) {
58
                    return CacheAdapter::wrap($connection);
59
                }
60
61
                // no break
62
63
            case $connection instanceof \Redis:
64
                $adapter = new DoctrineCache\RedisCache();
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\RedisCache has been deprecated: Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

64
                $adapter = /** @scrutinizer ignore-deprecated */ new DoctrineCache\RedisCache();
Loading history...
65
66
                $adapter->setRedis($connection);
67
68
                break;
69
70
            case $connection instanceof \Memcache:
71
                $adapter = new DoctrineCache\MemcacheCache();
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\MemcacheCache has been deprecated: Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

71
                $adapter = /** @scrutinizer ignore-deprecated */ new DoctrineCache\MemcacheCache();
Loading history...
72
                $adapter->setMemcache($connection);
73
74
                break;
75
76
            case $connection instanceof \Memcached:
77
                $adapter = new DoctrineCache\MemcachedCache();
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\MemcachedCache has been deprecated: Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

77
                $adapter = /** @scrutinizer ignore-deprecated */ new DoctrineCache\MemcachedCache();
Loading history...
78
                $adapter->setMemcached($connection);
79
80
                break;
81
        }
82
83
        if ($adapter instanceof DoctrineCache\Cache) {
84
            return CacheAdapter::wrap($adapter);
85
        }
86
87
        throw new CacheException(
88
            \sprintf('Unsupported Cache Adapter: %s.', \is_object($connection) ? \get_class($connection) : $connection)
89
        );
90
    }
91
92
    /**
93
     * @param mixed $adapter
94
     *
95
     * @return \Redis|\Memcache|\Memcached|DoctrineCache\Cache|null
96
     */
97
    private static function getPrefixedAdapter(string $connection, $adapter)
98
    {
99
        $connection = \parse_url($connection) ?: \explode('://', $connection, 2) ?: [];
100
101
        // Extract parsed connection string.
102
        list($scheme, $host, $port) = [
103
            $connection['scheme'] ?? $connection[0] ?? null,
104
            $connection['host'] ?? $connection[1] ?? null,
105
            $connection['port'] ?? null,
106
        ];
107
108
        if (isset($scheme, $host)) {
109
            switch ($connectionServer = \ucfirst($scheme)) {
110
                case 'Redis':
111
                    ($adapter = new \Redis())->connect($host, $port ?? 6379);
112
113
                    break;
114
115
                case 'sqlite':
116
                    $adapter = new DoctrineCache\SQLite3Cache(new \SQLite3($host), 'doctrine_cache');
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\SQLite3Cache has been deprecated: Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

116
                    $adapter = /** @scrutinizer ignore-deprecated */ new DoctrineCache\SQLite3Cache(new \SQLite3($host), 'doctrine_cache');
Loading history...
117
118
                    break;
119
120
                case 'Memcache':
121
                case 'Memcache':
122
                    /** @var \Memcache|\Memcached $adapter */
123
                    ($adapter = new $connectionServer())->addServer($host, $port ?? 11211);
124
125
                    break;
126
127
                case 'Serialize':
128
                case 'Serialise':
129
                    $adapter = new DoctrineCache\PhpFileCache($host);
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\PhpFileCache has been deprecated: Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

129
                    $adapter = /** @scrutinizer ignore-deprecated */ new DoctrineCache\PhpFileCache($host);
Loading history...
130
131
                    break;
132
133
                case 'Filesystem':
134
                case 'File':
135
                    $adapter = new DoctrineCache\FilesystemCache($host);
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\FilesystemCache has been deprecated: Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

135
                    $adapter = /** @scrutinizer ignore-deprecated */ new DoctrineCache\FilesystemCache($host);
Loading history...
136
            }
137
        }
138
139
        return $adapter;
140
    }
141
}
142