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

Data::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace Ackintosh\Snidel;
3
4
use Ackintosh\Snidel\SharedMemory;
5
use Ackintosh\Snidel\Exception\SharedMemoryControlException;
6
7
class Data
8
{
9
    /** @var int */
10
    private $pid;
11
12
    /** @var Snidel\SharedMemory */
13
    private $shm;
14
15
    /**
16
     * @param   int     $pid
17
     */
18
    public function __construct($pid)
19
    {
20
        $this->pid = $pid;
21
        $this->shm = new SharedMemory($pid);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Ackintosh\Snidel\SharedMemory($pid) of type object<Ackintosh\Snidel\SharedMemory> is incompatible with the declared type object<Ackintosh\Snidel\Snidel\SharedMemory> of property $shm.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
22
    }
23
24
    /**
25
     * write data
26
     *
27
     * @param   mixed     $data
28
     * @return  void
29
     * @throws  Snidel\Exception\SharedMemoryControlException
30
     */
31
    public function write($data)
32
    {
33
        $serializedData = serialize(array(
34
            'pid'   => $this->pid,
35
            'data'  => $data,
36
        ));
37
        try {
38
            $this->shm->open(strlen($serializedData));
39
        } catch (SharedMemoryControlException $e) {
40
            throw $e;
41
        }
42
43
        try {
44
            $this->shm->write($serializedData);
45
        } catch (SharedMemoryControlException $e) {
46
            throw $e;
47
        }
48
49
        $this->shm->close();
50
    }
51
52
    /**
53
     * read data and delete shared memory
54
     *
55
     * @return  mixed
56
     * @throws  Snidel\Exception\SharedMemoryControlException
57
     */
58
    public function readAndDelete()
59
    {
60
        try {
61
            $data = $this->read();
62
            $this->delete();
63
        } catch (SharedMemoryControlException $e) {
64
            throw $e;
65
        }
66
67
        return $data;
68
    }
69
70
    /**
71
     * read data
72
     *
73
     * @return  array
74
     * @throws  Snidel\Exception\SharedMemoryControlException
75
     */
76
    public function read()
77
    {
78
        try {
79
            $this->shm->open();
80
            $data = $this->shm->read();
81
        } catch (SharedMemoryControlException $e) {
82
            throw $e;
83
        }
84
85
        $this->shm->close();
86
        $unserialized = unserialize($data);
87
88
        return $unserialized['data'];
89
    }
90
91
    /**
92
     * delete shared memory
93
     *
94
     * @return  void
95
     * @throws  Snidel\Exception\SharedMemoryControlException
96
     */
97
    public function delete()
98
    {
99
        try {
100
            $this->shm->open();
101
        } catch (SharedMemoryControlException $e) {
102
            throw $e;
103
        }
104
105
        try {
106
            $this->shm->delete();
107
        } catch (SharedMemoryControlException $e) {
108
            throw $e;
109
        }
110
111
        $this->shm->close($removeTmpFile = true);
112
    }
113
114
    /**
115
     * delete shared memory if exists
116
     *
117
     * @return  void
118
     * @throws  Snidel\Exception\SharedMemoryControlException
119
     */
120
    public function deleteIfExists()
121
    {
122
        if (!$this->shm->exists()) {
123
            return;
124
        }
125
126
        try {
127
            $this->delete();
128
        } catch (SharedMemoryControlException $e) {
129
            throw $e;
130
        }
131
    }
132
}
133