mirror of
https://github.com/flarum/core.git
synced 2025-08-08 01:16:52 +02:00
refactoring of the extenders
This commit is contained in:
26
src/Extend/Notification.php
Normal file
26
src/Extend/Notification.php
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Flarum\Extend;
|
||||||
|
|
||||||
|
use Flarum\Extension\Extension;
|
||||||
|
use Flarum\User\NotificationPreference;
|
||||||
|
use Illuminate\Contracts\Container\Container;
|
||||||
|
|
||||||
|
class Notification implements ExtenderInterface
|
||||||
|
{
|
||||||
|
private $channels = [];
|
||||||
|
|
||||||
|
public function extend(Container $container, Extension $extension = null)
|
||||||
|
{
|
||||||
|
foreach ($this->channels as $channel => $enabled) {
|
||||||
|
NotificationPreference::addChannel($channel, $enabled ?? []);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addChannel(string $channel, array $enabledTypes = null)
|
||||||
|
{
|
||||||
|
$this->channels[$channel] = $enabledTypes;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
@@ -1,41 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
/*
|
|
||||||
* This file is part of Flarum.
|
|
||||||
*
|
|
||||||
* For detailed copyright and license information, please view the
|
|
||||||
* LICENSE file that was distributed with this source code.
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace Flarum\Extend;
|
|
||||||
|
|
||||||
use Flarum\Extension\Extension;
|
|
||||||
use Flarum\User\NotificationPreference;
|
|
||||||
use Illuminate\Contracts\Container\Container;
|
|
||||||
|
|
||||||
class NotificationChannel implements ExtenderInterface
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private $channel;
|
|
||||||
|
|
||||||
private $enabled = [];
|
|
||||||
|
|
||||||
public function __construct(string $channel)
|
|
||||||
{
|
|
||||||
$this->channel = $channel;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function extend(Container $container, Extension $extension = null)
|
|
||||||
{
|
|
||||||
NotificationPreference::addChannel($this->channel, $this->enabled);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function enabled(string $type, bool $enabled = true)
|
|
||||||
{
|
|
||||||
$this->enabled[$type] = $enabled;
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
}
|
|
@@ -10,19 +10,19 @@
|
|||||||
namespace Flarum\Extend;
|
namespace Flarum\Extend;
|
||||||
|
|
||||||
use Flarum\Extension\Extension;
|
use Flarum\Extension\Extension;
|
||||||
use Flarum\User\User;
|
use Flarum\User\User as Eloquent;
|
||||||
use Illuminate\Contracts\Container\Container;
|
use Illuminate\Contracts\Container\Container;
|
||||||
|
|
||||||
class UserPreferences implements ExtenderInterface
|
class User implements ExtenderInterface
|
||||||
{
|
{
|
||||||
public function extend(Container $container, Extension $extension = null)
|
public function extend(Container $container, Extension $extension = null)
|
||||||
{
|
{
|
||||||
// There's nothing here as the logic is contained in the `add()` method directly.
|
// There's nothing here as the logic is contained in the `add()` method directly.
|
||||||
}
|
}
|
||||||
|
|
||||||
public function add(string $key, callable $transformer = null, $default = null)
|
public function addPreference(string $key, callable $transformer = null, $default = null)
|
||||||
{
|
{
|
||||||
User::addPreference($key, $transformer, $default);
|
Eloquent::addPreference($key, $transformer, $default);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
@@ -9,9 +9,10 @@
|
|||||||
|
|
||||||
namespace Flarum\Tests\integration\extenders;
|
namespace Flarum\Tests\integration\extenders;
|
||||||
|
|
||||||
use Flarum\Extend\NotificationChannel;
|
use Flarum\Extend\Notification;
|
||||||
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
|
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
|
||||||
use Flarum\Tests\integration\TestCase;
|
use Flarum\Tests\integration\TestCase;
|
||||||
|
use Flarum\User\NotificationPreference;
|
||||||
use Flarum\User\User;
|
use Flarum\User\User;
|
||||||
|
|
||||||
class NotificationChannelTest extends TestCase
|
class NotificationChannelTest extends TestCase
|
||||||
@@ -31,17 +32,26 @@ class NotificationChannelTest extends TestCase
|
|||||||
|
|
||||||
private function add_channel()
|
private function add_channel()
|
||||||
{
|
{
|
||||||
$this->extend(new NotificationChannel('test'));
|
$this->extend((new Notification)->addChannel('test'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @test
|
* @test
|
||||||
*/
|
*/
|
||||||
public function can_add_notification_channel()
|
public function can_enable_notification_channel()
|
||||||
{
|
{
|
||||||
$this->add_channel();
|
$this->add_channel();
|
||||||
|
|
||||||
/** @var User $user */
|
/** @var User $user */
|
||||||
$user = User::find(2);
|
$user = User::find(2);
|
||||||
|
|
||||||
|
NotificationPreference::setNotificationPreference($user, 'test', 'newPost');
|
||||||
|
|
||||||
|
$this->assertTrue(
|
||||||
|
$user->notificationPreferences()
|
||||||
|
->where('channel', 'test')
|
||||||
|
->where('type', 'newPost')
|
||||||
|
->get('enabled')
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
namespace Flarum\Tests\integration\extenders;
|
namespace Flarum\Tests\integration\extenders;
|
||||||
|
|
||||||
use Flarum\Extend\UserPreferences;
|
use Flarum\Extend\User as Extender;
|
||||||
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
|
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
|
||||||
use Flarum\Tests\integration\TestCase;
|
use Flarum\Tests\integration\TestCase;
|
||||||
use Flarum\User\User;
|
use Flarum\User\User;
|
||||||
@@ -33,8 +33,8 @@ class UserPreferencesTest extends TestCase
|
|||||||
private function add_preference()
|
private function add_preference()
|
||||||
{
|
{
|
||||||
$this->extend(
|
$this->extend(
|
||||||
(new UserPreferences())
|
(new Extender())
|
||||||
->add('test', 'boolval', false)
|
->addPreference('test', 'boolval', false)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user