KeyTrait::delete()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
1
<?php namespace AdammBalogh\KeyValueStore\Adapter\SharedMemoryAdapter;
2
3
use AdammBalogh\KeyValueStore\Adapter\Util;
4
use AdammBalogh\KeyValueStore\Exception\KeyNotFoundException;
5
6
/**
7
 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
8
 */
9
trait KeyTrait
10
{
11
    /**
12
     * Removes a key.
13
     *
14
     * @param string $key
15
     *
16
     * @return bool True if the deletion was successful, false if the deletion was unsuccessful.
17
     *
18
     * @throws \Exception
19
     */
20
    public function delete($key)
21
    {
22
        try {
23
            $this->get($key);
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
24
        } catch (KeyNotFoundException $e) {
25
            return false;
26
        }
27
28
        if (!$this->shmProxy->remove($this->client, $key)) {
0 ignored issues
show
Bug introduced by
The property shmProxy 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...
Bug introduced by
The property client 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...
29
            throw new \Exception('Shm remove error');
30
        }
31
32
        return true;
33
    }
34
35
    /**
36
     * Sets a key's time to live in seconds.
37
     *
38
     * @param string $key
39
     * @param int $seconds
40
     *
41
     * @return bool True if the timeout was set, false if the timeout could not be set.
42
     *
43
     * @throws \Exception
44
     */
45
    public function expire($key, $seconds)
46
    {
47
        try {
48
            $value = $this->get($key);
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
49
        } catch (KeyNotFoundException $e) {
50
            return false;
51
        }
52
53
        return $this->set($key, Util::getDataWithExpire($value, $seconds, time()));
0 ignored issues
show
Bug introduced by
It seems like set() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
54
    }
55
56
    /**
57
     * Returns the remaining time to live of a key that has a timeout.
58
     *
59
     * @param string $key
60
     *
61
     * @return int Ttl in seconds.
62
     *
63
     * @throws KeyNotFoundException
64
     * @throws \Exception
65
     */
66
    public function getTtl($key)
67
    {
68
        if (!$this->shmProxy->has($this->client, $key)) {
69
            throw new KeyNotFoundException();
70
        }
71
72
        $getResult = $this->shmProxy->get($this->client, $key);
73
        $unserialized = @unserialize($getResult);
74
75
        if (!Util::hasInternalExpireTime($unserialized)) {
76
            throw new \Exception('Cannot retrieve ttl');
77
        }
78
79
        return $this->handleTtl($key, $unserialized['ts'], $unserialized['s']);
0 ignored issues
show
Bug introduced by
It seems like handleTtl() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
80
    }
81
82
    /**
83
     * Determines if a key exists.
84
     *
85
     * @param string $key
86
     *
87
     * @return bool True if the key does exist, false if the key does not exist.
88
     *
89
     * @throws \Exception
90
     */
91
    public function has($key)
92
    {
93
        try {
94
            $this->get($key);
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
95
        } catch (KeyNotFoundException $e) {
96
            return false;
97
        }
98
99
        return true;
100
    }
101
102
    /**
103
     * Removes the existing timeout on key, turning the key from volatile (a key with an expire set)
104
     * to persistent (a key that will never expire as no timeout is associated).
105
     *
106
     * @param string $key
107
     *
108
     * @return bool True if the persist was success, false if the persis was unsuccessful.
109
     *
110
     * @throws \Exception
111
     */
112
    public function persist($key)
113
    {
114
        if (!$this->shmProxy->has($this->client, $key)) {
115
            return false;
116
        }
117
118
        $getResult = $this->shmProxy->get($this->client, $key);
119
        $unserialized = @unserialize($getResult);
120
121
        if (!Util::hasInternalExpireTime($unserialized)) {
122
            return false;
123
        }
124
125
        try {
126
            $this->handleTtl($key, $unserialized['ts'], $unserialized['s']);
0 ignored issues
show
Bug introduced by
It seems like handleTtl() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
127
        } catch (KeyNotFoundException $e) {
128
            return false;
129
        }
130
131
        return $this->set($key, $unserialized['v']);
0 ignored issues
show
Bug introduced by
It seems like set() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
132
    }
133
}
134