Skip to content

Commit

Permalink
Refactor code (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
datlechin authored Jun 22, 2023
1 parent 1ccb8c1 commit e3867ad
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 59 deletions.
1 change: 0 additions & 1 deletion extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Datlechin\Birthdays;

use Datlechin\Birthdays\AddBirthdayValidation;
use Datlechin\Birthdays\Access\UserPolicy;
use Datlechin\Birthdays\Filter\BirthdayFilter;
use Datlechin\Birthdays\Listeners\AddUserBirthdayAttribute;
Expand Down
4 changes: 1 addition & 3 deletions js/src/forum/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ app.initializers.add('datlechin/flarum-birthdays', () => {
});

extend(EditUserModal.prototype, 'data', function (data) {
if (this.birthday() !== this.attrs.user.birthday()) {
data.birthday = this.birthday();
}
data.birthday = this.birthday();
});

extend(SignUpModal.prototype, 'oninit', function () {
Expand Down
20 changes: 8 additions & 12 deletions src/Access/UserPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,18 @@

class UserPolicy extends AbstractPolicy
{
/**
* @var SettingsRepositoryInterface
*/
protected $settings;

public function __construct(SettingsRepositoryInterface $settings)
public function __construct(protected SettingsRepositoryInterface $settings)
{
$this->settings = $settings;
}

public function viewBirthday(User $actor, User $user)
public function viewBirthday(User $actor, User $user): string
{
if (!$actor->hasPermission('user.editOwnBirthday') && $this->isSuspended($user)) {
if (! $actor->hasPermission('user.editOwnBirthday') && $this->isSuspended($user)) {
return $this->deny();
}

if (($actor->id === $user->id && $actor->hasPermission('user.editOwnBirthday'))
if (
($actor->id === $user->id && $actor->hasPermission('user.editOwnBirthday'))
|| $actor->hasPermission('user.viewBirthday')
) {
return $this->allow();
Expand All @@ -34,7 +29,7 @@ public function viewBirthday(User $actor, User $user)
return $this->deny();
}

public function editBirthday(User $actor, User $user)
public function editBirthday(User $actor, User $user): string
{
if ($actor->isGuest() && !$user->exists || $this->settings->get('datlechin-birthdays.set_on_registration')) {
return $this->allow();
Expand All @@ -43,9 +38,10 @@ public function editBirthday(User $actor, User $user)
} else if ($actor->can('edit', $user)) {
return $this->allow();
}

return $this->deny();
}

// source https://github.com/FriendsOfFlarum/user-bio/blob/master/src/Access/UserPolicy.php
protected function isSuspended(User $user): bool
{
return $user->suspended_until !== null
Expand Down
37 changes: 15 additions & 22 deletions src/AddBirthdayValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,37 @@

namespace Datlechin\Birthdays;

use DateTime;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\UserValidator;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Validator;
use Symfony\Contracts\Translation\TranslatorInterface;

class AddBirthdayValidation
{
/**
* @var TranslatorInterface
*/
protected $translator;

/**
* @var SettingsRepositoryInterface
*/
protected $settings;


public function __construct(TranslatorInterface $translator, SettingsRepositoryInterface $settings)
{
$this->translator = $translator;
$this->settings = $settings;
public function __construct(
protected TranslatorInterface $translator,
protected SettingsRepositoryInterface $settings
) {
}

/**
* @param Validator $validator
*/
public function __invoke($flarumValidator, Validator $validator)
public function __invoke(UserValidator $flarumValidator, Validator $validator): void
{
$rules = $validator->getRules();

$isRequired = $this->settings->get('datlechin-birthdays.required')
&& $this->settings->get('datlechin-birthdays.set_on_registration');

$rules['birthday'] = [
(bool) $this->settings->get('datlechin-birthdays.required') && (bool) $this->settings->get('datlechin-birthdays.set_on_registration') ? 'required' : 'nullable',
'nullable',
Rule::requiredIf($isRequired && ! $flarumValidator->getUser()),
'date_format:Y-m-d',
'before:today',
function ($attribute, $value, $fail) {
if ($value) {
$birthday = new \DateTime($value);
$now = new \DateTime();
$birthday = new DateTime($value);
$now = new DateTime();
$diff = $now->diff($birthday);
if ($diff->y <= (int) $this->settings->get('datlechin-birthdays.min_age')) {
$fail($this->translator->trans('datlechin-birthdays.api.invalid_age_message', [
Expand Down
12 changes: 1 addition & 11 deletions src/Filter/BirthdayFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,12 @@

class BirthdayFilter implements FilterInterface
{

/**
* @return string
*/
public function getFilterKey(): string
{
return 'birthday';
}

/**
* @param FilterState $filterState
* @param string $filterValue
* @param bool $negate
* @return void
*/
public function filter(FilterState $filterState, string $filterValue, bool $negate)
public function filter(FilterState $filterState, string $filterValue, bool $negate): void
{
$birthday = date('m-d', strtotime($filterValue));

Expand Down
5 changes: 3 additions & 2 deletions src/Listeners/AddUserBirthdayAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ public function __invoke(UserSerializer $serializer, User $user, array $attribut

if ($actor->can('viewBirthday', $user)) {
$attributes['birthday'] = $user->birthday;
$attributes['showDobDate'] = $user->showDobDate ? true : false;
$attributes['showDobYear'] = $user->showDobYear ? true : false;
$attributes['showDobDate'] = (bool) $user->showDobDate;
$attributes['showDobYear'] = (bool) $user->showDobYear;
$attributes['canEditOwnBirthday'] = $actor->id === $user->id && $actor->can('editOwnBirthday', $user);
}

return $attributes;
}
}
11 changes: 3 additions & 8 deletions src/Listeners/SaveBirthdayToDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@
class SaveBirthdayToDatabase
{

/**
* Handle the event.
*
* @param Saving $event
*/
public function handle(Saving $event)
public function handle(Saving $event): void
{
$user = $event->user;
$data = $event->data;
Expand All @@ -31,8 +26,8 @@ public function handle(Saving $event)

$actor->assertCan('editBirthday', $user);

if (isset($attributes['showDobDate'])) $user->showDobDate = $attributes['showDobDate'];
if (isset($attributes['showDobYear'])) $user->showDobYear = $attributes['showDobYear'];
$user->showDobDate = $attributes['showDobDate'] ?? false;
$user->showDobYear = $attributes['showDobYear'] ?? false;

if (isset($attributes['birthday'])) {
$user->birthday = $attributes['birthday'] === '' ? null : $attributes['birthday'];
Expand Down

0 comments on commit e3867ad

Please sign in to comment.