Skip to content

Commit

Permalink
Switch Rule to ValidationRule.
Browse files Browse the repository at this point in the history
  • Loading branch information
v16Studios committed Sep 28, 2023
1 parent f67b61f commit ca24b91
Show file tree
Hide file tree
Showing 19 changed files with 220 additions and 265 deletions.
22 changes: 10 additions & 12 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 Closure;
use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule;
use Enjin\Platform\Beam\Services\BeamService;
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,22 @@ public function __construct(protected bool $singleUse = false)
*
* @param string $attribute
* @param mixed $value
* @param Closure $fail
*/
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'));
}
}
}
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 $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'));
}
}
}
44 changes: 14 additions & 30 deletions src/Rules/IsEndDateValid.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,41 @@
namespace Enjin\Platform\Beam\Rules;

use Carbon\Carbon;
use Closure;
use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule;
use Enjin\Platform\Beam\Services\BeamService;
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 $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'));

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', ['value' => $startDate->toDateTimeString()]));
}
}

return true;
}

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

use Carbon\Carbon;
use Closure;
use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule;
use Enjin\Platform\Beam\Services\BeamService;
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 $fail
*
* @return bool
*/
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'));

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'));

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', ['value' => $endDate->toDateTimeString()]));
}
}

return true;
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return $this->message;
}
}
32 changes: 13 additions & 19 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 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 $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,11 @@ 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', ['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

0 comments on commit ca24b91

Please sign in to comment.