Skip to content

Commit

Permalink
♻️refactor: remove populate on create & run composer lint
Browse files Browse the repository at this point in the history
  • Loading branch information
ScuffedNewt committed Feb 9, 2024
1 parent dddc7e6 commit 9aba841
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 26 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/Admin/Data/CurrencyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function getEditCurrency($id) {
}

return view('admin.currencies.create_edit_currency', [
'currency' => $currency,
'currency' => $currency,
'currencies' => Currency::where('id', '!=', $id)->get()->sortBy('name')->pluck('name', 'id'),
]);
}
Expand Down
8 changes: 5 additions & 3 deletions app/Http/Controllers/Users/BankController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,24 @@ public function postTransfer(Request $request, CurrencyManager $service) {
/**
* Gets the currency conversion form for the user.
*
* @param App\Services\CurrencyManager $service
* @param mixed $id
*
* @return \Illuminate\Http\RedirectResponse
*/
public function getConvertCurrency($id) {
$currency = Currency::where('is_user_owned', 1)->where('id', $id)->first();
$convertOptions = Currency::whereIn('id', $currency->conversions->pluck('conversion_id')->toArray())->orderBy('sort_user', 'DESC')->pluck('name', 'id')->toArray();

return view('home._bank_convert', [
'convertOptions' => $convertOptions,
'convertOptions' => $convertOptions,
]);
}

/**
* Gets the currency conversion rate for the user.
*
* @param App\Services\CurrencyManager $service
* @param mixed $currency_id
* @param mixed $conversion_id
*
* @return \Illuminate\Http\RedirectResponse
*/
Expand Down
1 change: 0 additions & 1 deletion app/Models/Currency/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Models\Currency;

use App\Models\Model;
use App\Models\Currency\CurrencyConversion;

class Currency extends Model {
/**
Expand Down
10 changes: 7 additions & 3 deletions app/Models/Currency/CurrencyConversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
namespace App\Models\Currency;

use App\Models\Model;
use App\Models\Currency\Currency;

class CurrencyConversion extends Model {

/**
* The attributes that are mass assignable.
*
Expand Down Expand Up @@ -72,6 +70,8 @@ public function convert() {

/**
* Gets the ratio based on the decimal conversion rate.
*
* @param mixed $return
*/
public function ratio($return = false) {
$numerator = $this->rate * 100; // Convert rate to avoid floating point issues
Expand All @@ -85,11 +85,15 @@ public function ratio($return = false) {
if ($return) {
return [$numerator, $denominator];
}
return $numerator . ":" . $denominator;

return $numerator.':'.$denominator;
}

/**
* Gets the greatest common divisor of two numbers.
*
* @param mixed $a
* @param mixed $b
*/
private function gcd($a, $b) {
return $b ? $this->gcd($b, $a % $b) : $a;
Expand Down
3 changes: 1 addition & 2 deletions app/Services/CurrencyManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ public function debitCurrency($sender, $recipient, $type, $data, $currency, $qua
* @param Currency $conversion
* @param int $quantity
* @param User $user
*
*
* @return bool
*/
public function convertCurrency($currency, $conversion, $quantity, $user) {
Expand Down Expand Up @@ -428,7 +428,6 @@ public function convertCurrency($currency, $conversion, $quantity, $user) {
return $this->rollbackReturn(false);
}


/**
* Creates a currency log.
*
Expand Down
6 changes: 2 additions & 4 deletions app/Services/CurrencyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ public function createCurrency($data, $user) {

$currency = Currency::create($data);

$this->populateConversions($currency, $data);

if (!$this->logAdminAction($user, 'Created Currency', 'Created '.$currency->displayName)) {
throw new \Exception('Failed to log admin action.');
}
Expand Down Expand Up @@ -282,14 +280,14 @@ private function populateData($data, $currency = null) {

/**
* Processes user input for creating/updating a currency's conversions.
*
*
* @param Currency $currency
* @param array $data
*/
private function populateConversions($currency, $data) {
$currency->conversions()->delete();
if (isset($data['conversion_id']) && $data['conversion_id']) {
foreach($data['conversion_id'] as $key => $conversion_id) {
foreach ($data['conversion_id'] as $key => $conversion_id) {
$conversion = Currency::find($conversion_id);
if (!$conversion) {
continue;
Expand Down
10 changes: 5 additions & 5 deletions app/Services/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ abstract class Service {
/**
* Errors.
*
* @var Illuminate\Support\MessageBag
* @var App\Services\Illuminate\Support\MessageBag
*/
protected $errors = null;
protected $cache = [];
Expand Down Expand Up @@ -61,7 +61,7 @@ public function hasError($key) {
/**
* Return errors.
*
* @return Illuminate\Support\MessageBag
* @return App\Services\Illuminate\Support\MessageBag
*/
public function errors() {
return $this->errors;
Expand All @@ -81,7 +81,7 @@ public function getAllErrors() {
*
* @param mixed $key
*
* @return Illuminate\Support\MessageBag
* @return App\Services\Illuminate\Support\MessageBag
*/
public function getError($key) {
return $this->errors->get($key);
Expand Down Expand Up @@ -200,7 +200,7 @@ public function logAdminAction($user, $action, $action_details) {
// Check that the currency exists, first
$currency = Currency::find(config('lorekeeper.extensions.staff_rewards.currency_id'));
if ($currency) {
if (!(new CurrencyManager)->creditCurrency(null, $user, 'Staff Reward', $action_details, $currency, $reward)) {
if (!(new App\Services\CurrencyManager)->creditCurrency(null, $user, 'Staff Reward', $action_details, $currency, $reward)) {
return false;
}
}
Expand Down Expand Up @@ -250,7 +250,7 @@ protected function setError($key, $value) {
/**
* Add multiple errors to the message bag.
*
* @param Illuminate\Support\MessageBag $errors
* @param App\Services\Illuminate\Support\MessageBag $errors
*/
protected function setErrors($errors) {
$this->errors->merge($errors);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCurrencyConversionTables extends Migration
{
class CreateCurrencyConversionTables extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
public function up(): void {
Schema::create('currency_conversions', function (Blueprint $table) {
$table->integer('currency_id')->unsigned();
$table->integer('conversion_id')->unsigned();
Expand All @@ -21,8 +19,7 @@ public function up(): void
/**
* Reverse the migrations.
*/
public function down(): void
{
public function down(): void {
Schema::dropIfExists('currency_conversions');
}
};
}

0 comments on commit 9aba841

Please sign in to comment.