CacheClient::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpCache\CacheClient;
4
5
use PhpCache\IO\CacheIOHandler;
6
7
/**
8
 * Description of newPHPClass.
9
 *
10
 * @author dude920228
11
 */
12
class CacheClient implements CacheClientInterface
13
{
14
    /**
15
     * @var CacheIOHandler
16
     */
17
    private $ioHandler;
18
19
    public function __construct(CacheIOHandler $ioHandler)
20
    {
21
        $this->ioHandler = $ioHandler;
22
    }
23
24
    public function set(string $key, $package): void
25
    {
26
        $socket = $this->ioHandler->createClientSocket();
27
        $data = ['action' => 'set', 'key' => $key, 'message' => $package];
28
        $dataString = serialize($data);
29
        $this->ioHandler->writeToSocket($socket, $dataString);
30
        $this->ioHandler->closeSocket($socket);
31
    }
32
33
    public function get(string $key)
34
    {
35
        $data = ['action' => 'get', 'key' => $key];
36
        $socket = $this->ioHandler->createClientSocket();
37
        $dataString = serialize($data);
38
        $this->ioHandler->writeToSocket($socket, $dataString);
39
        $recv = $this->ioHandler->readFromSocket($socket);
40
        $this->ioHandler->closeSocket($socket);
41
42
        return unserialize($recv);
43
    }
44
45
    public function delete(string $key): void
46
    {
47
        $data = ['action' => 'delete', 'key' => $key];
48
        $socket = $this->ioHandler->createClientSocket();
49
        $dataString = serialize($data);
50
        $this->ioHandler->writeToSocket($socket, $dataString);
51
        $this->ioHandler->closeSocket($socket);
52
    }
53
54
    public function quitServer(): void
55
    {
56
        $data = ['action' => 'quit'];
57
        $socket = $this->ioHandler->createClientSocket();
58
        $dataString = serialize($data);
59
        $this->ioHandler->writeToSocket($socket, $dataString);
60
    }
61
62
    public function getEntries(): array
63
    {
64
        $data = ['action' => 'getEntries'];
65
        $socket = $this->ioHandler->createClientSocket();
66
        $dataString = serialize($data);
67
        $this->ioHandler->writeToSocket($socket, $dataString);
68
        $entries = $this->ioHandler->readFromSocket($socket);
69
        $this->ioHandler->closeSocket($socket);
70
71
        return unserialize($entries);
72
    }
73
}
74