Completed
Push — master ( a62b20...c3c961 )
by Akihito
02:14
created

Snidel_SharedMemory::open()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 9.2
cc 4
eloc 6
nc 8
nop 1
1
<?php
2
class Snidel_SharedMemory
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
3
{
4
    /** @int **/
5
    private $pid;
6
7
    /** @int **/
8
    private $key;
9
10
    /** @int **/
11
    private $segmentId;
12
13
    /**
14
     * @param   int     $pid
15
     */
16
    public function __construct($pid)
17
    {
18
        $this->pid = $pid;
19
        $this->key = $this->generateKey($pid);
20
    }
21
22
    /**
23
     * create or open shared memory block
24
     *
25
     * @param   int     $length
26
     * @throws  RuntimeException
27
     */
28
    public function open($length = 0)
29
    {
30
        $flags  = ($length === 0) ? 'a' : 'n';
31
        $mode   = ($length === 0) ? 0 : 0666;
32
        $this->segmentId = shmop_open($this->key, $flags, $mode, $length);
33
        if ($this->segmentId === false) {
34
            throw new RuntimeException('could not open shared memory');
35
        }
36
    }
37
38
    /**
39
     * write data into shared memory block
40
     *
41
     * @param   string  $data
42
     * @throws  RuntimeException
43
     */
44
    public function write($data)
45
    {
46
        $writtenSize = shmop_write($this->segmentId, $data, 0);
47
        if ($writtenSize === false) {
48
            $this->delete();
49
            $this->close();
50
            throw new RuntimeException('could not write the data to shared memory');
51
        }
52
    }
53
54
    /**
55
     * read data from shared memory block
56
     *
57
     * @return string
58
     * @throws  RuntimeException
59
     */
60
    public function read()
61
    {
62
        $data = shmop_read($this->segmentId, 0, shmop_size($this->segmentId));
63
        if ($data === false) {
64
            throw new RuntimeException('could not read the data to shared memory');
65
        }
66
67
        return $data;
68
    }
69
70
    /**
71
     * delete shared memory block
72
     *
73
     * @throws  RuntimeException
74
     */
75
    public function delete()
76
    {
77
        if (!shmop_delete($this->segmentId)) {
78
            throw new RuntimeException('could not delete the data to shared memory');
79
        }
80
    }
81
82
    /**
83
     * cloase shared memory block
84
     *
85
     * @param   bool    $removeTmpFile
86
     */
87
    public function close($removeTmpFile = false)
88
    {
89
        shmop_close($this->segmentId);
90
        if ($removeTmpFile) {
91
            unlink('/tmp/' . sha1($this->pid));
92
        }
93
    }
94
95
    /**
96
     * generate IPC key
97
     *
98
     * @return  int
99
     */
100 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...
101
    {
102
        $pathname = '/tmp/' . sha1($pid);
103
        if (!file_exists($pathname)) {
104
            touch($pathname);
105
        }
106
107
        return ftok($pathname, 'S');
108
    }
109
}
110