Passed
Push — main ( 6069e1...9002b9 )
by Sammy
07:39 queued 05:53
created

Smith.php (2 issues)

1
<?php
2
3
namespace HexMakina\Smith;
4
5
class Smith
6
{
7
  const REPORTING_USER = 'user_messages';
8
9
  private $index_filter = 'filter';
10
  private $index_operator = 'operator';
11
  private $index_messages = 'user_messages';
0 ignored issues
show
The private property $index_messages is not used, and could be removed.
Loading history...
12
13
  // IS-54-16 : Behold, I have created the smith who blows the fire of coals
14
  // $options : https://www.php.net/manual/fr/session.configuration.php
15
  public function __construct($options)
16
  {
17
    if(isset($options['session_name']))
18
    {
19
      session_name($options['session_name']);
20
      unset($options['session_name']);
21
    }
22
23
    session_start($options); // https://www.php.net/manual/fr/function.session-start.php
24
  }
25
26
27
  // IS-54-16 : and produces a weapon for its purpose
28
  public function add_message($level, $message, $context=[])
29
  {
30
    if(!isset($_SESSION[self::REPORTING_USER]))
31
      $_SESSION[self::REPORTING_USER] = [];
32
33
    if(!isset($_SESSION[self::REPORTING_USER][$level]))
34
      $_SESSION[self::REPORTING_USER][$level] = [];
35
36
    $_SESSION[self::REPORTING_USER][$level][] = [$message, $context];
37
  }
38
39
  public function messages($level=null)
40
  {
41
    if(is_null($level))
42
      return $_SESSION[self::REPORTING_USER];
43
44
    return $_SESSION[self::REPORTING_USER][$level] ?? null;
45
  }
46
47
  public function reset_messages($level=null)
48
  {
49
    $this->reset(self::REPORTING_USER, $level);
50
  }
51
52
53
  public function add_runtime_filters($filters)
54
  {
55
    $_SESSION[self::$this->index_filter] = array_merge($_SESSION[$this->index_filter] ?? [], $filters);
0 ignored issues
show
Bug Best Practice introduced by
The property this does not exist on HexMakina\Smith\Smith. Did you maybe forget to declare it?
Loading history...
56
  }
57
58
  public function has_filter($filter_name) : bool
59
  {
60
    return isset($_SESSION[$this->index_filter][$filter_name]) && strlen(''.$_SESSION[$this->index_filter][$filter_name]) > 0;
61
  }
62
63
  public function filters($filter_name=null, $value=null)
64
  {
65
    if(is_null($filter_name))
66
      return $_SESSION[$this->index_filter];
67
68
    if(!is_null($value))
69
      $_SESSION[$this->index_filter][$filter_name] = $value;
70
71
    return $_SESSION[$this->index_filter][$filter_name] ?? null;
72
  }
73
74
  public function reset_filters($filter_name=null)
75
  {
76
    $this->reset($this->index_filter, $filter_name);
77
  }
78
79
  public function operator_id($setter = null)
80
  {
81
    if(!is_null($setter))
82
      $_SESSION[$this->index_operator] = ['id' => $setter, 'set_on' => time()];
83
84
    return $_SESSION[$this->index_operator]['id'] ?? null;
85
  }
86
87
  public function operator_started_on()
88
  {
89
    return $_SESSION[$this->index_operator]['set_on'] ?? null;
90
  }
91
92
  // IS-54-16 : I have also created the ravager to destroy
93
  public function destroy() : bool
94
  {
95
96
    if(ini_get("session.use_cookies"))
97
    {
98
      $params = session_get_cookie_params();
99
      setcookie(session_name(), '', time() - 42000,
100
        $params["path"], $params["domain"],
101
        $params["secure"], $params["httponly"]
102
      );
103
    }
104
    return session_destroy();
105
  }
106
107
  private function reset($index, $part=null)
108
  {
109
    if(is_null($part))
110
      $_SESSION[$index]=[];
111
    else
112
      unset($_SESSION[$index][$part]);
113
  }
114
}
115