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

Memcached::__construct()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5.2331

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 33
ccs 15
cts 19
cp 0.7895
rs 9.3222
c 0
b 0
f 0
cc 5
nc 5
nop 2
crap 5.2331
1
<?php namespace Comodojo\Cache\Providers;
2
3
use \Comodojo\Cache\Drivers\Memcached as MemcachedDriver;
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 Memcached extends AbstractEnhancedProvider {
29
30
    protected $default_properties = [
31
        "server" => '127.0.0.1',
32
        "port" => 11211,
33
        "weight" => 0,
34
        "persistent_id" => null,
35
        "username" => null,
36
        "password" => null
37
    ];
38
39 28
    public function __construct(array $properties = [], LoggerInterface $logger = null) {
40
41 28
        parent::__construct($properties, $logger);
42
43 28
        $properties = $this->getProperties();
44
45 28
        if ( empty($properties->server) ) {
46
            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...
47
        }
48
49 28
        if ( $properties->persistent_id !== null && DataValidation::validateString($properties->persistent_id) === false ) {
50
            throw new InvalidCacheArgumentException("Invalid persistent id");
51
        }
52
53 28
        $port = DataFilter::filterPort($properties->port, 11211);
0 ignored issues
show
Bug introduced by
11211 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

53
        $port = DataFilter::filterPort($properties->port, /** @scrutinizer ignore-type */ 11211);
Loading history...
54 28
        $weight = DataFilter::filterInteger($properties->weight);
55
56
        try {
57
58 28
            $this->driver = new MemcachedDriver([
59 28
                'persistent_id' => $properties->persistent_id,
60 28
                'server' => $properties->server,
61 28
                'port' => $port,
62 28
                'weight' => $weight,
63 28
                "username" => $properties->username,
64 28
                "password" => $properties->password
65
            ]);
66
67 28
            $this->test();
68
69
        } catch (Exception $e) {
70
71
            throw new CacheException($e->getMessage());
72
73
        }
74
75 28
    }
76
77
    public function getInstance() {
78
        return $this->driver->getInstance();
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 1
    public function getStats() {
85
86 1
        $info = $this->driver->stats();
87
88 1
        $objects = 0;
89
90 1
        foreach ( $info as $key => $value ) {
91
92 1
            $objects = max($objects, $value['curr_items']);
93
94
        }
95
96 1
        return new EnhancedCacheItemPoolStats(
97 1
            $this->getId(),
98 1
            $this->driver->getName(),
99 1
            $this->getState(),
100 1
            $objects,
101 1
            $info
102
        );
103
104
    }
105
106
}
107