FlashMessages::reset()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * FlashMessages
4
 * Add and retrieve flash messages.
5
 * Flash messages are stored in session.
6
 * Types of messages: success, info, warning, danger
7
 *
8
 * @package     erdiko/core
9
 * @copyright   2012-2017 Arroyo Labs, Inc. http://www.arroyolabs.com
10
 * @author      John Arroyo <[email protected]>
11
 */
12
namespace erdiko\core\helpers;
13
14
15
class FlashMessages
16
{
17
	/**
18
	 * Reset the messages
19
	 */
20
	public static function reset()
0 ignored issues
show
Coding Style introduced by
reset uses the super-global variable $_SESSION 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...
21
	{
22
		$_SESSION['erdiko_flash_message'] = array();
23
	}
24
25
	/**
26
	 * Set flash message
27
	 * Add a message to the array
28
	 * @note: Should we restrict/limit types to only the 4 listed in the header?
29
	 * @param string $message
30
	 * @param string $type
31
	 */
32
	public static function set($message, $type = 'danger')
0 ignored issues
show
Coding Style introduced by
set uses the super-global variable $_SESSION 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...
33
	{
34
		$_SESSION['erdiko_flash_message'][] = array(
35
			'text' => $message,
36
			'type' => $type
37
			);
38
39
		$_SESSION['erdiko_flash_message'];
40
	}
41
42
	/**
43
	 * Get flash messages
44
	 * @return array $messages
45
	 */
46
	public static function get()
0 ignored issues
show
Coding Style introduced by
get uses the super-global variable $_SESSION 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...
47
	{
48
		$messages = array();
49
		if(isset($_SESSION['erdiko_flash_message']))
50
			$messages = $_SESSION['erdiko_flash_message'];
51
		self::reset();
52
		
53
		return $messages;
54
	}
55
	
56
}