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

Snidel_Data::genKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %
Metric Value
dl 9
loc 9
rs 9.6667
cc 2
eloc 5
nc 2
nop 0
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
    /**
8
     * @param   int     $pid
9
     */
10
    public function __construct($pid)
11
    {
12
        $this->pid = $pid;
13
        $this->shm = new Snidel_SharedMemory($pid);
0 ignored issues
show
Bug introduced by
The property shm does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
14
    }
15
16
    /**
17
     * write data
18
     *
19
     * @param   mix     $data
20
     * @return  void
21
     * @throws  RuntimeException
22
     */
23
    public function write($data)
24
    {
25
        $serializedData = serialize(array(
26
            'pid'   => $this->pid,
27
            'data'  => $data,
28
        ));
29
        try {
30
            $this->shm->open(strlen($serializedData));
31
        } catch (RuntimeException $e) {
32
            throw $e;
33
        }
34
35
        try {
36
            $this->shm->write($serializedData);
37
        } catch (RuntimeException $e) {
38
            throw $e;
39
        }
40
41
        $this->shm->close();
42
    }
43
44
    /**
45
     * read data and delete shared memory
46
     *
47
     * @return  mix
48
     * @throws  RuntimeException
49
     */
50
    public function readAndDelete()
51
    {
52
        try {
53
            $data = $this->read();
54
            $this->delete();
55
        } catch (RuntimeException $e) {
56
            throw $e;
57
        }
58
59
        return $data;
60
    }
61
62
    /**
63
     * read data
64
     *
65
     * @return  array
66
     * @throws  RuntimeException
67
     */
68
    public function read()
69
    {
70
        try {
71
            $this->shm->open();
72
            $data = $this->shm->read();
73
        } catch (RuntimeException $e) {
74
            throw $e;
75
        }
76
77
        $this->shm->close();
78
        $unserialized = unserialize($data);
79
80
        return $unserialized['data'];
81
    }
82
83
    /**
84
     * delete shared memory
85
     *
86
     * @return  void
87
     * @throws  RuntimeException
88
     */
89
    public function delete()
90
    {
91
        try {
92
            $this->shm->open();
93
        } catch (RuntimeException $e) {
94
            return;
95
        }
96
97
        try {
98
            $this->shm->delete();
99
        } catch (RuntimeException $e) {
100
            throw $e;
101
        }
102
103
        $this->shm->close($removeTmpFile = true);
104
    }
105
}
106