Add filter can_rename_player
This commit is contained in:
parent
cdb7456ab2
commit
df3c1687ad
@ -4,10 +4,12 @@ namespace App\Http\Controllers;
|
||||
|
||||
use View;
|
||||
use Event;
|
||||
use Eventy;
|
||||
use Option;
|
||||
use App\Models\User;
|
||||
use App\Models\Player;
|
||||
use App\Models\Texture;
|
||||
use App\Services\Rejection;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Events\PlayerWasAdded;
|
||||
use App\Events\PlayerWasDeleted;
|
||||
@ -126,6 +128,11 @@ class PlayerController extends Controller
|
||||
|
||||
$dispatcher->dispatch('player.renaming', [$player, $newName]);
|
||||
|
||||
$can = Eventy::filter('can_rename_player', $player, $newName);
|
||||
if ($can instanceof Rejection) {
|
||||
return json($can->getReason(), 1);
|
||||
}
|
||||
|
||||
if (! Player::where('name', $newName)->get()->isEmpty()) {
|
||||
return json(trans('user.player.rename.repeated'), 6);
|
||||
}
|
||||
|
34
app/Services/Rejection.php
Normal file
34
app/Services/Rejection.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class Rejection
|
||||
{
|
||||
/** @var string */
|
||||
protected $reason;
|
||||
|
||||
/** @var mixed */
|
||||
protected $data;
|
||||
|
||||
public function __construct(string $reason, $data = [])
|
||||
{
|
||||
$this->reason = $reason;
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
public function getReason(): string
|
||||
{
|
||||
return $this->reason;
|
||||
}
|
||||
|
||||
public function getData($key = null, $default = null)
|
||||
{
|
||||
if (is_null($key)) {
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
return Arr::get($this->data, $key, $default);
|
||||
}
|
||||
}
|
@ -73,13 +73,6 @@ if (! function_exists('add_filter')) {
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('apply_filters')) {
|
||||
function apply_filters()
|
||||
{
|
||||
return call_user_func_array([app('eventy'), 'filter'], func_get_args());
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('bs_footer_extra')) {
|
||||
function bs_footer_extra(): string
|
||||
{
|
||||
|
@ -3,10 +3,12 @@
|
||||
namespace Tests;
|
||||
|
||||
use Event;
|
||||
use Eventy;
|
||||
use App\Events;
|
||||
use App\Models\User;
|
||||
use App\Models\Player;
|
||||
use App\Models\Texture;
|
||||
use App\Services\Rejection;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class PlayerControllerTest extends TestCase
|
||||
@ -187,9 +189,21 @@ class PlayerControllerTest extends TestCase
|
||||
]);
|
||||
Event::assertDispatched('player.renaming');
|
||||
|
||||
// Rejected by filter
|
||||
$pid = $player->pid;
|
||||
Eventy::addFilter('can_rename_player', function ($player) {
|
||||
return new Rejection('rejected');
|
||||
}, 20, 1);
|
||||
$name = factory(Player::class)->create()->name;
|
||||
$this->postJson('/user/player/rename/'.$player->pid, ['name' => 'new'])
|
||||
->assertJson([
|
||||
'code' => 1,
|
||||
'message' => 'rejected',
|
||||
]);
|
||||
Eventy::removeAllFilters('can_rename_player');
|
||||
|
||||
// Success
|
||||
Event::fake();
|
||||
$pid = $player->pid;
|
||||
$this->postJson('/user/player/rename/'.$pid, ['name' => 'new_name'])
|
||||
->assertJson([
|
||||
'code' => 0,
|
||||
|
@ -2,6 +2,8 @@
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use Eventy;
|
||||
|
||||
class FilterTest extends TestCase
|
||||
{
|
||||
public function testAddFilter()
|
||||
@ -21,19 +23,11 @@ class FilterTest extends TestCase
|
||||
});
|
||||
}
|
||||
|
||||
public function testApplyFilters()
|
||||
{
|
||||
$this->mock('eventy', function ($mock) {
|
||||
$mock->shouldReceive('filter')->withArgs(['my.hook', 'value'])->once();
|
||||
});
|
||||
apply_filters('my.hook', 'value');
|
||||
}
|
||||
|
||||
public function testIntegration()
|
||||
{
|
||||
add_filter('hook.test', function ($value) {
|
||||
return $value.'ed';
|
||||
});
|
||||
$this->assertEquals('tested', apply_filters('hook.test', 'test'));
|
||||
$this->assertEquals('tested', Eventy::filter('hook.test', 'test'));
|
||||
}
|
||||
}
|
||||
|
27
tests/ServicesTest/RejectionTest.php
Normal file
27
tests/ServicesTest/RejectionTest.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use App\Services\Rejection;
|
||||
|
||||
class RejectionTest extends TestCase
|
||||
{
|
||||
public function testGetReason()
|
||||
{
|
||||
$reason = 'rejected';
|
||||
$rejection = new Rejection($reason);
|
||||
$this->assertEquals($reason, $rejection->getReason());
|
||||
}
|
||||
|
||||
public function testGetData()
|
||||
{
|
||||
$rejection = new Rejection('', 'data');
|
||||
$this->assertEquals('data', $rejection->getData());
|
||||
|
||||
$rejection = new Rejection('', ['a' => 'b']);
|
||||
$this->assertEquals('b', $rejection->getData('a'));
|
||||
$this->assertNull($rejection->getData('nope'));
|
||||
$this->assertEquals('default', $rejection->getData('nope', 'default'));
|
||||
$this->assertEquals(['a' => 'b'], $rejection->getData());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user