ValueTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 5
dl 0
loc 51
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 12 3
A set() 0 11 2
1
<?php namespace AdammBalogh\KeyValueStore\Implementation;
2
3
use AdammBalogh\KeyValueStore\Adapter\Util;
4
use AdammBalogh\KeyValueStore\Exception\InternalException;
5
use AdammBalogh\KeyValueStore\Exception\KeyNotFoundException;
6
7
/**
8
 * @SuppressWarnings(PHPMD.StaticAccess)
9
 */
10
trait ValueTrait
11
{
12
    use AdapterTrait;
13
14
    /**
15
     * Gets the value of a key.
16
     *
17
     * @param string $key
18
     *
19
     * @return mixed The value of the key.
20
     *
21
     * @throws \InvalidArgumentException
22
     * @throws KeyNotFoundException
23
     * @throws InternalException
24
     */
25
    public function get($key)
26
    {
27
        Util::checkArgString($key);
28
29
        try {
30
            return $this->getAdapter()->get($key);
31
        } catch (KeyNotFoundException $e) {
32
            throw $e;
33
        } catch (\Exception $e) {
34
            throw new InternalException($e->getMessage(), $e->getCode(), $e);
35
        }
36
    }
37
38
    /**
39
     * Sets the value of a key.
40
     *
41
     * @param string $key
42
     * @param mixed $value Can be any of serializable data type.
43
     *
44
     * @return bool True if the set was successful, false if it was unsuccessful.
45
     *
46
     * @throws \InvalidArgumentException
47
     * @throws InternalException
48
     */
49
    public function set($key, $value)
50
    {
51
        Util::checkArgString($key);
52
        Util::checkArgSerializable($value);
53
54
        try {
55
            return $this->getAdapter()->set($key, $value);
56
        } catch (\Exception $e) {
57
            throw new InternalException($e->getMessage(), $e->getCode(), $e);
58
        }
59
    }
60
}
61