Completed
Push — master ( 43d5f2...a04db9 )
by Yuri
05:26
created

Flash::__callStatic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4286
nc 1
cc 1
eloc 3
nop 2
crap 1
1
<?php
2
3
namespace Tamtamchik\SimpleFlash;
4
5
use Tamtamchik\SimpleFlash\Exceptions\FlashSingletonException;
6
use Tamtamchik\SimpleFlash\Templates\Bootstrap3Template;
7
8
/**
9
 * Class Flash.
10
 *
11
 * @method static Engine message($message, $type = 'info') Base method for adding messages to flash.
12
 * @method static string display($type = null) Returns Bootstrap ready HTML for Engine messages.
13
 * @method static bool hasMessages($type = null) Returns if there are any messages in container.
14
 * @method static Engine clear($type = null) Clears messages from session store.
15
 * @method static Engine error($message) Shortcut for error message.
16
 * @method static Engine warning($message) Shortcut for warning message.
17
 * @method static Engine info($message) Shortcut for info message.
18
 * @method static Engine success($message) Shortcut for success message.
19
 * @method static Engine setTemplate(TemplateInterface $template) Change render template.
20
 * @method static TemplateInterface getTemplate() Get template for modifications.
21
 */
22
class Flash
23
{
24
    /**
25
     * Base instance of Flash engine.
26
     *
27
     * @var Engine
28
     */
29
    private static $engine;
30
31
    // Don't allow instantiation
32
    private final function __clone() { }
0 ignored issues
show
Coding Style introduced by
As per PSR2, final should precede the visibility keyword.
Loading history...
33
34 3
    public final function __sleep()
0 ignored issues
show
Coding Style introduced by
As per PSR2, final should precede the visibility keyword.
Loading history...
35
    {
36 3
        throw new FlashSingletonException('Serializing of Flash is not allowed!');
37
    }
38
39
    /**
40
     * Creates flash container from session.
41
     *
42
     * @param TemplateInterface|null $template
43
     */
44 84
    public function __construct(TemplateInterface $template = null)
45
    {
46 84
        if ($assigned = is_null($template)) {
47 81
            $template = new Bootstrap3Template();
48 81
        }
49
50 84
        if ( ! $assigned || ! isset(self::$engine)) {
51 9
            self::$engine = new Engine($template);
52 9
        }
53 84
    }
54
55
    /**
56
     * Invoke Engine methods.
57
     *
58
     * @param string $method    - method to invoke
59
     * @param array  $arguments - arguments for method
60
     *
61
     * @return mixed
62
     */
63 78
    protected static function invoke($method, array $arguments)
64
    {
65
        $target = [
66 78
            self::$engine,
67 78
            $method,
68 78
        ];
69
70 78
        return call_user_func_array($target, $arguments);
71
    }
72
73
    /**
74
     * Magic methods for static calls.
75
     *
76
     * @param string $method    - method to invoke
77
     * @param array  $arguments - arguments for method
78
     *
79
     * @return mixed
80
     */
81 6
    public static function __callStatic($method, array $arguments)
82
    {
83 6
        new self();
84
85 6
        return self::invoke($method, $arguments);
86
    }
87
88
    /**
89
     * Magic methods for instances calls.
90
     *
91
     * @param string $method    - method to invoke
92
     * @param array  $arguments - arguments for method
93
     *
94
     * @return mixed
95
     */
96 72
    public function __call($method, array $arguments)
97
    {
98 72
        return $this->invoke($method, $arguments);
99
    }
100
101
    /**
102
     * Mimic object __toString method.
103
     *
104
     * @return string - HTML with flash messages
105
     */
106 6
    public function __toString()
107
    {
108 6
        return strval(self::$engine);
109
    }
110
}
111