1
0
mirror of https://github.com/flarum/core.git synced 2025-08-06 08:27:42 +02:00

refactoring of the extenders

This commit is contained in:
Daniël Klabbers
2020-04-01 17:02:14 +02:00
parent 23736fcfda
commit ae55cd3d20
5 changed files with 46 additions and 51 deletions

View 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;
}
}

View File

@@ -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;
}
}

View File

@@ -10,19 +10,19 @@
namespace Flarum\Extend;
use Flarum\Extension\Extension;
use Flarum\User\User;
use Flarum\User\User as Eloquent;
use Illuminate\Contracts\Container\Container;
class UserPreferences implements ExtenderInterface
class User implements ExtenderInterface
{
public function extend(Container $container, Extension $extension = null)
{
// 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;
}

View File

@@ -9,9 +9,10 @@
namespace Flarum\Tests\integration\extenders;
use Flarum\Extend\NotificationChannel;
use Flarum\Extend\Notification;
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
use Flarum\Tests\integration\TestCase;
use Flarum\User\NotificationPreference;
use Flarum\User\User;
class NotificationChannelTest extends TestCase
@@ -31,17 +32,26 @@ class NotificationChannelTest extends TestCase
private function add_channel()
{
$this->extend(new NotificationChannel('test'));
$this->extend((new Notification)->addChannel('test'));
}
/**
* @test
*/
public function can_add_notification_channel()
public function can_enable_notification_channel()
{
$this->add_channel();
/** @var User $user */
$user = User::find(2);
NotificationPreference::setNotificationPreference($user, 'test', 'newPost');
$this->assertTrue(
$user->notificationPreferences()
->where('channel', 'test')
->where('type', 'newPost')
->get('enabled')
);
}
}

View File

@@ -9,7 +9,7 @@
namespace Flarum\Tests\integration\extenders;
use Flarum\Extend\UserPreferences;
use Flarum\Extend\User as Extender;
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
use Flarum\Tests\integration\TestCase;
use Flarum\User\User;
@@ -33,8 +33,8 @@ class UserPreferencesTest extends TestCase
private function add_preference()
{
$this->extend(
(new UserPreferences())
->add('test', 'boolval', false)
(new Extender())
->addPreference('test', 'boolval', false)
);
}