StoreFactory::create()   D
last analyzed

Complexity

Conditions 18
Paths 18

Size

Total Lines 36
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 18.0147

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 28
c 1
b 0
f 1
dl 0
loc 36
ccs 27
cts 28
cp 0.9643
rs 4.8666
cc 18
nc 18
nop 1
crap 18.0147

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Zenstruck\Governator;
4
5
use Psr\Cache\CacheItemPoolInterface;
6
use Psr\SimpleCache\CacheInterface;
7
use Symfony\Component\Cache\Adapter\AbstractAdapter;
8
use Symfony\Component\Cache\Traits\RedisClusterProxy;
9
use Symfony\Component\Cache\Traits\RedisProxy;
10
use Zenstruck\Governator\Store\MemoryStore;
11
use Zenstruck\Governator\Store\Psr16CacheStore;
12
use Zenstruck\Governator\Store\Psr6CacheStore;
13
use Zenstruck\Governator\Store\RedisStore;
14
use Zenstruck\Governator\Store\UnlimitedStore;
15
16
/**
17
 * @see https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Lock/Store/StoreFactory.php
18
 *
19
 * @author Kevin Bond <[email protected]>
20
 */
21
final class StoreFactory
22
{
23
    /**
24
     * Create a Throttle Store for passed connection DSN/object.
25
     *
26
     * @param string|object $connection
27
     *
28
     * @throws \InvalidArgumentException If not capable of handling passed connection
29
     */
30 22
    public static function create($connection): Store
31
    {
32 22
        if (!\is_string($connection) && !\is_object($connection)) {
33 1
            throw new \TypeError(\sprintf('Argument 1 passed to "%s()" must be a string or a connection object, "%s" given.', __METHOD__, \gettype($connection)));
34
        }
35
36
        switch (true) {
37 21
            case $connection instanceof Store:
38 1
                return $connection;
39 20
            case $connection instanceof CacheItemPoolInterface:
40 1
                return new Psr6CacheStore($connection);
41 19
            case $connection instanceof CacheInterface:
42 1
                return new Psr16CacheStore($connection);
43 18
            case $connection instanceof \Redis:
44 17
            case $connection instanceof \RedisArray:
45 16
            case $connection instanceof \RedisCluster:
46 15
            case $connection instanceof \Predis\ClientInterface:
47 14
            case $connection instanceof RedisProxy:
48 13
            case $connection instanceof RedisClusterProxy:
49 6
                return new RedisStore($connection);
0 ignored issues
show
Bug introduced by
It seems like $connection can also be of type string; however, parameter $client of Zenstruck\Governator\Sto...disStore::__construct() does only seem to accept object, 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

49
                return new RedisStore(/** @scrutinizer ignore-type */ $connection);
Loading history...
50 12
            case !\is_string($connection):
51 1
                throw new \InvalidArgumentException(\sprintf('Unsupported Connection: "%s".', \get_class($connection)));
0 ignored issues
show
Bug introduced by
It seems like $connection can also be of type string; however, parameter $object of get_class() does only seem to accept object, 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

51
                throw new \InvalidArgumentException(\sprintf('Unsupported Connection: "%s".', \get_class(/** @scrutinizer ignore-type */ $connection)));
Loading history...
52 11
            case 'memory' === $connection:
53 8
                return new MemoryStore();
54 3
            case 'unlimited' === $connection:
55 1
                return new UnlimitedStore();
56 2
            case 0 === \strpos($connection, 'redis:'):
0 ignored issues
show
Bug introduced by
It seems like $connection can also be of type object; however, parameter $haystack of strpos() does only seem to accept string, 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

56
            case 0 === \strpos(/** @scrutinizer ignore-type */ $connection, 'redis:'):
Loading history...
57 1
            case 0 === \strpos($connection, 'rediss:'):
58 1
                if (!\class_exists(AbstractAdapter::class)) {
59
                    throw new \InvalidArgumentException(\sprintf('Unsupported DSN "%s". Try running "composer require symfony/cache".', $connection));
0 ignored issues
show
Bug introduced by
It seems like $connection can also be of type object; however, parameter $args of sprintf() does only seem to accept string, 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

59
                    throw new \InvalidArgumentException(\sprintf('Unsupported DSN "%s". Try running "composer require symfony/cache".', /** @scrutinizer ignore-type */ $connection));
Loading history...
60
                }
61
62 1
                return new RedisStore(AbstractAdapter::createConnection($connection, ['lazy' => true]));
0 ignored issues
show
Bug introduced by
It seems like $connection can also be of type object; however, parameter $dsn of Symfony\Component\Cache\...ter::createConnection() does only seem to accept string, 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

62
                return new RedisStore(AbstractAdapter::createConnection(/** @scrutinizer ignore-type */ $connection, ['lazy' => true]));
Loading history...
63
        }
64
65 1
        throw new \InvalidArgumentException(\sprintf('Unsupported Connection: "%s".', $connection));
66
    }
67
}
68