From ae55cd3d20ad77bb29e26abf55a4ae3accdd4d8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20Klabbers?= Date: Wed, 1 Apr 2020 17:02:14 +0200 Subject: [PATCH] refactoring of the extenders --- src/Extend/Notification.php | 26 ++++++++++++ src/Extend/NotificationChannel.php | 41 ------------------- src/Extend/{UserPreferences.php => User.php} | 8 ++-- .../extenders/NotificationChannelTest.php | 16 ++++++-- .../extenders/UserPreferencesTest.php | 6 +-- 5 files changed, 46 insertions(+), 51 deletions(-) create mode 100644 src/Extend/Notification.php delete mode 100644 src/Extend/NotificationChannel.php rename src/Extend/{UserPreferences.php => User.php} (68%) diff --git a/src/Extend/Notification.php b/src/Extend/Notification.php new file mode 100644 index 000000000..141f9aeb0 --- /dev/null +++ b/src/Extend/Notification.php @@ -0,0 +1,26 @@ +channels as $channel => $enabled) { + NotificationPreference::addChannel($channel, $enabled ?? []); + } + } + + public function addChannel(string $channel, array $enabledTypes = null) + { + $this->channels[$channel] = $enabledTypes; + + return $this; + } +} diff --git a/src/Extend/NotificationChannel.php b/src/Extend/NotificationChannel.php deleted file mode 100644 index 39aca61e0..000000000 --- a/src/Extend/NotificationChannel.php +++ /dev/null @@ -1,41 +0,0 @@ -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; - } -} diff --git a/src/Extend/UserPreferences.php b/src/Extend/User.php similarity index 68% rename from src/Extend/UserPreferences.php rename to src/Extend/User.php index c2efda952..827b3e7fd 100644 --- a/src/Extend/UserPreferences.php +++ b/src/Extend/User.php @@ -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; } diff --git a/tests/integration/extenders/NotificationChannelTest.php b/tests/integration/extenders/NotificationChannelTest.php index 9a4581bdc..5ea90008c 100644 --- a/tests/integration/extenders/NotificationChannelTest.php +++ b/tests/integration/extenders/NotificationChannelTest.php @@ -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') + ); } } diff --git a/tests/integration/extenders/UserPreferencesTest.php b/tests/integration/extenders/UserPreferencesTest.php index da181ff23..82fa4b576 100644 --- a/tests/integration/extenders/UserPreferencesTest.php +++ b/tests/integration/extenders/UserPreferencesTest.php @@ -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) ); }