Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PLA-1304] Switch Rule to ValidationRule. #39

Merged
merged 4 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/Rules/CanClaim.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace Enjin\Platform\Beam\Rules;

use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule;
use Closure;
use Enjin\Platform\Beam\Services\BeamService;
use Enjin\Platform\Rules\Traits\HasDataAwareRule;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;

class CanClaim implements DataAwareRule, Rule
class CanClaim implements DataAwareRule, ValidationRule
{
use HasDataAwareRule;

Expand All @@ -25,25 +26,24 @@ public function __construct(protected bool $singleUse = false)
*
* @param string $attribute
* @param mixed $value
* @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*
* @return void
*/
public function passes($attribute, $value): bool
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (!Arr::get($this->data, 'account')) {
return true;
return;
}

if ($this->singleUse) {
$value = explode(':', decrypt($value), 3)[1];
}

return ((int) Cache::get(BeamService::key($value), BeamService::claimsCountResolver($value))) > 0;
}
$passes = ((int) Cache::get(BeamService::key($value), BeamService::claimsCountResolver($value))) > 0;

/**
* Get the validation error message.
*/
public function message(): string
{
return __('enjin-platform-beam::validation.can_claim');
if (!$passes) {
$fail('enjin-platform-beam::validation.can_claim')->translate();
}
}
}
29 changes: 14 additions & 15 deletions src/Rules/HasBeamFlag.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace Enjin\Platform\Beam\Rules;

use Closure;
use Enjin\Platform\Beam\Enums\BeamFlag;
use Enjin\Platform\Beam\Models\Beam;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Contracts\Validation\ValidationRule;

class HasBeamFlag implements Rule
class HasBeamFlag implements ValidationRule
{
/**
* Create new rule instance.
Expand All @@ -17,21 +18,19 @@ public function __construct(protected BeamFlag $flag)

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*
* @return void
*/
public function passes($attribute, $value)
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (!$beam = Beam::whereCode($value)->first()) {
return false;
}

return $beam->hasFlag($this->flag);
}
$beam = Beam::whereCode($value)->first();

/**
* Get the validation error message.
*/
public function message()
{
return __('enjin-platform-beam::validation.has_beam_flag');
if (!$beam || !$beam->hasFlag($this->flag)) {
$fail('enjin-platform-beam::validation.has_beam_flag')->translate();
}
}
}
49 changes: 18 additions & 31 deletions src/Rules/IsEndDateValid.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,44 @@
namespace Enjin\Platform\Beam\Rules;

use Carbon\Carbon;
use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule;
use Closure;
use Enjin\Platform\Beam\Services\BeamService;
use Enjin\Platform\Rules\Traits\HasDataAwareRule;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Arr;

class IsEndDateValid implements DataAwareRule, Rule
class IsEndDateValid implements DataAwareRule, ValidationRule
{
use HasDataAwareRule;

/**
* The error message.
*/
protected string $message;

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*
* @return bool
* @return void
*/
public function passes($attribute, $value)
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$date = Carbon::parse($value);
if ($start = Arr::get($this->data, 'start')) {
if ($date->lte(Carbon::parse($start))) {
$this->message = __('enjin-platform-beam::validation.end_date_after_start');
$start = Arr::get($this->data, 'start');
if ($start && $date->lte(Carbon::parse($start))) {
$fail(__('enjin-platform-beam::validation.end_date_after_start'));
v16Studios marked this conversation as resolved.
Show resolved Hide resolved

return false;
}
} elseif ($beam = resolve(BeamService::class)->findByCode($this->data['code'])) {
return;
}

if ($beam = resolve(BeamService::class)->findByCode($this->data['code'])) {
$startDate = Carbon::parse($beam->start);
if ($date->lte($startDate)) {
$this->message = __('enjin-platform-beam::validation.end_date_greater_than', ['value' => $startDate->toDateTimeString()]);

return false;
$fail('enjin-platform-beam::validation.end_date_greater_than')
->translate([
'value' => $startDate->toDateTimeString(),
]);
}
}

return true;
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return $this->message;
}
}
44 changes: 15 additions & 29 deletions src/Rules/IsStartDateValid.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,49 @@
namespace Enjin\Platform\Beam\Rules;

