Flash::__callStatic()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Tamtamchik\SimpleFlash;
4
5
use Tamtamchik\SimpleFlash\Core\Engine;
6
use Tamtamchik\SimpleFlash\Exceptions\FlashSingletonException;
7
8
/**
9
 * Class Flash.
10
 *
11
 * @method static Engine setTemplate(TemplateInterface $template) Change render template.
12
 * @method static TemplateInterface getTemplate() Get template for modifications.
13
 *
14
 * @method static bool some($type = null) Returns if there are any messages in container.
15
 * @method static Engine message($message, $type = 'info') Base method for adding messages to flash.
16
 * @method static Engine clear($type = null) Clears messages from session store.
17
 *
18
 * @method static Engine error($message) Shortcut for error message.
19
 * @method static Engine warning($message) Shortcut for warning message.
20
 * @method static Engine info($message) Shortcut for info message.
21
 * @method static Engine success($message) Shortcut for success message.
22
 *
23
 * @method static string display($type = null, $template = Templates::BASE) Returns Bootstrap ready HTML for messages.
24
 * @method static string displayBootstrap($type = null) Returns Bootstrap ready for messages.
25
 * @method static string displayFoundation($type = null) Returns Foundation ready for messages.
26
 * @method static string displayBulma($type = null) Returns Bulma ready HTML for messages.
27
 * @method static string displayMaterialize($type = null) Returns Materialize ready for messages.
28
 * @method static string displayTailwind($type = null) Returns Tailwind ready for messages.
29
 * @method static string displayPrimer($type = null) Returns Primer ready for messages.
30
 * @method static string displayUiKit($type = null) Returns UiKit ready for messages.
31
 * @method static string displaySemantic($type = null) Returns Semantic UI ready for messages.
32
 * @method static string displaySpectre($type = null) Returns Spectre.css ready for messages.
33
 * @method static string displayHalfmoon($type = null) Returns Spectre.css ready for messages.
34
 */
35
class Flash
36
{
37
    /**
38
     * Base instance of Flash engine.
39
     *
40
     * @var Engine
41
     */
42
    private static $engine;
43
44
    /**
45
     * Creates flash container from session.
46
     *
47
     * @param TemplateInterface|null $template
48
     * @throws Exceptions\FlashTemplateNotFoundException
49
     */
50 50
    public function __construct(?TemplateInterface $template = null)
51
    {
52 50
        if ($assigned = is_null($template)) {
53 49
            $template = TemplateFactory::create();
54
        }
55
56 50
        if ( ! $assigned || ! isset(self::$engine)) {
57 2
            self::$engine = new Engine($template);
58
        }
59
    }
60
61
    /**
62
     * Magic methods for static calls.
63
     *
64
     * @param string $method - method to invoke
65
     * @param array $arguments - arguments for method
66
     *
67
     * @return mixed
68
     */
69 2
    public static function __callStatic(string $method, array $arguments)
70
    {
71 2
        new self();
72
73 2
        return self::invoke($method, $arguments);
74
    }
75
76
    /**
77
     * Invoke Engine methods.
78
     *
79
     * @param string $method - method to invoke
80
     * @param array $arguments - arguments for method
81
     *
82
     * @return mixed
83
     */
84 48
    protected static function invoke(string $method, array $arguments)
85
    {
86 48
        return call_user_func_array([self::$engine, $method], $arguments);
87
    }
88
89
    /**
90
     * @throws FlashSingletonException
91
     */
92 1
    public function __sleep()
93
    {
94 1
        throw new FlashSingletonException('Serialization of Flash is not allowed!');
95
    }
96
97
    /**
98
     * Magic methods for instances calls.
99
     *
100
     * @param string $method - method to invoke
101
     * @param array $arguments - arguments for method
102
     *
103
     * @return mixed
104
     */
105 46
    public function __call(string $method, array $arguments)
106
    {
107 46
        return $this->invoke($method, $arguments);
108
    }
109
110
    /**
111
     * Mimic object __toString method.
112
     *
113
     * @return string - HTML with flash messages
114
     */
115 2
    public function __toString()
116
    {
117 2
        return strval(self::$engine);
118
    }
119
120
    /**
121
     * Don't allow instantiation.
122
     */
123
    private function __clone()
124
    {
125
    }
126
}
127