Passed
Push — main ( 915a08...2be97f )
by Sammy
01:45
created

StateAgent.class.php (1 issue)

Severity
1
<?php
2
3
namespace HexMakina\kadro;
4
5
class StateAgent
6
{
7
  private $index_filter = 'filter';
8
  private $index_operator = 'operator';
9
10
  // IS-54-16 : Behold, I have created the smith who blows the fire of coals
11
  // $options : https://www.php.net/manual/fr/session.configuration.php
12
  public function __construct($options)
13
  {
14
    if(isset($options['session_name']))
15
    {
16
      session_name($options['session_name']);
17
      unset($options['session_name']);
18
    }
19
    
20
    session_start($options); // https://www.php.net/manual/fr/function.session-start.php
21
  }
22
23
  // IS-54-16 : and produces a weapon for its purpose
24
25
  public function add_runtime_filters($filters)
26
  {
27
    $_SESSION[$this->index_filter] = array_merge($_SESSION[$this->index_filter] ?? [], $filters);
28
  }
29
30
  public function has_filter($filter_name) : bool
31
  {
32
    return isset($_SESSION[$this->index_filter][$filter_name]) && strlen(''.$_SESSION[$this->index_filter][$filter_name]) > 0;
33
  }
34
35
  public function filters($filter_name=null, $value=null)
36
  {
37
    if(is_null($filter_name))
38
      return $_SESSION[$this->index_filter];
39
40
    if(!is_null($value))
41
      $_SESSION[$this->index_filter][$filter_name] = $value;
42
43
    return $_SESSION[$this->index_filter][$filter_name] ?? null;
44
  }
45
46
  public function reset_filters($filter_name=null)
47
  {
48
    if(is_null($filter_name))
49
      $_SESSION[$this->index_filter]=[];
50
    else
51
      unset($_SESSION[$this->index_filter][$filter_name]);
52
  }
53
54
  public function operator_id($setter = null)
55
  {
56
    if(!is_null($setter))
57
      $_SESSION[$this->index_operator] = ['id' => $setter, 'set_on' => time()];
58
59
    return $_SESSION[$this->index_operator]['id'] ?? null;
60
  }
61
62
  public function operator_started_on()
63
  {
64
    return $_SESSION[$this->index_operator]['set_on'] ?? null;
65
  }
66
67
  // IS-54-16 : I have also created the ravager to destroy
68
  public function destroy() : bool
69
  {
70
71
    if(ini_get("session.use_cookies"))
72
    {
73
      $params = session_get_cookie_params();
74
      setcookie(session_name(), '', time() - 42000,
75
        $params["path"], $params["domain"],
76
        $params["secure"], $params["httponly"]
77
      );
78
    }
79
    return session_destroy();
80
  }
81
}
82
?>
0 ignored issues
show
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
83