Completed
Push — master ( afd267...dbe270 )
by Nazar
04:25
created

Data   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get_data() 0 4 2
A get_data_internal() 0 4 3
A set_data() 0 15 4
A del_data() 0 7 3
1
<?php
2
/**
3
 * @package   CleverStyle CMS
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
	function get_data ($item, $session_id = null) {
23
		$session_data = $this->get_data_internal($session_id);
24
		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
	protected function get_data_internal ($session_id) {
32
		$session_id = $session_id ?: $this->session_id;
33
		return is_md5($session_id) ? $this->get_internal($session_id) : false;
1 ignored issue
show
Bug introduced by
It seems like get_internal() 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...
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
	function set_data ($item, $value, $session_id = null) {
46
		$session_data = $this->get_data_internal($session_id);
47
		/**
48
		 * If there is no session yet - let's create one
49
		 */
50
		if (!$session_data) {
51
			$session_id   = $this->add(User::GUEST_ID);
1 ignored issue
show
Bug introduced by
It seems like add() 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...
52
			$session_data = $this->get_data_internal($session_id);
53
		}
54
		if (!isset($session_data['data'])) {
55
			return false;
56
		}
57
		$session_data['data'][$item] = $value;
58
		return $this->update($session_data) && $this->cache->del($session_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...
59
	}
60
	/**
61
	 * Delete data, stored with session
62
	 *
63
	 * @param string      $item
64
	 * @param null|string $session_id
65
	 *
66
	 * @return bool
67
	 *
68
	 */
69
	function del_data ($item, $session_id = null) {
70
		$session_data = $this->get_data_internal($session_id);
71
		if (!isset($session_data['data'][$item])) {
72
			return true;
73
		}
74
		return $this->update($session_data) && $this->cache->del($session_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...
75
	}
76
}
77