1
0
mirror of https://github.com/Kovah/LinkAce.git synced 2025-01-18 05:38:40 +01:00

Replace current alert provider with laracasts/flash package

This commit is contained in:
Kovah 2020-04-28 10:15:32 +02:00
parent 0093662eaf
commit e44f10f861
No known key found for this signature in database
GPG Key ID: AAAA031BA9830D7B
19 changed files with 95 additions and 279 deletions

View File

@ -1,117 +0,0 @@
<?php
/*
* This file is part of Laravel Alert.
*
* (c) Vincent Klaiber <hello@vinkla.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Helper;
use Illuminate\Session\Store;
/**
* This is the alert class.
*
* @author Vincent Klaiber <hello@vinkla.com>
*/
class Alert
{
/**
* The session storage instance.
*
* @var Store
*/
protected $session;
/**
* Create a new alert instance.
*
* @param Store $session
*
* @return void
*/
public function __construct(Store $session)
{
$this->session = $session;
}
/**
* Flash an alert.
*
* @param string $message
* @param string $style
*
* @return Alert
*/
public function flash(string $message, string $style = 'info'): self
{
$this->session->flash('alert.message', $message);
$this->session->flash('alert.style', $style);
return $this;
}
/**
* Flash a danger alert.
*
* @param string $message
*
* @return Alert
*/
public function danger(string $message): self
{
return $this->flash($message, 'danger');
}
/**
* Flash an error alert.
*
* @param string $message
*
* @return Alert
*/
public function error(string $message): self
{
return $this->danger($message);
}
/**
* Flash an info alert.
*
* @param string $message
*
* @return Alert
*/
public function info(string $message): self
{
return $this->flash($message, 'info');
}
/**
* Flash a success alert.
*
* @param string $message
*
* @return Alert
*/
public function success(string $message): self
{
return $this->flash($message, 'success');
}
/**
* Flash a warning alert.
*
* @param string $message
*
* @return Alert
*/
public function warning(string $message): self
{
return $this->flash($message, 'warning');
}
}

View File

