Completed
Push — master ( 526d6e...2376dc )
by Akihito
03:04
created

SharedMemory::write()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
namespace Ackintosh\Snidel;
3
4
use Ackintosh\Snidel\Exception\SharedMemoryControlException;
5
6
class SharedMemory
7
{
8
    /** @var int **/
9
    private $pid;
10
11
    /** @var int **/
12
    private $key;
13
14
    /** @var int **/
15
    private $segmentId;
16
17
    /** @const string **/
18
    const TMP_FILE_PREFIX = 'snidel_shm_';
19
20
    /**
21
     * @param   int     $pid
22
     */
23
    public function __construct($pid)
24
    {
25
        $this->pid = $pid;
26
        $this->key = $this->generateKey($pid);
27
    }
28
29
    /**
30
     * create or open shared memory block
31
     *
32
     * @param   int     $length
33
     * @throws  Ackintosh\Snidel\Exception\SharedMemoryControlException
34
     */
35
    public function open($length = 0)
36
    {
37
        $flags  = ($length === 0) ? 'a' : 'n';
38
        $mode   = ($length === 0) ? 0 : 0666;
39
        $this->segmentId = @shmop_open($this->key, $flags, $mode, $length);
40
        if ($this->segmentId === false) {
41
            throw new SharedMemoryControlException('could not open shared memory');
42
        }
43
    }
44
45
    /**
46
     * write data into shared memory block
47
     *
48
     * @param   string  $data
49
     * @throws  Ackintosh\Snidel\Exception\SharedMemoryControlException
50
     */
51
    public function write($data)
52
    {
53
        $writtenSize = @shmop_write($this->segmentId, $data, 0);
54
        if ($writtenSize === false) {
55
            throw new SharedMemoryControlException('could not write the data to shared memory');
56
        }
57
    }
58
59
    /**
60
     * read data from shared memory block
61
     *
62
     * @return string
63
     * @throws  Ackintosh\Snidel\Exception\SharedMemoryControlException
64
     */
65
    public function read()
66
    {
67
        $data = @shmop_read($this->segmentId, 0, shmop_size($this->segmentId));
68
        if ($data === false) {
69
            throw new SharedMemoryControlException('could not read the data to shared memory');
70
        }
71
72
        return $data;
73
    }
74
75
    /**
76
     * delete shared memory block
77
     *
78
     * @throws  Ackintosh\Snidel\Exception\SharedMemoryControlException
79
     */
80
    public function delete()
81
    {
82
        if ($this->segmentId && !@shmop_delete($this->segmentId)) {
83
            throw new SharedMemoryControlException('could not delete the data to shared memory');
84
        }
85
    }
86
87
    /**
88
     * cloase shared memory block
89
     *
90
     * @param   bool    $removeTmpFile
91
     */
92
    public function close($removeTmpFile = false)
93
    {
94
        if ($this->segmentId) {
95
            shmop_close($this->segmentId);
96
        }
97
        if ($removeTmpFile) {
98
            unlink('/tmp/' . self::TMP_FILE_PREFIX . sha1($this->pid));
99
        }
100
    }
101
102
    public function exists()
103
    {
104
        $ret = @shmop_open($this->key, 'a', 0, 0);
105
        return $ret !== false;
106
    }
107
108
    /**
109
     * generate IPC key
110
     *
111
     * @return  int
112
     */
113 View Code Duplication
    private function generateKey($pid)
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...
114
    {
115
        $pathname = '/tmp/' . self::TMP_FILE_PREFIX . sha1($pid);
116
        if (!file_exists($pathname)) {
117
            touch($pathname);
118
        }
119
120
        return ftok($pathname, 'S');
121
    }
122
}
123