use Carbon\Carbon;
use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule;
use Closure;
use Enjin\Platform\Beam\Services\BeamService;
use Enjin\Platform\Rules\Traits\HasDataAwareRule;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Arr;

class IsStartDateValid implements DataAwareRule, Rule
class IsStartDateValid implements DataAwareRule, ValidationRule
{
use HasDataAwareRule;

/**
* The error message.
*/
protected string $message;

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*
* @return bool
* @return void
*/
public function passes($attribute, $value)
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if ($end = Arr::get($this->data, 'end')) {
if (Carbon::parse($value)->gte(Carbon::parse($end))) {
$this->message = __('enjin-platform-beam::validation.start_date_after_end');
$fail(__('enjin-platform-beam::validation.start_date_after_end'));
v16Studios marked this conversation as resolved.
Show resolved Hide resolved

return false;
return;
}
}

if ($beam = resolve(BeamService::class)->findByCode($this->data['code'])) {
if (Carbon::parse($beam->start)->isPast()) {
$this->message = __('enjin-platform-beam::validation.start_date_has_passed');
$fail(__('enjin-platform-beam::validation.start_date_has_passed'));
v16Studios marked this conversation as resolved.
Show resolved Hide resolved

return false;
return;
}
$endDate = Carbon::parse($beam->end);
if (Carbon::parse($value)->gte($endDate)) {
$this->message = __('enjin-platform-beam::validation.start_date_less_than', ['value' => $endDate->toDateTimeString()]);

return false;
$fail('enjin-platform-beam::validation.start_date_less_than')
->translate([
'value' => $endDate->toDateTimeString(),
]);
}
}

return true;
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return $this->message;
}
}
37 changes: 17 additions & 20 deletions src/Rules/MaxTokenCount.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

namespace Enjin\Platform\Beam\Rules;

use Closure;
use Enjin\Platform\Beam\Enums\BeamType;
use Enjin\Platform\Beam\Models\BeamClaim;
use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule;
use Enjin\Platform\Beam\Rules\Traits\IntegerRange;
use Enjin\Platform\Models\Collection;
use Enjin\Platform\Rules\Traits\HasDataAwareRule;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Arr;

class MaxTokenCount implements DataAwareRule, Rule
class MaxTokenCount implements DataAwareRule, ValidationRule
{
use HasDataAwareRule;
use IntegerRange;
Expand All @@ -32,14 +33,15 @@ public function __construct(protected ?string $collectionId)
*
* @param string $attribute
* @param mixed $value
* @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*
* @return bool
* @return void
*/
public function passes($attribute, $value)
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if ($this->collectionId && ($collection = Collection::withCount('tokens')->firstWhere(['collection_chain_id'=> $this->collectionId]))) {
if (!is_null($this->limit = $collection->max_token_count)) {
return $collection->max_token_count >= $collection->tokens_count
$passes = $collection->max_token_count >= $collection->tokens_count
+ collect($this->data['tokens'])
->filter(fn ($token) => BeamType::getEnumCase($token['type']) == BeamType::MINT_ON_DEMAND)
->reduce(function ($carry, $token) {
Expand All @@ -48,8 +50,8 @@ public function passes($attribute, $value)

return $val + (
$range === false
? $token['claimQuantity']
: (($range[1] - $range[0]) + 1) * $token['claimQuantity']
? $token['claimQuantity']
: (($range[1] - $range[0]) + 1) * $token['claimQuantity']
);
}, $carry);
}, 0)
Expand All @@ -58,19 +60,14 @@ public function passes($attribute, $value)
fn ($query) => $query->where('collection_chain_id', $this->collectionId)->where('end', '>', now())
)->where('type', BeamType::MINT_ON_DEMAND->name)
->count();

if (!$passes) {
$fail('enjin-platform-beam::validation.max_token_count')
->translate([
'limit' => $this->limit,
]);
}
}
}

return true;
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return __('enjin-platform-beam::validation.max_token_count', ['limit' => $this->limit]);
}
}
Loading
Loading