Passed
Push — master ( 4c7da5...a55b86 )
by Ilia
09:52
created

DatabaseSessionHandler::close()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Elgg\Http;
3
4
/**
5
 * Database session handler
6
 *
7
 * @access private
8
 * 
9
 * @package    Elgg.Core
10
 * @subpackage Http
11
 */
12
class DatabaseSessionHandler implements \SessionHandlerInterface {
13
14
	/** @var \Elgg\Database $db */
15
	protected $db;
16
17
	/**
18
	 * Constructor
19
	 *
20
	 * @param \Elgg\Database $db The database
21
	 */
22
	public function __construct(\Elgg\Database $db) {
23
		$this->db = $db;
24
	}
25
26
	/**
27
	 * {@inheritDoc}
28
	 */
29
	public function open($save_path, $name) {
30
		return true;
31
	}
32
33
	/**
34
	 * {@inheritDoc}
35
	 */
36
	public function read($session_id) {
37
		
38
		$id = sanitize_string($session_id);
39
		$query = "SELECT * FROM {$this->db->getTablePrefix()}users_sessions WHERE session='$id'";
40
		$result = $this->db->getDataRow($query);
41
		if ($result) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
42
			return (string) $result->data;
43
		} else {
44
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface SessionHandlerInterface::read of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
45
		}
46
	}
47
48
	/**
49
	 * {@inheritDoc}
50
	 */
51
	public function write($session_id, $session_data) {
52
		$id = sanitize_string($session_id);
53
		$time = time();
54
		$sess_data_sanitised = sanitize_string($session_data);
55
		
56
		// do not persist a session for health probes
57
		if (strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'kube-probe') !== false && $_SERVER["REQUEST_URI"] == '/splash')
58
			return true;
59
		if ($_SERVER["REQUEST_URI"] == '/')
60
			return true;
61
		if (strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'solr-crawler') !== false)
62
			return true;
63
		if (strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'SimplePie') !== false)
64
			return true;
65
66
		$query = "REPLACE INTO {$this->db->getTablePrefix()}users_sessions
67
			(session, ts, data) VALUES
68
			('$id', '$time', '$sess_data_sanitised')";
69
70
		if ($this->db->insertData($query) !== false) {
71
			return true;
72
		} else {
73
			return false;
74
		}
75
	}
76
77
	/**
78
	 * {@inheritDoc}
79
	 */
80
	public function close() {
81
		return true;
82
	}
83
84
	/**
85
	 * {@inheritDoc}
86
	 */
87
	public function destroy($session_id) {
88
		
89
		$id = sanitize_string($session_id);
90
		$query = "DELETE FROM {$this->db->getTablePrefix()}users_sessions WHERE session='$id'";
91
		return (bool) $this->db->deleteData($query);
92
	}
93
94
	/**
95
	 * {@inheritDoc}
96
	 */
97
	public function gc($max_lifetime) {
98
		
99
		$life = time() - $max_lifetime;
100
		$query = "DELETE FROM {$this->db->getTablePrefix()}users_sessions WHERE ts < '$life'";
101
		return (bool) $this->db->deleteData($query);
102
	}
103
}
104