Skip to content

Commit

Permalink
Merge pull request #14 from tattersoftware/cleanup
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
MGatner authored Aug 31, 2021
2 parents db6fbc3 + d1ce45e commit f01d829
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 43 deletions.
84 changes: 49 additions & 35 deletions src/Alerts.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use CodeIgniter\Session\Session;
use CodeIgniter\View\RendererInterface;
use Config\Services;
use Tatter\Alerts\Config\Alerts as AlertsConfig;
use Tatter\Alerts\Exceptions\AlertsException;

Expand All @@ -40,60 +39,70 @@ class Alerts
*/
protected $session;

// initiate library, check for existing session
/**
* The session key to use for the queue.
*
* @var string
*/
private $key;

/**
* Initiates the library, check for existing session.
*
* @param AlertsConfig $config
* @param RendererInterface|null $view
*/
public function __construct(AlertsConfig $config, RendererInterface $view = null)
{
// save configuration
$this->config = $config;

// initiate the Session library
$this->session = Services::session();

// verify renderer
if ($view instanceof RendererInterface) {
$this->view = $view;
} else {
$this->view = Services::renderer();
}
$this->config = $config;
$this->session = service('session');
$this->view = $view ?? service('renderer');
$this->key = $this->config->prefix . 'queue';

// validations
// Validate the configuration
if (empty($this->config->template)) {
throw AlertsException::forInvalidTemplate('');
}

$locator = Services::locator();
if (! $locator->locateFile($this->config->template)) {
if (! service('locator')->locateFile($this->config->template)) {
throw AlertsException::forMissingTemplateView($this->config->template);
}
}

// add a new alert to the queue
/**
* Adds a new alert to the queue.
*
* @param string $class Class to apply, e.g. "info", "success"
* @param string $text Text of the alert
*
* @return void
*/
public function add($class, $text)
{
$alert = [
'class' => $class,
'text' => $text,
];

// start the queue if it doesn't exist
if (! $this->session->has($this->config->prefix . 'queue')) {
$this->session->set($this->config->prefix . 'queue', [$alert]);
// Start the queue if it doesn't exist
if (! $this->session->has($this->key)) {
$this->session->set($this->key, [$alert]);
}

// push onto the queue if it was already there
// Push onto the queue if it was already there
else {
$this->session->push($this->config->prefix . 'queue', [$alert]);
$this->session->push($this->key, [$alert]);
}
}

// clears the queue and returns template formatted alerts
/**
* Clears the queue and returns template formatted alerts.
*
* @return string
*/
public function display()
{
// get any alerts
$alerts = $this->session->get($this->config->prefix . 'queue') ?? [];

// clear alerts
$this->session->remove($this->config->prefix . 'queue');
// Retrieve and clear the queue
$alerts = $this->session->get($this->key) ?? [];
$this->session->remove($this->key);

// Check for flashdata (if configured)
if ($this->config->getflash) {
Expand All @@ -109,16 +118,21 @@ public function display()
}

if (empty($alerts)) {
return;
return '';
}
// render the specified view template

// Render the specified view template
return $this->view->setVar('prefix', $this->config->prefix)
->setVar('alerts', $alerts)
->render($this->config->template);
}

// returns default CSS as inline style sheet
// should be injected into <head>
/**
* Returns default CSS as inline style sheet to be
* included in the <head> tag.
*
* @return string
*/
public function css()
{
return $this->view->setVar('prefix', $this->config->prefix)->render('Tatter\Alerts\Views\css');
Expand Down
18 changes: 15 additions & 3 deletions src/Config/Alerts.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,24 @@

class Alerts extends BaseConfig
{
// prefix for SESSION variables and HTML classes, to prevent collision
/**
* Prefix for SESSION variables and HTML classes, to prevent collision.
*
* @var string
*/
public $prefix = 'alerts-';

// Template to use for HTML output
/**
* Template to use for HTML output.
*
* @var string
*/
public $template = 'Tatter\\Alerts\\Views\\bootstrap';

// Whether to check session flashdata for common alert keys
/**
* Whether to check session flashdata for common alert keys.
*
* @var bool
*/
public $getflash = true;
}
4 changes: 2 additions & 2 deletions src/Exceptions/AlertsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ class AlertsException extends FrameworkException implements ExceptionInterface
{
public static function forInvalidTemplate(string $template = null)
{
return new static(lang('Alerts.invalidTemplate', [$template]));
return new static(lang('Alerts.invalidTemplate', [$template ?? '']));
}

public static function forMissingTemplateView(string $template = null)
{
return new static(lang('Alerts.missingTemplateView', [$template]));
return new static(lang('Alerts.missingTemplateView', [$template ?? '']));
}
}
9 changes: 8 additions & 1 deletion src/Helpers/alerts_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@
*/

if (! function_exists('alert')) {
// add a new alert to the queue
/**
* Adds a new alert to the queue.
*
* @param string $class Class to apply, e.g. "info", "success"
* @param string $text Text of the alert
*
* @return void
*/
function alert(string $class, string $text)
{
$alerts = service('alerts');
Expand Down
25 changes: 25 additions & 0 deletions tests/_support/AlertsTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* This file is part of Tatter Alerts.
*
* (c) 2021 Tatter Software
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Tests\Support;

use CodeIgniter\Test\CIUnitTestCase;
use Config\Services;

abstract class AlertsTestCase extends CIUnitTestCase
{
protected function setUp(): void
{
Services::reset();

parent::setUp();
}
}
46 changes: 46 additions & 0 deletions tests/unit/LibraryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/**
* This file is part of Tatter Alerts.
*
* (c) 2021 Tatter Software
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

use Tatter\Alerts\Alerts;
use Tests\Support\AlertsTestCase;

/**
* @internal
*/
final class LibraryTest extends AlertsTestCase
{
public function testAdd()
{
$expected = [
[
'class' => 'fruit',
'text' => 'banana',
],
[
'class' => 'candy',
'text' => 'snickers',
],
];

service('alerts')->add('fruit', 'banana');
service('alerts')->add('candy', 'snickers');

$this->assertSame($expected, session('alerts-queue'));
}

public function testCss()
{
$expected = view('Tatter\Alerts\Views\css', ['prefix' => 'alerts-']);
$result = service('alerts')->css();

$this->assertSame($expected, $result);
}
}
4 changes: 2 additions & 2 deletions tests/unit/ServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
* the LICENSE file that was distributed with this source code.
*/

use CodeIgniter\Test\CIUnitTestCase;
use Tatter\Alerts\Alerts;
use Tests\Support\AlertsTestCase;

/**
* @internal
*/
final class ServiceTest extends CIUnitTestCase
final class ServiceTest extends AlertsTestCase
{
public function testServiceReturnsLibrary()
{
Expand Down

0 comments on commit f01d829

Please sign in to comment.