Completed
Push — master ( 21fd07...bea5f6 )
by Nazar
04:17
created

Data::del_data()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 3
rs 9.4285
c 1
b 1
f 1
1
<?php
2
/**
3
 * @package   CleverStyle Framework
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs\Session;
9
use
10
	cs\User;
11
12
trait Data {
13
	/**
14
	 * Get data, stored with session
15
	 *
16
	 * @param string      $item
17
	 * @param null|string $session_id
18
	 *
19
	 * @return false|mixed
20
	 *
21
	 */
22 2
	function get_data ($item, $session_id = null) {
23 2
		$session_data = $this->get_data_internal($session_id);
24 2
		return isset($session_data['data'][$item]) ? $session_data['data'][$item] : false;
25
	}
26
	/*
27
	 * @param null|string $session_id
28
	 *
29
	 * @return array|false
30
	 */
31 2
	protected function get_data_internal ($session_id) {
32 2
		$session_id = $session_id ?: $this->session_id;
33 2
		return is_md5($session_id) ? $this->get_internal($session_id) : false;
34
	}
35
	/**
36
	 * Store data with session
37
	 *
38
	 * @param string      $item
39
	 * @param mixed       $value
40
	 * @param null|string $session_id
41
	 *
42
	 * @return bool
43
	 *
44
	 */
45 2
	function set_data ($item, $value, $session_id = null) {
46 2
		$session_data = $this->get_data_internal($session_id);
47
		/**
48
		 * If there is no session yet - let's create one
49
		 */
50 2
		if (!$session_data) {
51 2
			$session_id   = $this->add(User::GUEST_ID);
52 2
			$session_data = $this->get_data_internal($session_id);
53
		}
54 2
		$session_data['data'][$item] = $value;
55 2
		return $this->update($session_data) && $this->cache->del($session_data['id']);
56
	}
57
	/**
58
	 * Delete data, stored with session
59
	 *
60
	 * @param string      $item
61
	 * @param null|string $session_id
62
	 *
63
	 * @return bool
64
	 *
65
	 */
66 2
	function del_data ($item, $session_id = null) {
67 2
		$session_data = $this->get_data_internal($session_id);
68 2
		if (!isset($session_data['data'][$item])) {
69 2
			return true;
70
		}
71 2
		unset($session_data['data'][$item]);
72 2
		return $this->update($session_data) && $this->cache->del($session_data['id']);
1 ignored issue
show
Bug introduced by
It seems like update() 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...
73
	}
74
}
75