Completed
Push — master ( 44710a...254d31 )
by Akihito
02:02
created

Snidel_SharedMemory   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 117
Duplicated Lines 7.69 %

Coupling/Cohesion

Components 1
Dependencies 1
Metric Value
wmc 18
lcom 1
cbo 1
dl 9
loc 117
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A open() 0 9 4
A __construct() 0 5 1
A write() 0 7 2
A read() 0 9 2
A delete() 0 6 3
A close() 0 9 3
A exists() 0 5 1
A generateKey() 9 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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