@ -31,12 +31,12 @@ class LinkAce
try {
$response = Http::get($url);
} catch (ConnectionException $e) {
alert(trans('link.added_connection_error'), 'warning');
flash(trans('link.added_connection_error'), 'warning');
Log::warning($url . ': ' . $e->getMessage());
return $fallback;
} catch (RequestException $e) {
alert(trans('link.added_request_error'), 'warning');
flash(trans('link.added_request_error'), 'warning');
Log::warning($url . ': ' . $e->getMessage());
return $fallback;

View File

@ -195,25 +195,6 @@ function waybackLink($link): ?string
return WaybackMachine::getArchiveLink($link);
}
/**
* Flash an alert.
*
* @param string|null $message
* @param string|null $style
*
* @return Alert
*/
function alert(?string $message = null, ?string $style = 'info'): Alert
{
$alert = app('alert');
if ($message === null) {
return $alert;
}
return $alert->flash($message, $style);
}
/**
* Return proper link attributes based on the links_new_tab user setting
*

View File

@ -46,7 +46,7 @@ class ImportController extends Controller
$links = $parser->parseString($data);
if (empty($links)) {
alert(trans('import.import_empty'), 'warning');
flash(trans('import.import_empty'), 'warning');
return redirect()->back();
}
@ -92,7 +92,7 @@ class ImportController extends Controller
$imported++;
}
alert(trans('import.import_successfully', [
flash(trans('import.import_successfully', [
'imported' => $imported,
'skipped' => $skipped,
]), 'success');

View File

@ -48,7 +48,7 @@ class SystemSettingsController extends Controller
]);
}
alert(trans('settings.settings_saved'));
flash(trans('settings.settings_saved'));
return redirect()->route('get-sysstemsettings');
}

View File

@ -83,7 +83,7 @@ class TrashController extends Controller
}
if ($entries->isEmpty()) {
alert(trans('trash.delete_no_entries'), 'warning');
flash(trans('trash.delete_no_entries'), 'warning');
return redirect()->back();
}
@ -91,7 +91,7 @@ class TrashController extends Controller
$entry->forceDelete();
}
alert(trans('trash.delete_success.' . $model), 'success');
flash(trans('trash.delete_success.' . $model), 'success');
return redirect()->route('get-trash');
}
@ -133,7 +133,7 @@ class TrashController extends Controller
$entry->restore();
alert(trans('trash.restore.' . $model), 'success');
flash(trans('trash.restore.' . $model), 'success');
return redirect()->route('get-trash');
}

View File

@ -51,7 +51,7 @@ class UserSettingsController extends Controller
'email',
]));
alert(trans('settings.settings_saved'), 'success');
flash(trans('settings.settings_saved'), 'success');
return redirect()->back();
}
@ -89,7 +89,7 @@ class UserSettingsController extends Controller
]);
}
alert(trans('settings.settings_saved'), 'success');
flash(trans('settings.settings_saved'), 'success');
return redirect()->back();
}
@ -107,14 +107,14 @@ class UserSettingsController extends Controller
]);
if (!$authorizationSuccessful) {
alert(trans('settings.old_password_invalid'));
flash(trans('settings.old_password_invalid'));
return redirect()->back()->withInput();
}
$currentUser->password = Hash::make($request->input('new_password'));
$currentUser->save();
alert(trans('settings.password_updated'), 'success');
flash(trans('settings.password_updated'), 'success');
return redirect()->back();
}

View File

@ -68,7 +68,7 @@ class LinkController extends Controller
{
$link = LinkRepository::create($request->all());
alert(trans('link.added_successfully'), 'success');
flash(trans('link.added_successfully'), 'success');
$isBookmarklet = session('bookmarklet.create');
@ -130,7 +130,7 @@ class LinkController extends Controller
$link = LinkRepository::update($link, $request->all());
alert(trans('link.updated_successfully'), 'success');
flash(trans('link.updated_successfully'), 'success');
return redirect()->route('links.show', [$link->id]);
}
@ -150,11 +150,11 @@ class LinkController extends Controller
$deletionSuccessfull = LinkRepository::delete($link);
if (!$deletionSuccessfull) {
alert(trans('link.deletion_error'), 'error');
flash(trans('link.deletion_error'), 'error');
return redirect()->back();
}
alert(trans('link.deleted_successfully'), 'warning');
flash(trans('link.deleted_successfully'), 'warning');
return redirect()->route('links.index');
}

View File

@ -62,7 +62,7 @@ class ListController extends Controller
$list = ListRepository::create($data);
alert(trans('list.added_successfully'), 'success');
flash(trans('list.added_successfully'), 'success');
if ($request->get('reload_view')) {
session()->flash('reload_view', true);
@ -128,7 +128,7 @@ class ListController extends Controller
$list = ListRepository::update($list, $request->all());
alert(trans('list.updated_successfully'), 'success');
flash(trans('list.updated_successfully'), 'success');
return redirect()->route('lists.show', [$list->id]);
}
@ -148,11 +148,11 @@ class ListController extends Controller
$deletionSuccessfull = ListRepository::delete($list);
if (!$deletionSuccessfull) {
alert(trans('list.deletion_error'), 'error');
flash(trans('list.deletion_error'), 'error');
return redirect()->back();
}
alert(trans('list.deleted_successfully'), 'warning');
flash(trans('list.deleted_successfully'), 'warning');
return redirect()->route('lists.index');
}
}

View File

@ -38,7 +38,7 @@ class NoteController extends Controller
$data = $request->except(['_token']);
NoteRepository::create($data);
alert(trans('note.added_successfully'), 'success');
flash(trans('note.added_successfully'), 'success');
return redirect()->route('links.show', [$link->id]);
}
@ -74,7 +74,7 @@ class NoteController extends Controller
$data = $request->except(['_token', 'note_id']);
$note = NoteRepository::update($note, $data);
alert(trans('note.updated_successfully'), 'success');
flash(trans('note.updated_successfully'), 'success');
return redirect()->route('links.show', [$note->link->id]);
}
@ -96,11 +96,11 @@ class NoteController extends Controller
$deletionSuccessfull = NoteRepository::delete($note);
if (!$deletionSuccessfull) {
alert(trans('note.deletion_error'), 'error');
flash(trans('note.deletion_error'), 'error');
return redirect()->back();
}
alert(trans('note.deleted_successfully'), 'warning');
flash(trans('note.deleted_successfully'), 'warning');
return redirect()->route('links.show', [$linkId]);
}

View File

@ -69,7 +69,7 @@ class TagController extends Controller
$tag = TagRepository::create($data);
alert(trans('tag.added_successfully'), 'success');
flash(trans('tag.added_successfully'), 'success');
if ($request->get('reload_view')) {
session()->flash('reload_view', true);
@ -136,7 +136,7 @@ class TagController extends Controller
$data = $request->all();
$tag = TagRepository::update($tag, $data);
alert(trans('tag.updated_successfully'), 'success');
flash(trans('tag.updated_successfully'), 'success');
return redirect()->route('tags.show', [$tag->id]);
}
@ -156,11 +156,11 @@ class TagController extends Controller
$deletionSuccessfull = TagRepository::delete($tag);
if (!$deletionSuccessfull) {
alert(trans('tag.deletion_error'), 'error');
flash(trans('tag.deletion_error'), 'error');
return redirect()->back();
}
alert(trans('tag.deleted_successfully'), 'warning');
flash(trans('tag.deleted_successfully'), 'warning');
return redirect()->route('tags.index');
}
}

View File

@ -42,7 +42,7 @@ class DatabaseController extends Controller
$this->createTempDatabaseConnection($request->all());
if ($this->databaseHasData() && !$request->has('overwrite_data')) {
alert(trans('setup.database.data_present'), 'danger');
flash(trans('setup.database.data_present'), 'danger');
return redirect()->back()->with('data_present', true)->withInput();
}
@ -91,7 +91,7 @@ class DatabaseController extends Controller
]);
} catch (Exception $e) {
$alert = trans('setup.database.config_error') . ' ' . $e->getMessage();
alert($alert, 'danger');
flash($alert, 'danger');
return false;
}

View File

@ -9,6 +9,7 @@
"doctrine/dbal": "^2.8",
"fideloper/proxy": "^4.0",
"guzzlehttp/guzzle": "^6.3",
"laracasts/flash": "^3.1",
"laravel/framework": "^7.0",
"laravel/ui": "^2.0",
"predis/predis": "^1.1",

58
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "c592b455e5f6fe455948538fe1ba1bc5",
"content-hash": "70d6d370a7861265d9d5655be2a59f81",
"packages": [
{
"name": "brick/math",
@ -896,6 +896,60 @@
],
"time": "2016-11-07T19:29:14+00:00"
},
{
"name": "laracasts/flash",
"version": "3.1",
"source": {
"type": "git",
"url": "https://github.com/laracasts/flash.git",
"reference": "150d4348477db31b9a93ccd07f713e3d0513b3bf"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/laracasts/flash/zipball/150d4348477db31b9a93ccd07f713e3d0513b3bf",
"reference": "150d4348477db31b9a93ccd07f713e3d0513b3bf",
"shasum": ""
},
"require": {
"illuminate/support": "~5.0|^6.0|^7.0",
"php": ">=5.4.0"
},
"require-dev": {
"mockery/mockery": "dev-master",
"phpunit/phpunit": "^6.1"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"Laracasts\\Flash\\FlashServiceProvider"
],
"aliases": {
"Flash": "Laracasts\\Flash\\Flash"
}
}
},
"autoload": {
"psr-0": {
"Laracasts\\Flash": "src/"
},
"files": [
"src/Laracasts/Flash/functions.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Jeffrey Way",
"email": "jeffrey@laracasts.com"
}
],
"description": "Easy flash notifications",
"time": "2020-03-03T15:50:52+00:00"
},
{
"name": "laravel/framework",
"version": "v7.4.0",
@ -4654,6 +4708,7 @@
"email": "jakub.onderka@gmail.com"
}
],
"abandoned": "php-parallel-lint/php-console-color",
"time": "2018-09-29T17:23:10+00:00"
},
{
@ -4700,6 +4755,7 @@
}
],
"description": "Highlight PHP code in terminal",
"abandoned": "php-parallel-lint/php-console-highlighter",
"time": "2018-09-29T18:48:56+00:00"
},
{

View File

@ -1,8 +1,4 @@
@if (session()->has('alert.message'))
<div class="alert alert-{{ session()->get('alert.style') }}" role="alert">
{!! session()->get('alert.message') !!}
</div><!-- /alert -->
@endif
@include('flash::message')
@if ($errors->any())
<div class="alert alert-danger">

View File

@ -88,7 +88,9 @@ class TrashControllerTest extends TestCase
$response = $this->get('trash/clear/links');
$response->assertStatus(302);
$this->assertEquals('No entries to be deleted.', session()->get('alert.message'));
$flashMessage = session('flash_notification', collect())->first();
$this->assertEquals('No entries to be deleted.', $flashMessage['message']);
}
/*

View File

@ -1,6 +1,6 @@
<?php
namespace Tests\Feature;
namespace Tests\Feature\App;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;
@ -85,7 +85,8 @@ class UserSettingsControllerTest extends TestCase
$response->assertStatus(302);
$this->assertEquals('Password changed successfully!', session()->get('alert.message'));
$flashMessage = session('flash_notification', collect())->first();
$this->assertEquals('Password changed successfully!', $flashMessage['message']);
$this->assertEquals(true, Auth::attempt([
'email' => $this->user->email,

View File

@ -1,84 +0,0 @@
<?php
/*
* This file is part of Laravel Alert.
*
* (c) Vincent Klaiber <hello@vinkla.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Tests\Unit\Helper;
use App\Helper\Alert;
use Tests\TestCase;
/**
* This is the alert test class.
*
* @author Vincent Klaiber <hello@vinkla.com>
*/
class AlertTest extends TestCase
{
public function testFlash()
{
$alert = $this->getAlert();
$alert->flash('bart');
$this->assertFlash('bart', 'info');
}
public function testError()
{
$alert = $this->getAlert();
$alert->error('maggie');
$this->assertFlash('maggie', 'danger');
}
public function testDanger()
{
$alert = $this->getAlert();
$alert->danger('homer');
$this->assertFlash('homer', 'danger');
}
public function testInfo()
{
$alert = $this->getAlert();
$alert->info('lisa');
$this->assertFlash('lisa', 'info');
}
public function testSuccess()
{
$alert = $this->getAlert();
$alert->success('marge');
$this->assertFlash('marge', 'success');
}
public function testWarning()
{
$alert = $this->getAlert();
$alert->warning('bob');
$this->assertFlash('bob', 'warning');
}
public function getAlert()
{
return new Alert($this->app['session.store']);
}
protected function assertFlash($message, $style)
{
$this->assertSame($message, $this->app->session->get('alert.message'));
$this->assertSame($style, $this->app->session->get('alert.style'));
}
}

View File

@ -61,24 +61,4 @@ class HelperFunctionsTest extends TestCase
$this->assertNull($link);
}
/**
* Test if the alert helper function is working correctly
*
* @return void
*/
public function testAlert()
{
alert('flanders');
$this->assertFlash('flanders', 'info');
alert()->warning('burns');
$this->assertFlash('burns', 'warning');
}
protected function assertFlash($message, $style)
{
$this->assertSame($message, $this->app->session->get('alert.message'));
$this->assertSame($style, $this->app->session->get('alert.style'));
}
}