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

Snidel_Data::write()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 20
rs 9.4285
cc 3
eloc 13
nc 3
nop 1
1
<?php
2
class Snidel_Data
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 Snidel_SharedMemory */
8
    private $shm;
9
10
    /**
11
     * @param   int     $pid
12
     */
13
    public function __construct($pid)
14
    {
15
        $this->pid = $pid;
16
        $this->shm = new Snidel_SharedMemory($pid);
17
    }
18
19
    /**
20
     * write data
21
     *
22
     * @param   mixed     $data
23
     * @return  void
24
     * @throws  Snidel_Exception_SharedMemoryControlException
25
     */
26
    public function write($data)
27
    {
28
        $serializedData = serialize(array(
29
            'pid'   => $this->pid,
30
            'data'  => $data,
31
        ));
32
        try {
33
            $this->shm->open(strlen($serializedData));
34
        } catch (Snidel_Exception_SharedMemoryControlException $e) {
35
            throw $e;
36
        }
37
38
        try {
39
            $this->shm->write($serializedData);
40
        } catch (Snidel_Exception_SharedMemoryControlException $e) {
41
            throw $e;
42
        }
43
44
        $this->shm->close();
45
    }
46
47
    /**
48
     * read data and delete shared memory
49
     *
50
     * @return  mixed
51
     * @throws  Snidel_Exception_SharedMemoryControlException
52
     */
53
    public function readAndDelete()
54
    {
55
        try {
56
            $data = $this->read();
57
            $this->delete();
58
        } catch (Snidel_Exception_SharedMemoryControlException $e) {
59
            throw $e;
60
        }
61
62
        return $data;
63
    }
64
65
    /**
66
     * read data
67
     *
68
     * @return  array
69
     * @throws  Snidel_Exception_SharedMemoryControlException
70
     */
71
    public function read()
72
    {
73
        try {
74
            $this->shm->open();
75
            $data = $this->shm->read();
76
        } catch (Snidel_Exception_SharedMemoryControlException $e) {
77
            throw $e;
78
        }
79
80
        $this->shm->close();
81
        $unserialized = unserialize($data);
82
83
        return $unserialized['data'];
84
    }
85
86
    /**
87
     * delete shared memory
88
     *
89
     * @return  void
90
     * @throws  Snidel_Exception_SharedMemoryControlException
91
     */
92
    public function delete()
93
    {
94
        try {
95
            $this->shm->open();
96
        } catch (Snidel_Exception_SharedMemoryControlException $e) {
97
            throw $e;
98
        }
99
100
        try {
101
            $this->shm->delete();
102
        } catch (Snidel_Exception_SharedMemoryControlException $e) {
103
            throw $e;
104
        }
105
106
        $this->shm->close($removeTmpFile = true);
107
    }
108
109
    /**
110
     * delete shared memory if exists
111
     *
112
     * @return  void
113
     * @throws  Snidel_Exception_SharedMemoryControlException
114
     */
115
    public function deleteIfExists()
116
    {
117
        if (!$this->shm->exists()) {
118
            return;
119
        }
120
121
        try {
122
            $this->delete();
123
        } catch (Snidel_Exception_SharedMemoryControlException $e) {
124
            throw $e;
125
        }
126
    }
127
}
128