Completed
Push — master ( bd0b39...0b658d )
by Sven
01:52
created

FlintRedisCacheFactory::retrieveOldInstance()   C

Complexity

Conditions 10
Paths 8

Size

Total Lines 21
Code Lines 11

Duplication

Lines 9
Ratio 42.86 %

Code Coverage

Tests 13
CRAP Score 10

Importance

Changes 0
Metric Value
dl 9
loc 21
ccs 13
cts 13
cp 1
rs 6.6746
c 0
b 0
f 0
cc 10
eloc 11
nc 8
nop 3
crap 10

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
namespace Suven\FlintRedis;
3
4
abstract class FlintRedisCacheFactory
5
{
6
    const STRATEGY_REDIS = 1;
7
    const STRATEGY_FLINTSTONE = 2;
8
9
    private static $cachedInstances = [];
10
11 15
    private static function getKey($realm, $strategy)
12
    {
13 15
        return "${realm}.${strategy}";
14
    }
15
16
    private static function getFullKey($key, $options)
17
    {
18 15
        $optionsKey = md5(json_encode($options));
19 15
        return "${key}.${optionsKey}";
20
    }
21
22
    /**
23
     * Retrieves a new or previously created cache-instance for the provided
24
     * parameters.
25
     *
26
     * If you previously created an instance for a collection 'foo' with a
27
     * strategy [and options] and are later 'creating' in instance but only
28
     * provide the realm 'foo', you will retreive your previously created instance.
29
     *
30
     * That way you only have to provide the strategy and options once per $realm.
31
     *
32
     * @param  string  $realm    Your collections name
33
     * @param  number  $strategy Either FlintRedisCacheFactory::STRATEGY_REDIS
34
     *                           or FlintRedisCacheFactory:: STRATEGY_FLINTSTONE
35
     * @param  array   $options  Options you want to pass to predis/flintstone
36
     * @return FlintRedisCache
37
     */
38
    public static function create($realm, $strategy = false, $options = false)
39
    {
40 15
        $instance = self::retrieveOldInstance($strategy, $realm, $options);
41
42 15
        if (!$instance) {
43 15
            $instance = self::newCacheInstance($strategy, $realm, $options);
44 10
        }
45
46 15
        return $instance;
47
    }
48
49
    private static function retrieveOldInstance($strategy, $realm, $options)
50
    {
51 15
        $key = self::getKey($realm, $strategy);
52 15
        $fullKey = self::getFullKey($key, $options);
53
54 15
        $instance = false;
55
56 15 View Code Duplication
        if ($strategy && $options && isset(self::$cachedInstances[$fullKey])) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57 3
            $instance = self::$cachedInstances[$fullKey];
58 2
        }
59
60 15 View Code Duplication
        if ($strategy && !$options && isset(self::$cachedInstances[$key])) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61 6
            $instance = self::$cachedInstances[$key];
62 4
        }
63
64 15 View Code Duplication
        if (!$strategy && !$options && isset(self::$cachedInstances[$realm])) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65 9
            $instance = self::$cachedInstances[$realm];
66 6
        }
67
68 15
        return $instance;
69
    }
70
71
    private static function newCacheInstance($strategy, $realm, $options)
72
    {
73 15
        $strategy = $strategy ? $strategy : self::STRATEGY_REDIS;
74 15
        $options = $options ? $options : [];
75
76 15
        $key = self::getKey($realm, $strategy);
77 15
        $fullKey = self::getFullKey($key, $options);
78
79
        $instance;
0 ignored issues
show
Bug introduced by
The variable $instance seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
80
81 15
        if ($strategy === self::STRATEGY_REDIS) {
82 15
            $instance = new FlintRedisCacheRedis($realm, $options);
83 10
        } else {
84 12
            $instance = new FlintRedisCacheFlintstone($realm, $options);
85
        }
86
87 15
        self::$cachedInstances[$fullKey] = &$instance;
88 15
        self::$cachedInstances[$key] = &$instance;
89 15
        self::$cachedInstances[$realm] = &$instance;
90
91 15
        return $instance;
92
    }
93
}
94