Skip to content

Commit

Permalink
Use Eloquent Attributes instead of magic methods
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkomarev committed Apr 10, 2023
1 parent 6d767e4 commit 96a0653
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 102 deletions.
7 changes: 1 addition & 6 deletions src/Reactant/Models/Reactant.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,7 @@ final class Reactant extends Model implements

protected $table = 'love_reactants';

/**
* @var string[]
*/
protected $fillable = [
'type',
];
protected static $unguarded = true;

/**
* @var string[]
Expand Down
46 changes: 18 additions & 28 deletions src/Reactant/ReactionCounter/Models/ReactionCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Cog\Laravel\Love\Reactant\Models\Reactant;
use Cog\Laravel\Love\ReactionType\Models\ReactionType;
use Cog\Laravel\Love\Support\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

Expand All @@ -33,30 +34,31 @@ final class ReactionCounter extends Model implements

protected $table = 'love_reactant_reaction_counters';

protected static $unguarded = true;

/**
* @var array<int|float>
* @var array<string, int|float>
*/
protected $attributes = [
'count' => self::COUNT_DEFAULT,
'weight' => self::WEIGHT_DEFAULT,
];

/**
* @var string[]
*/
protected $fillable = [
'reaction_type_id',
'count',
'weight',
];
public function count(): Attribute
{
return new Attribute(
get: fn (int | null $value) => $value ?? self::COUNT_DEFAULT,
set: fn (int | null $value) => $value ?? self::COUNT_DEFAULT,
);
}

/**
* @var string[]
*/
protected $casts = [
'count' => 'integer',
'weight' => 'float',
];
public function weight(): Attribute
{
return new Attribute(
get: fn (float | null $value) => $value ?? self::WEIGHT_DEFAULT,
set: fn (float | null $value) => $value ?? self::WEIGHT_DEFAULT,
);
}

public function reactant(): BelongsTo
{
Expand Down Expand Up @@ -123,16 +125,4 @@ public function decrementWeight(
): void {
$this->decrement('weight', $amount);
}

public function setCountAttribute(
int | null $count,
): void {
$this->attributes['count'] = $count ?? self::COUNT_DEFAULT;
}

public function setWeightAttribute(
float | null $weight,
): void {
$this->attributes['weight'] = $weight ?? self::WEIGHT_DEFAULT;
}
}
45 changes: 18 additions & 27 deletions src/Reactant/ReactionTotal/Models/ReactionTotal.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Cog\Contracts\Love\Reactant\ReactionTotal\Models\ReactionTotal as ReactionTotalInterface;
use Cog\Laravel\Love\Reactant\Models\Reactant;
use Cog\Laravel\Love\Support\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

Expand All @@ -31,29 +32,31 @@ final class ReactionTotal extends Model implements

protected $table = 'love_reactant_reaction_totals';

protected static $unguarded = true;

/**
* @var array<int|float>
* @var array<string, int|float>
*/
protected $attributes = [
'count' => self::COUNT_DEFAULT,
'weight' => self::WEIGHT_DEFAULT,
];

/**
* @var string[]
*/
protected $fillable = [
'count',
'weight',
];
public function count(): Attribute
{
return new Attribute(
get: fn (int | null $value) => $value ?? self::COUNT_DEFAULT,
set: fn (int | null $value) => $value ?? self::COUNT_DEFAULT,
);
}

/**
* @var string[]
*/
protected $casts = [
'count' => 'integer',
'weight' => 'float',
];
public function weight(): Attribute
{
return new Attribute(
get: fn (float | null $value) => $value ?? self::WEIGHT_DEFAULT,
set: fn (float | null $value) => $value ?? self::WEIGHT_DEFAULT,
);
}

