Passed
Push — master ( f0c8d3...7cbfe3 )
by Krisztián
05:50 queued 02:45
created

CacheServer::close()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpCache\CacheServer;
4
5
use PhpCache\IO\CacheIOHandler;
6
use PhpCache\Storage\Bucket;
7
use PhpCache\Storage\Maintainer;
8
9
/**
10
 * Description of CacheServer.
11
 *
12
 * @author dude920228
13
 */
14
class CacheServer implements CacheServerInterface
15
{
16
    private $socket;
17
    private $running;
18
19
    /**
20
     * @var CacheIOHandler
21
     */
22
    private $ioHandler;
23
24
    /**
25
     * @var Bucket
26
     */
27
    private $bucket;
28
29
    /**
30
     * @var ActionHandler
31
     */
32
    private $actionHandler;
33
34
    /**
35
     * @var Maintainer
36
     */
37
    private $maintainer;
38
39
    private $cacheEventListener;
40
41
    private $clients;
42
43
    public function __construct(
44
        $ioHandler, $bucket, $actionHandler, $maintainer, $cacheEventListener = false
45
    ) {
46
        $this->running = true;
47
        $this->ioHandler = $ioHandler;
48
        $this->bucket = $bucket;
49
        $this->actionHandler = $actionHandler;
50
        $this->maintainer = $maintainer;
51
        $this->clients = [];
52
        $this->cacheEventListener = $cacheEventListener;
53
    }
54
55
    public function run()
56
    {
57
        $this->socket = $this->ioHandler->createServerSocket();
58
        while ($this->running) {
59
            $this->maintainer->checkBackup(time(), $this->bucket);
60
            $this->maintainer->maintainBucket($this->bucket);
61
            if (($connection = @socket_accept($this->socket))) {
62
                $clientId = uniqid();
63
                socket_set_nonblock($connection);
64
                $this->clients[$clientId] = $connection;
65
                $read = $this->clients;
66
                $write = [];
67
                $except = [];
68
                socket_select($read, $write, $except, 10);
69
                $dataString = $this->ioHandler->readFromSocket($connection);
70
                $data = unserialize($dataString);
71
                ($this->actionHandler)(
72
                    $this,
73
                    $data,
74
                    $connection
75
                );
76
                $this->ioHandler->closeSocket($connection);
77
                unset($this->clients[$clientId]);
78
            }
79
        }
80
    }
81
82
    public function getBucket()
83
    {
84
        return $this->bucket;
85
    }
86
87
    public function getIOHandler()
88
    {
89
        return $this->ioHandler;
90
    }
91
92
    public function getCacheEventListener()
93
    {
94
        return $this->cacheEventListener;
95
    }
96
97
    public function getMaintainer()
98
    {
99
        return $this->maintainer;
100
    }
101
102
    public function getSocket()
103
    {
104
        return $this->socket;
105
    }
106
107
    public function getEventListener()
108
    {
109
        return $this->cacheEventListener;
110
    }
111
112
    public function close()
113
    {
114
        $this->maintainer->backup($this->bucket);
115
        $this->running = false;
116
        $this->ioHandler->closeSocket($this->socket);
117
        $this->ioHandler->removeSocket();
118
    }
119
}
120