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

PhpRedis::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3.072

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 27
ccs 12
cts 15
cp 0.8
rs 9.7666
c 0
b 0
f 0
cc 3
nc 4
nop 2
crap 3.072
1
<?php namespace Comodojo\Cache\Providers;
2
3
use \Comodojo\Cache\Drivers\PhpRedis as PhpRedisDriver;
4
use \Comodojo\Cache\Item;
5
use \Comodojo\Cache\Components\EnhancedCacheItemPoolStats;
6
use \Comodojo\Foundation\Validation\DataValidation;
7
use \Comodojo\Foundation\Validation\DataFilter;
8
use \Psr\Log\LoggerInterface;
9
use \Comodojo\Exception\CacheException;
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 28
    public function __construct(array $properties = [], LoggerInterface $logger = null) {
38
39 28
        parent::__construct($properties, $logger);
40
41 28
        $properties = $this->getProperties();
42
43 28
        if ( empty($properties->server) ) {
44
            throw new InvalidCacheArgumentException("Invalid or unspecified memcached server");
0 ignored issues
show
Bug introduced by
The type Comodojo\Cache\Providers...dCacheArgumentException 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...
45
        }
46
47 28
        $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 28
        $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 $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...
Bug introduced by
Comodojo\Cache\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 $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...
49
50
        try {
51
52 28
            $this->driver = new PhpRedisDriver([
53 28
                'server' => $properties->server,
54 28
                'port' => $port,
55 28
                'timeout' => $timeout,
56 28
                'password' => $properties->password
57
            ]);
58
59 28
            $this->test();
60
61
        } catch (Exception $e) {
62
63
            throw new CacheException($e->getMessage());
64
65
        }
66
67 28
    }
68
69
    public function getInstance() {
70
        return $this->driver->getInstance();
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 1
    public function getStats() {
77
78 1
        $info = $this->driver->stats();
79
80 1
        return new EnhancedCacheItemPoolStats(
81 1
            $this->getId(),
82 1
            $this->driver->getName(),
83 1
            $this->getState(),
84 1
            $info['objects'],
85 1
            $info['stats']
86
        );
87
88
    }
89
90
}
91