Skip to content

Commit

Permalink
fix: move return field key logic to trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Dartui committed Jul 24, 2024
1 parent 906c124 commit 17fd6db
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 57 deletions.
22 changes: 0 additions & 22 deletions src/Repository/Recipient/BaseRecipient.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,6 @@ abstract class BaseRecipient implements Interfaces\Receivable
use Traits\HasSlug;
use Casegnostic;

/**
* List of available return fields.
*/
protected const AVAILABLE_RETURN_FIELDS = ['ID', 'user_email'];

/**
* Return field key name.
*
* @var string
*/
protected $returnField = 'user_email';

/**
* Recipient input default value
*
Expand All @@ -59,16 +47,6 @@ public function __construct($params = [])
$this->setName($params['name']);
}

if (is_string($params['return_field'] ?? null)) {
$returnField = $params['return_field'];

if (!in_array($returnField, self::AVAILABLE_RETURN_FIELDS, true)) {
trigger_error(sprintf('Recipient return field "%s" is not supported.', $returnField), E_USER_ERROR);
}

$this->returnField = $returnField;
}

if (!isset($params['default_value'])) {
trigger_error('Recipient requires default_value', E_USER_ERROR);
}
Expand Down
32 changes: 16 additions & 16 deletions src/Repository/Recipient/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@

namespace BracketSpace\Notification\Repository\Recipient;

use BracketSpace\Notification\Repository\Field;
use BracketSpace\Notification\Database\Queries\UserQueries;
use BracketSpace\Notification\Repository\Field;
use BracketSpace\Notification\Traits;

/**
* Role recipient
*/
class Role extends BaseRecipient
{
use Traits\HasReturnField;

/**
* Recipient constructor
*
Expand All @@ -26,15 +29,18 @@ class Role extends BaseRecipient
*/
public function __construct($params = [])
{
$this->setReturnField(
is_string($params['return_field'] ?? null)
? $params['return_field']
: $this->getDefaultReturnField()
);

parent::__construct(
array_merge(
$params,
[
'slug' => 'role',
'name' => __('Role', 'notification'),
'default_value' => 'administrator',
]
)
[
'slug' => 'role',
'name' => __('Role', 'notification'),
'default_value' => 'administrator',
]
);
}

Expand All @@ -50,13 +56,7 @@ public function parseValue($value = '')
$value = $this->getDefaultValue();
}

$values = [];

foreach (UserQueries::withRole($value) as $user) {
$values[] = $user[$this->returnField];
}

return $values;
return wp_list_pluck(UserQueries::withRole($value), $this->getReturnField());
}

/**
Expand Down
24 changes: 15 additions & 9 deletions src/Repository/Recipient/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@

use BracketSpace\Notification\Repository\Field;
use BracketSpace\Notification\Database\Queries\UserQueries;
use BracketSpace\Notification\Traits;

/**
* User recipient
*/
class User extends BaseRecipient
{
use Traits\HasReturnField;

/**
* Recipient constructor
*
Expand All @@ -26,15 +29,18 @@ class User extends BaseRecipient
*/
public function __construct($params = [])
{
$this->setReturnField(
is_string($params['return_field'] ?? null)
? $params['return_field']
: $this->getDefaultReturnField()
);

parent::__construct(
array_merge(
$params,
[
'slug' => 'user',
'name' => __('User', 'notification'),
'default_value' => get_current_user_id(),
]
)
[
'slug' => 'user',
'name' => __('User', 'notification'),
'default_value' => get_current_user_id(),
]
);
}

Expand All @@ -53,7 +59,7 @@ public function parseValue($value = '')
$user = get_userdata((int)$value);

if ($user) {
return [$user->{$this->returnField}];
return [$user->{$this->getReturnField()}];
}

return [];
Expand Down
26 changes: 16 additions & 10 deletions src/Repository/Recipient/UserID.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
namespace BracketSpace\Notification\Repository\Recipient;

use BracketSpace\Notification\Repository\Field;
use BracketSpace\Notification\Traits;

/**
* User ID recipient
*/
class UserID extends BaseRecipient
{
use Traits\HasReturnField;

/**
* Recipient constructor
*
Expand All @@ -25,15 +28,18 @@ class UserID extends BaseRecipient
*/
public function __construct($params = [])
{
$this->setReturnField(
is_string($params['return_field'] ?? null)
? $params['return_field']
: $this->getDefaultReturnField()
);

parent::__construct(
array_merge(
$params,
[
'slug' => 'user_id',
'name' => __('User ID', 'notification'),
'default_value' => '',
]
)
[
'slug' => 'user_id',
'name' => __('User ID', 'notification'),
'default_value' => '',
]
);
}

Expand All @@ -57,11 +63,11 @@ public function parseValue($value = '')
$users = get_users(
[
'include' => $userIds,
'fields' => [$this->returnField],
'fields' => [$this->getReturnField()],
]
);

return wp_list_pluck($users, $this->returnField);
return wp_list_pluck($users, $this->getReturnField());
}

/**
Expand Down
63 changes: 63 additions & 0 deletions src/Traits/HasReturnField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* Has Name Trait.
*
* @package notification
*/

declare(strict_types=1);

namespace BracketSpace\Notification\Traits;

/**
* HasName trait
*/
trait HasReturnField
{
/**
* Return field name.
*
* @var ?string
*/
protected $returnField;

/**
* Gets the default return field name.
*
* @return string
*/
public function getDefaultReturnField()
{
return 'user_email';
}

/**
* Gets the return field name.
*
* @return string
*/
public function getReturnField()
{
return $this->returnField ?? $this->getDefaultReturnField();
}

/**
* Sets return field name.
*
* @param string $returnField Return field name.
* @return $this
*/
public function setReturnField(string $returnField)
{
$availableReturnFields = ['ID', 'user_email'];

if (!in_array($returnField, $availableReturnFields, true)) {
trigger_error(sprintf('Recipient return field "%s" is not supported.', $returnField), E_USER_ERROR);
}

$this->returnField = $returnField;

return $this;
}
}

0 comments on commit 17fd6db

Please sign in to comment.