ar_http_cookieStore::putvar()   A
last analyzed

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 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 3
cp 0
crap 2
1
<?php
2
3
	ar_pinp::allow('ar_http_cookie');
4
5
	class ar_http_cookie extends arBase {
6
7
		public static function get( $name = "ARUserCookie" ) {
0 ignored issues
show
Coding Style introduced by
get uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
8
			return new ar_http_cookieStore( $name, $_COOKIE[$name] );
9
		}
10
11
	}
12
13
	class ar_http_cookieStore extends arBase implements arKeyValueStoreInterface {
14
15
		public $values = array();
16
		protected $name = 'ARUserCookie';
17
		protected $configuration = array(
18
			'expire' => null,
19
			'path'   => '/',
20
			'domain' => '',
21
			'secure' => false
22
		);
23
24
		public function __construct( $name = 'ARUserCookie', $cookie = null, $configuration = array() ) {
25
			if ( $name == 'ARSessionCookie' ) {
26
				// prevent access to Ariadne's authentication cookie
27
				$name = 'ARUserCookie';
28
			}
29
			$this->name = $name;
30
			try {
31
				$this->values = json_decode($cookie);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_decode($cookie) of type * is incompatible with the declared type array of property $values.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
32
			} catch(Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
33
			}
34
			if ( !isset($this->values) && isset($cookie) ) {
35
				$this->values = $cookie;
36
			}
37
			$this->configure( $configuration );
38
		}
39
40
		public function __set( $name, $value ) {
41
			$this->putvar( $name, $value );
42
		}
43
44
		public function __get( $name ) {
45
			return $this->getvar( $name );
46
		}
47
48
		public function putvar( $name, $value ) {
49
			$this->values[ $name ] = $value;
50
		}
51
52
		public function getvar( $name ) {
53
			return $this->values[ $name ];
54
		}
55
56 View Code Duplication
		public function configure( $name, $value = null ) {
57
			if ( is_array( $name ) ) {
58
				$this->configuration = $name + $this->configuration;
59
			} else {
60
				$this->configuration[$name] = $value;
61
			}
62
		}
63
64
		public function save( $name = null ) {
65
			if ( $name == 'ARSessionCookie' ) {
66
				// prevent access to Ariadne's authentication cookie
67
				$name = 'ARUserCookie';
68
			}
69
			$this->name = $name;
70
			//TODO/FIXME: test save with non-array/non-object value
71
			return setcookie( $this->name, json_encode( $this->values ), $this->configuration['expire'],
72
				$this->configuration['path'], $this->configuration['domain'], $this->configuration['secure'] );
73
		}
74
75
	}
76