Test Setup Failed
Push — master ( f298e7...6a9768 )
by Barry
07:17
created

Cookie::get_session_from_cookie()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 8
nc 4
nop 0
dl 0
loc 16
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
namespace Never5\DownloadMonitor\Shop\Session;
4
5
use Never5\DownloadMonitor\Shop\Services\Services;
6
7
/**
8
 * Class Cookie
9
 * @package Never5\DownloadMonitor\Shop\Session
10
 *
11
 * This class handles all cookie related things for the session cookie
12
 */
13
class Cookie {
14
15
	const COOKIE_NAME = 'dlm_session';
16
17
	/**
18
	 * Get session from cookie
19
	 *
20
	 * @return Session
21
	 */
22
	public function get_session_from_cookie() {
23
		$session = null;
24
25
		if ( isset( $_COOKIE[ self::COOKIE_NAME ] ) ) {
26
			$cookie_data = json_decode( base64_decode( $_COOKIE[ self::COOKIE_NAME ] ), true );
27
28
			if ( is_array( $cookie_data ) && ! empty( $cookie_data['key'] ) && ! empty( $cookie_data['hash'] ) ) {
29
30
				try {
31
					$session = Services::get()->service( 'session_repository' )->retrieve( $cookie_data['key'], $cookie_data['hash'] );
32
				} catch ( \Exception $exception ) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
33
				}
34
			}
35
		}
36
37
		return $session;
38
	}
39
40
	/**
41
	 * Set the session cookie
42
	 *
43
	 * @param Session $session
44
	 */
45
	public function set_session_cookie( $session ) {
46
		setcookie( self::COOKIE_NAME, base64_encode( json_encode( array(
47
			'key'  => $session->get_key(),
48
			'hash' => $session->get_hash()
49
		) ) ), $session->get_expiry()->getTimestamp(), COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, false, true );
0 ignored issues
show
Bug introduced by
The constant Never5\DownloadMonitor\Shop\Session\COOKIE_DOMAIN was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant Never5\DownloadMonitor\Shop\Session\COOKIEPATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
50
	}
51
52
	/**
53
	 * Destroy the session cookie
54
	 */
55
	public function destroy_session_cookie() {
56
		setcookie( self::COOKIE_NAME, "", 1, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, false, true );
0 ignored issues
show
Bug introduced by
The constant Never5\DownloadMonitor\Shop\Session\COOKIEPATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant Never5\DownloadMonitor\Shop\Session\COOKIE_DOMAIN was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
57
	}
58
59
	/**
60
	 * Returns true if it's safe to set cookie, false if it's not
61
	 *
62
	 * @return bool
63
	 */
64
	public function is_cookie_allowed() {
65
		if ( headers_sent( $file, $line ) ) {
66
			return false;
67
		}
68
69
		return true;
70
	}
71
}