Skip to content

Commit f01d829

Browse files
authored
Merge pull request #14 from tattersoftware/cleanup
Cleanup
2 parents db6fbc3 + d1ce45e commit f01d829

File tree

7 files changed

+147
-43
lines changed

7 files changed

+147
-43
lines changed

src/Alerts.php

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use CodeIgniter\Session\Session;
1515
use CodeIgniter\View\RendererInterface;
16-
use Config\Services;
1716
use Tatter\Alerts\Config\Alerts as AlertsConfig;
1817
use 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');

src/Config/Alerts.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,24 @@
1515

1616
class Alerts extends BaseConfig
1717
{
18-
// prefix for SESSION variables and HTML classes, to prevent collision
18+
/**
19+
* Prefix for SESSION variables and HTML classes, to prevent collision.
20+
*
21+
* @var string
22+
*/
1923
public $prefix = 'alerts-';
2024

21-
// Template to use for HTML output
25+
/**
26+
* Template to use for HTML output.
27+
*
28+
* @var string
29+
*/
2230
public $template = 'Tatter\\Alerts\\Views\\bootstrap';
2331

24-
// Whether to check session flashdata for common alert keys
32+
/**
33+
* Whether to check session flashdata for common alert keys.
34+
*
35+
* @var bool
36+
*/
2537
public $getflash = true;
2638
}

src/Exceptions/AlertsException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ class AlertsException extends FrameworkException implements ExceptionInterface
1818
{
1919
public static function forInvalidTemplate(string $template = null)
2020
{
21-
return new static(lang('Alerts.invalidTemplate', [$template]));
21+
return new static(lang('Alerts.invalidTemplate', [$template ?? '']));
2222
}
2323

2424
public static function forMissingTemplateView(string $template = null)
2525
{
26-
return new static(lang('Alerts.missingTemplateView', [$template]));
26+
return new static(lang('Alerts.missingTemplateView', [$template ?? '']));
2727
}
2828
}

src/Helpers/alerts_helper.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@
1010
*/
1111

1212
if (! function_exists('alert')) {
13-
// add a new alert to the queue
13+
/**
14+
* Adds a new alert to the queue.
15+
*
16+
* @param string $class Class to apply, e.g. "info", "success"
17+
* @param string $text Text of the alert
18+
*
19+
* @return void
20+
*/
1421
function alert(string $class, string $text)
1522
{
1623
$alerts = service('alerts');

tests/_support/AlertsTestCase.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/**
4+
* This file is part of Tatter Alerts.
5+
*
6+
* (c) 2021 Tatter Software
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace Tests\Support;
13+
14+
use CodeIgniter\Test\CIUnitTestCase;
15+
use Config\Services;
16+
17+
abstract class AlertsTestCase extends CIUnitTestCase
18+
{
19+
protected function setUp(): void
20+
{
21+
Services::reset();
22+
23+
parent::setUp();
24+
}
25+
}

tests/unit/LibraryTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/**
4+
* This file is part of Tatter Alerts.
5+
*
6+
* (c) 2021 Tatter Software
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
use Tatter\Alerts\Alerts;
13+
use Tests\Support\AlertsTestCase;
14+
15+
/**
16+
* @internal
17+
*/
18+
final class LibraryTest extends AlertsTestCase
19+
{
20+
public function testAdd()
21+
{
22+
$expected = [
23+
[
24+
'class' => 'fruit',
25+
'text' => 'banana',
26+
],
27+
[
28+
'class' => 'candy',
29+
'text' => 'snickers',
30+
],
31+
];
32+
33+
service('alerts')->add('fruit', 'banana');
34+
service('alerts')->add('candy', 'snickers');
35+
36+
$this->assertSame($expected, session('alerts-queue'));
37+
}
38+
39+
public function testCss()
40+
{
41+
$expected = view('Tatter\Alerts\Views\css', ['prefix' => 'alerts-']);
42+
$result = service('alerts')->css();
43+
44+
$this->assertSame($expected, $result);
45+
}
46+
}

tests/unit/ServiceTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
* the LICENSE file that was distributed with this source code.
1010
*/
1111

12-
use CodeIgniter\Test\CIUnitTestCase;
1312
use Tatter\Alerts\Alerts;
13+
use Tests\Support\AlertsTestCase;
1414

1515
/**
1616
* @internal
1717
*/
18-
final class ServiceTest extends CIUnitTestCase
18+
final class ServiceTest extends AlertsTestCase
1919
{
2020
public function testServiceReturnsLibrary()
2121
{

0 commit comments

Comments
 (0)