public function reactant(): BelongsTo
{
Expand Down Expand Up @@ -98,16 +101,4 @@ public function decrementWeight(
): void {
$this->decrement('weight', $amount);
}

public function setCountAttribute(
int | null $count,
): void {
$this->attributes['count'] = $count ?? self::COUNT_DEFAULT;
}

public function setWeightAttribute(
float | null $weight,
): void {
$this->attributes['weight'] = $weight ?? self::WEIGHT_DEFAULT;
}
}
31 changes: 31 additions & 0 deletions src/Reactant/ReactionTotal/Observers/ReactionTotalObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of Laravel Love.
*
* (c) Anton Komarev <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Cog\Laravel\Love\Reactant\ReactionTotal\Observers;

use Cog\Laravel\Love\Reactant\ReactionTotal\Models\ReactionTotal;

final class ReactionTotalObserver
{
public function creating(
ReactionTotal $total,
): void {
if ($total->getAttributeValue('count') === null) {
$total->setAttribute('count', ReactionTotal::COUNT_DEFAULT);
}

if ($total->getAttributeValue('weight') === null) {
$total->setAttribute('weight', ReactionTotal::WEIGHT_DEFAULT);
}
}
}
7 changes: 1 addition & 6 deletions src/Reacter/Models/Reacter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@ final class Reacter extends Model implements

protected $table = 'love_reacters';

/**
* @var string[]
*/
protected $fillable = [
'type',
];
protected static $unguarded = true;

/**
* @var string[]
Expand Down
54 changes: 28 additions & 26 deletions src/Reaction/Models/Reaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Cog\Laravel\Love\Reacter\Models\Reacter;
use Cog\Laravel\Love\ReactionType\Models\ReactionType;
use Cog\Laravel\Love\Support\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

Expand All @@ -37,29 +38,40 @@ final class Reaction extends Model implements

protected $table = 'love_reactions';

protected static $unguarded = true;

/**
* @var float[]
* @var array<string, float>
*/
protected $attributes = [
'rate' => self::RATE_DEFAULT,
];

/**
* @var string[]
*/
protected $fillable = [
'reactant_id',
'reaction_type_id',
'rate',
];
public function id(): Attribute
{
return new Attribute(
get: fn (string | null $value) => $value,
set: fn (string | null $value) => $value,
);
}

/**
* @var string[]
*/
protected $casts = [
'id' => 'string',
'rate' => 'float',
];
public function rate(): Attribute
{
return new Attribute(
get: fn (float | null $value) => $value ?? self::RATE_DEFAULT,
set: function (float | null $value) {
if ($value !== null && ($value < self::RATE_MIN || $value > self::RATE_MAX)) {
throw RateOutOfRange::withValueBetween(
$value,
self::RATE_MIN,
self::RATE_MAX,
);
}

return $value ?? self::RATE_DEFAULT;
},
);
}

public function reactant(): BelongsTo
{
Expand Down Expand Up @@ -106,16 +118,6 @@ public function getWeight(): float
return $this->getType()->getMass() * $this->getRate();
}

public function setRateAttribute(
float | null $rate,
): void {
if ($rate !== null && ($rate < self::RATE_MIN || $rate > self::RATE_MAX)) {
throw RateOutOfRange::withValueBetween($rate, self::RATE_MIN, self::RATE_MAX);
}

$this->attributes['rate'] = $rate ?? self::RATE_DEFAULT;
}

public function isOfType(
ReactionTypeInterface $reactionType,
): bool {
Expand Down
10 changes: 2 additions & 8 deletions src/ReactionType/Models/ReactionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,15 @@ final class ReactionType extends Model implements

protected $table = 'love_reaction_types';

protected static $unguarded = true;

/**
* @var int[]
*/
protected $attributes = [
'mass' => self::MASS_DEFAULT,
];

/**
* @var string[]
*/
protected $fillable = [
'name',
'mass',
];

/**
* @var string[]
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Reaction/Models/ReactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function it_throws_rate_out_of_range_on_fill_rate_with_overflow_value():
/** @test */
public function it_casts_id_to_string(): void
{
$reaction = Reaction::factory()->make([
$reaction = new Reaction([
'id' => 4,
]);

Expand Down

0 comments on commit 96a0653

Please sign in to comment.