Completed
Push — master ( 037ec5...5f25ab )
by Mathieu
01:33
created

Memcached::setPort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Suricate\Cache;
3
4
use Suricate;
5
6
/**
7
 * Memcache extension for Suricate
8
 *
9
 * @package Suricate
10
 * @author  Mathieu LESNIAK <[email protected]>
11
 *
12
 * @property string $host           Memcache host (default: localhost)
13
 * @property string $port           Memcache port (default: 11211)
14
 * @property int    $defaultExpiry  Key default expiry
15
 */
16
17
class Memcached extends Suricate\Cache
18
{
19
    protected $parametersList = array(
20
                                    'host',
21
                                    'port',
22
                                    'defaultExpiry',
23
                                );
24
    private $handler;
25
26
    public function __construct()
27
    {
28
        $this->handler          = false;
29
        $this->host             = 'localhost';
30
        $this->port             = '11211';
31
    }
32
    
33
    public function getHost()
34
    {
35
        return $this->host;
36
    }
37
38
    public function setHost($host)
39
    {
40
        $this->host = $host;
41
42
        return $this;
43
    }
44
45
    public function getPort()
46
    {
47
        return $this->port;
48
    }
49
50
    public function setPort($port)
51
    {
52
        $this->port = $port;
53
        
54
        return $this;
55
    }
56
57
    public function getDefaultExpiry()
58
    {
59
        return $this->defaultExpiry;
60
    }
61
62
    public function setDefaultExpiry($expiry)
63
    {
64
        $this->defaultExpiry = $expiry;
65
66
        return $this;
67
    }
68
    
69 View Code Duplication
    private function connect()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
70
    {
71
        if ($this->handler === false) {
72
            if (class_exists('Memcached')) {
73
                try {
74
                    $this->handler = new \Memcached();
75
                    $this->handler->addServer($this->host, $this->port);
76
                } catch (\Exception $e) {
77
                    throw new \Exception('Can\'t connect to memcache server');
78
                }
79
            } else {
80
                throw new \BadMethodCallException('Can\'t find Memcached extension');
81
            }
82
        } else {
83
            return $this;
84
        }
85
    }
86
87
    /**
88
     * Put a value into memcache
89
     * @param string $variable Variable name
90
     * @param mixed $value    Value
91
     * @param int $expiry   Cache expiry
92
     *
93
     * @return bool
94
     */
95
    public function set(string $variable, $value, $expiry = null)
96
    {
97
        $this->connect();
98
99
        if ($expiry === null) {
100
            $expiry = $this->defaultExpiry;
101
        }
102
103
        return $this->handler->set($variable, $value, $expiry);
104
    }
105
106
    public function get(string $variable)
107
    {
108
        $this->connect();
109
        return $this->handler->get($variable);
110
    }
111
112
    /**
113
     * Delete a variable from memcache
114
     *
115
     * @param string $variable
116
     *
117
     * @return bool
118
     */
119
    public function delete(string $variable)
120
    {
121
        $this->connect();
122
        return $this->handler->delete($variable);
123
    }
124
}
125