Passed
Push — master ( 5cb94e...eb0704 )
by Marco
03:34
created

PhpRedis   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 80.77%

Importance

Changes 0
Metric Value
wmc 5
eloc 29
dl 0
loc 55
ccs 21
cts 26
cp 0.8077
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 2 1
A __construct() 0 27 3
A getStats() 0 10 1
1
<?php namespace Comodojo\SimpleCache\Providers;
2
3
use \Comodojo\Cache\Drivers\PhpRedis as PhpRedisDriver;
4
use \Comodojo\Foundation\Validation\DataValidation;
5
use \Comodojo\Foundation\Validation\DataFilter;
6
use \Comodojo\Cache\Components\EnhancedCacheItemPoolStats;
7
use \Psr\Log\LoggerInterface;
8
use \Comodojo\Exception\SimpleCacheException;
9
use \Comodojo\Exception\InvalidSimpleCacheArgumentException;
10
use \Exception;
11
12
/**
13
 * @package     Comodojo Cache
14
 * @author      Marco Giovinazzi <[email protected]>
15
 * @license     MIT
16
 *
17
 * LICENSE:
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
 * THE SOFTWARE.
26
 */
27
28
class PhpRedis extends AbstractEnhancedProvider {
29
30
    protected $default_properties = [
31
        "server" => '127.0.0.1',
32
        "port" => 6379,
33
        "timeout" => 0,
34
        "password" => null
35
    ];
36
37 19
    public function __construct(array $properties = [], LoggerInterface $logger = null) {
38
39 19
        parent::__construct($properties, $logger);
40
41 19
        $properties = $this->getProperties();
42
43 19
        if ( empty($properties->server) ) {
44
            throw new InvalidSimpleCacheArgumentException("Invalid or unspecified memcached server");
45
        }
46
47 19
        $port = DataFilter::filterPort($properties->port, 6379);
0 ignored issues
show
Bug introduced by
6379 of type integer is incompatible with the type array expected by parameter $default of Comodojo\Foundation\Vali...ataFilter::filterPort(). ( Ignorable by Annotation )

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

47
        $port = DataFilter::filterPort($properties->port, /** @scrutinizer ignore-type */ 6379);
Loading history...
48 19
        $timeout = DataFilter::filterInteger($properties->timeout, 0, PHP_INT_MAX, 0);
0 ignored issues
show
Bug introduced by
0 of type integer is incompatible with the type array expected by parameter $min of Comodojo\Foundation\Vali...Filter::filterInteger(). ( Ignorable by Annotation )

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

48
        $timeout = DataFilter::filterInteger($properties->timeout, /** @scrutinizer ignore-type */ 0, PHP_INT_MAX, 0);
Loading history...
Bug introduced by
Comodojo\SimpleCache\Providers\PHP_INT_MAX of type integer is incompatible with the type array expected by parameter $max of Comodojo\Foundation\Vali...Filter::filterInteger(). ( Ignorable by Annotation )

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

48
        $timeout = DataFilter::filterInteger($properties->timeout, 0, /** @scrutinizer ignore-type */ PHP_INT_MAX, 0);
Loading history...
Bug introduced by
0 of type integer is incompatible with the type array expected by parameter $default of Comodojo\Foundation\Vali...Filter::filterInteger(). ( Ignorable by Annotation )

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

48
        $timeout = DataFilter::filterInteger($properties->timeout, 0, PHP_INT_MAX, /** @scrutinizer ignore-type */ 0);
Loading history...
49
50
        try {
51
52 19
            $this->driver = new PhpRedisDriver([
53 19
                'server' => $properties->server,
54 19
                'port' => $port,
55 19
                'timeout' => $timeout,
56 19
                'password' => $properties->password
57
            ]);
58
59 19
            $this->test();
60
61
        } catch (Exception $e) {
62
63
            throw new SimpleCacheException($e->getMessage());
64
65
        }
66
67 19
    }
68
69
    public function getInstance() {
70
        return $this->driver->getInstance();
71
    }
72
73 1
    public function getStats() {
74
75 1
        $info = $this->driver->stats();
76
77 1
        return new EnhancedCacheItemPoolStats(
78 1
            $this->getId(),
79 1
            $this->driver->getName(),
80 1
            $this->getState(),
81 1
            $info['objects'],
82 1
            $info['stats']
83
        );
84
85
    }
86
87
}
88