FlintRedisCacheFactory   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 59
ccs 16
cts 20
cp 0.8
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 18 4
A retrieveOldInstance() 0 4 2
A newCacheInstance() 0 10 2
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
    /**
12
     * @var number Either FlintRedisCacheFactory::STRATEGY_REDIS or FlintRedisCacheFactory:: STRATEGY_FLINTSTONE
13
     */
14
    public static $strategy = self::STRATEGY_REDIS;
15
16
    /**
17
     * @var array Options you want to pass to predis/flintstone
18
     */
19
    public static $options = [];
20
21
    /**
22
     * Retrieves a new or previously created cache-instance for the provided
23
     * parameters.
24
     *
25
     * @param  string  $realm    Your collections name
26
     * @return FlintRedisCache
27
     */
28 6
    public static function create($realm, $strategy = false, $options = false)
29
    {
30 6
        $instance = self::retrieveOldInstance($realm);
31
32 6
        if ($strategy) {
33
            self::$strategy = $strategy;
0 ignored issues
show
Documentation Bug introduced by
It seems like $strategy of type boolean is incompatible with the declared type integer|double of property $strategy.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
34
        }
35
36 6
        if ($options) {
37
            self::$options = $options;
0 ignored issues
show
Documentation Bug introduced by
It seems like $options of type boolean is incompatible with the declared type array of property $options.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
38
        }
39
40 6
        if (!$instance) {
41 6
            $instance = self::newCacheInstance($realm);
42 4
        }
43
44 6
        return $instance;
45
    }
46
47 6
    private static function retrieveOldInstance($realm)
48
    {
49 6
        return isset(self::$cachedInstances[$realm]) ? self::$cachedInstances[$realm] : false;
50
    }
51
52 6
    private static function newCacheInstance($realm)
53
    {
54 6
        if (self::$strategy === self::STRATEGY_REDIS) {
55 6
            self::$cachedInstances[$realm] = new FlintRedisCacheRedis($realm, self::$options);
56 4
        } else {
57 6
            self::$cachedInstances[$realm] = new FlintRedisCacheFlintstone($realm, self::$options);
58
        }
59
60 6
        return self::$cachedInstances[$realm];
61
    }
62
}
63