1313
1414use CodeIgniter \Session \Session ;
1515use CodeIgniter \View \RendererInterface ;
16- use Config \Services ;
1716use Tatter \Alerts \Config \Alerts as AlertsConfig ;
1817use Tatter \Alerts \Exceptions \AlertsException ;
1918
@@ -40,60 +39,70 @@ class Alerts
4039 */
4140 protected $ session ;
4241
43- // initiate library, check for existing session
42+ /**
43+ * The session key to use for the queue.
44+ *
45+ * @var string
46+ */
47+ private $ key ;
48+
49+ /**
50+ * Initiates the library, check for existing session.
51+ *
52+ * @param AlertsConfig $config
53+ * @param RendererInterface|null $view
54+ */
4455 public function __construct (AlertsConfig $ config , RendererInterface $ view = null )
4556 {
46- // save configuration
47- $ this ->config = $ config ;
48-
49- // initiate the Session library
50- $ this ->session = Services::session ();
51-
52- // verify renderer
53- if ($ view instanceof RendererInterface) {
54- $ this ->view = $ view ;
55- } else {
56- $ this ->view = Services::renderer ();
57- }
57+ $ this ->config = $ config ;
58+ $ this ->session = service ('session ' );
59+ $ this ->view = $ view ?? service ('renderer ' );
60+ $ this ->key = $ this ->config ->prefix . 'queue ' ;
5861
59- // validations
62+ // Validate the configuration
6063 if (empty ($ this ->config ->template )) {
6164 throw AlertsException::forInvalidTemplate ('' );
6265 }
63-
64- $ locator = Services::locator ();
65- if (! $ locator ->locateFile ($ this ->config ->template )) {
66+ if (! service ('locator ' )->locateFile ($ this ->config ->template )) {
6667 throw AlertsException::forMissingTemplateView ($ this ->config ->template );
6768 }
6869 }
6970
70- // add a new alert to the queue
71+ /**
72+ * Adds a new alert to the queue.
73+ *
74+ * @param string $class Class to apply, e.g. "info", "success"
75+ * @param string $text Text of the alert
76+ *
77+ * @return void
78+ */
7179 public function add ($ class , $ text )
7280 {
7381 $ alert = [
7482 'class ' => $ class ,
7583 'text ' => $ text ,
7684 ];
7785
78- // start the queue if it doesn't exist
79- if (! $ this ->session ->has ($ this ->config -> prefix . ' queue ' )) {
80- $ this ->session ->set ($ this ->config -> prefix . ' queue ' , [$ alert ]);
86+ // Start the queue if it doesn't exist
87+ if (! $ this ->session ->has ($ this ->key )) {
88+ $ this ->session ->set ($ this ->key , [$ alert ]);
8189 }
82-
83- // push onto the queue if it was already there
90+ // Push onto the queue if it was already there
8491 else {
85- $ this ->session ->push ($ this ->config -> prefix . ' queue ' , [$ alert ]);
92+ $ this ->session ->push ($ this ->key , [$ alert ]);
8693 }
8794 }
8895
89- // clears the queue and returns template formatted alerts
96+ /**
97+ * Clears the queue and returns template formatted alerts.
98+ *
99+ * @return string
100+ */
90101 public function display ()
91102 {
92- // get any alerts
93- $ alerts = $ this ->session ->get ($ this ->config ->prefix . 'queue ' ) ?? [];
94-
95- // clear alerts
96- $ this ->session ->remove ($ this ->config ->prefix . 'queue ' );
103+ // Retrieve and clear the queue
104+ $ alerts = $ this ->session ->get ($ this ->key ) ?? [];
105+ $ this ->session ->remove ($ this ->key );
97106
98107 // Check for flashdata (if configured)
99108 if ($ this ->config ->getflash ) {
@@ -109,16 +118,21 @@ public function display()
109118 }
110119
111120 if (empty ($ alerts )) {
112- return ;
121+ return '' ;
113122 }
114- // render the specified view template
123+
124+ // Render the specified view template
115125 return $ this ->view ->setVar ('prefix ' , $ this ->config ->prefix )
116126 ->setVar ('alerts ' , $ alerts )
117127 ->render ($ this ->config ->template );
118128 }
119129
120- // returns default CSS as inline style sheet
121- // should be injected into <head>
130+ /**
131+ * Returns default CSS as inline style sheet to be
132+ * included in the <head> tag.
133+ *
134+ * @return string
135+ */
122136 public function css ()
123137 {
124138 return $ this ->view ->setVar ('prefix ' , $ this ->config ->prefix )->render ('Tatter\Alerts\Views\css ' );
0 commit comments