Skip to content

Commit

Permalink
Merge pull request #17 from SSEsmaeeli/fix/style-fix
Browse files Browse the repository at this point in the history
Refactor - Fix styling
  • Loading branch information
HPWebdeveloper authored Jan 14, 2024
2 parents beee8d7 + 1c5b4d0 commit 174ab31
Show file tree
Hide file tree
Showing 16 changed files with 86 additions and 88 deletions.
5 changes: 3 additions & 2 deletions config/pay-pocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
return [
'log_reference_length' => 12,
'log_reference_prefix' => null,
'log_reference_generator' => null,
'log_reference_prefix' => '',
'log_reference_generator_class' => Illuminate\Support\Str::class,
'log_reference_generator_method' => 'random',
];
9 changes: 2 additions & 7 deletions src/Exceptions/InsufficientBalanceException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
namespace HPWebdeveloper\LaravelPayPocket\Exceptions;

use Exception;
use Throwable;

class InsufficientBalanceException extends Exception
{
/**
* Construct the exception.
*
* @param string $message
* @param int $code
*/
public function __construct($message = 'Insufficient balance to cover the order', $code = 0, ?\Throwable $previous = null)
public function __construct(string $message = 'Insufficient balance to cover the order', int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
9 changes: 2 additions & 7 deletions src/Exceptions/InvalidDepositException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
namespace HPWebdeveloper\LaravelPayPocket\Exceptions;

use Exception;
use Throwable;

class InvalidDepositException extends Exception
{
/**
* Construct the exception.
*
* @param string $message
* @param int $code
*/
public function __construct($message = 'Invalid deposit operation', $code = 0, ?\Throwable $previous = null)
public function __construct(string $message = 'Invalid deposit operation', int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
9 changes: 2 additions & 7 deletions src/Exceptions/InvalidValueException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
namespace HPWebdeveloper\LaravelPayPocket\Exceptions;

use Exception;
use Throwable;

class InvalidValueException extends Exception
{
/**
* Construct the exception.
*
* @param string $message
* @param int $code
*/
public function __construct($message = 'Invalie value to deposit', $code = 0, ?\Throwable $previous = null)
public function __construct(string $message = 'Invalie value to deposit', int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Exceptions/InvalidWalletTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace HPWebdeveloper\LaravelPayPocket\Exceptions;

use Exception;
use Throwable;

class InvalidWalletTypeException extends Exception
{
public function __construct($message = 'Invalid wallet type', $code = 0, ?\Throwable $previous = null)
public function __construct(string $message = 'Invalid wallet type', int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Exceptions/WalletNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace HPWebdeveloper\LaravelPayPocket\Exceptions;

use Exception;
use Throwable;

class WalletNotFoundException extends Exception
{
public function __construct($message = 'Wallet not found', $code = 0, ?\Throwable $previous = null)
public function __construct(string $message = 'Wallet not found', int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
11 changes: 6 additions & 5 deletions src/Facades/LaravelPayPocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

namespace HPWebdeveloper\LaravelPayPocket\Facades;

use HPWebdeveloper\LaravelPayPocket\Interfaces\WalletOperations;
use Illuminate\Support\Facades\Facade;

/**
* @see \HPWebdeveloper\LaravelPayPocket\Services\PocketServices
*
* @method static void pay(\Illuminate\Database\Eloquent\Model $user, int|float $orderValue, ?string $notes = null)
* @method static bool deposit(\Illuminate\Database\Eloquent\Model $user, string $type, int|float $amount, ?string $notes = null)
* @method static int|float checkBalance(\Illuminate\Database\Eloquent\Model $user)
* @method static int|float walletBalanceByType(\Illuminate\Database\Eloquent\Model $user, string $type)
* @method static void pay(WalletOperations $user, int|float $orderValue, ?string $notes = null)
* @method static bool deposit(WalletOperations $user, string $type, int|float $amount, ?string $notes = null)
* @method static int|float checkBalance(WalletOperations $user)
* @method static int|float walletBalanceByType(WalletOperations $user, string $type)
*/
class LaravelPayPocket extends Facade
{
protected static function getFacadeAccessor()
protected static function getFacadeAccessor(): string
{
return \HPWebdeveloper\LaravelPayPocket\Services\PocketServices::class;
}
Expand Down
15 changes: 9 additions & 6 deletions src/Interfaces/WalletOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace HPWebdeveloper\LaravelPayPocket\Interfaces;

use HPWebdeveloper\LaravelPayPocket\Exceptions\InsufficientBalanceException;

interface WalletOperations
{
/**
Expand All @@ -12,7 +14,7 @@ public function getWalletBalanceAttribute(): int|float;
/**
* Get the balance of a specific wallet type.
*/
public function getWalletBalanceByType(string $walletType): float|int;
public function getWalletBalanceByType(string $walletType): int|float;

/**
* Check if User's wallet balance is more than given value
Expand All @@ -22,16 +24,17 @@ public function hasSufficientBalance(int|float $value): bool;
/**
* Pay the order value from the user's wallets.
*
* @param ?string $notes
*
* @throws \HPWebdeveloper\LaravelPayPocket\Exceptions\InsufficientBalanceException
* @throws InsufficientBalanceException
*/
public function pay(int|float $orderValue, ?string $notes = null): void;

/**
* Deposit an amount to the user's wallet of a specific type.
*
* @param ?string $notes
*/
public function deposit(string $type, int|float $amount, ?string $notes = null): bool;

/**
* Get user's wallet balance.
*/
public function getWalletBalance(): int|float;
}
8 changes: 8 additions & 0 deletions src/Models/WalletsLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
* HPWebdeveloper\LaravelPayPocket\Models\WalletsLog
*
* @property string $status
* @property int|float $from
* @property int|float $to
* @property string $type
* @property string $ip
* @property int|float $value
* @property string $wallet_name
* @property string $notes
* @property string $reference
*/
class WalletsLog extends Model
{
Expand Down
19 changes: 8 additions & 11 deletions src/Services/PocketServices.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,41 @@

namespace HPWebdeveloper\LaravelPayPocket\Services;

use Illuminate\Database\Eloquent\Model;
use HPWebdeveloper\LaravelPayPocket\Exceptions\InsufficientBalanceException;
use HPWebdeveloper\LaravelPayPocket\Interfaces\WalletOperations;

class PocketServices
{
/**
* Deposit an amount to the user's wallet of a specific type.
*
* @param ?string $notes
*/
public function deposit(Model $user, string $type, int|float $amount, ?string $notes = null): bool
public function deposit(WalletOperations $user, string $type, int|float $amount, ?string $notes = null): bool
{
return $user->deposit($type, $amount, $notes);
}

/**
* Pay the order value from the user's wallets.
*
* @param ?string $notes
*
* @throws \HPWebdeveloper\LaravelPayPocket\Exceptions\InsufficientBalanceException
* @throws InsufficientBalanceException
*/
public function pay(Model $user, int|float $orderValue, ?string $notes = null): void
public function pay(WalletOperations $user, int|float $orderValue, ?string $notes = null): void
{
$user->pay($orderValue, $notes);
}

/**
* Get the balance of the user.
*/
public function checkBalance(Model $user): int|float
public function checkBalance(WalletOperations $user): int|float
{
return $user->walletBalance;
return $user->getWalletBalance();
}

/**
* Get the balance of a specific wallet type.
*/
public function walletBalanceByType(Model $user, string $type): float|int
public function walletBalanceByType(WalletOperations $user, string $type): int|float
{
return $user->getWalletBalanceByType($type);
}
Expand Down
46 changes: 24 additions & 22 deletions src/Traits/BalanceOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace HPWebdeveloper\LaravelPayPocket\Traits;

use Illuminate\Support\Str;
use HPWebdeveloper\LaravelPayPocket\Models\WalletsLog;
use InvalidArgumentException;

trait BalanceOperation
{
protected $createdLog;
protected WalletsLog $createdLog;

/**
* Check if Balance is more than zero.
Expand All @@ -18,8 +19,6 @@ public function hasBalance(): bool

/**
* Decrement Balance and create a log entry.
*
* @param ?string $notes
*/
public function decrementAndCreateLog(int|float $value, ?string $notes = null): void
{
Expand All @@ -29,8 +28,6 @@ public function decrementAndCreateLog(int|float $value, ?string $notes = null):

/**
* Increment Balance and create a log entry.
*
* @param ?string $notes
*/
public function incrementAndCreateLog(int|float $value, ?string $notes = null): void
{
Expand All @@ -40,38 +37,43 @@ public function incrementAndCreateLog(int|float $value, ?string $notes = null):

/**
* Create a new log record
*
* @param ?string $notes
*/
protected function createLog(string $logType, int|float $value, ?string $notes = null): void
{
$currentBalance = $this->balance ?? 0;

$newBalance = $logType === 'dec' ? $currentBalance - $value : $currentBalance + $value;

$refGen = config('pay-pocket.log_reference_generator', [
Str::class, 'random', [config('pay-pocket.log_reference_length', 12)],
]);
$refGen = [
Str::class, 'random', [config('pay-pocket.log_reference_length', 12)],
];

$reference = config('pay-pocket.reference_string_prefix', '');
$reference .= isset($refGen[0], $refGen[1])
? $refGen[0]::{$refGen[1]}(...$refGen[2] ?? [])
: Str::random(config('pay-pocket.log_reference_length', 12));

$this->createdLog = $this->logs()->create([
'wallet_name' => $this->type->value,
'from' => $currentBalance,
'to' => $newBalance,
'type' => $logType,
'ip' => \Request::ip(),
'ip' => request()->ip(),
'value' => $value,
'notes' => $notes,
'reference' => $reference,
'reference' => $this->generateReference(),
]);

$this->createdLog->changeStatus('Done');
}

/**
* @throws InvalidArgumentException
*/
protected function generateReference(): string
{
$className = config('pay-pocket.log_reference_generator_class');
$methodName = config('pay-pocket.log_reference_generator_method');
$length = config('pay-pocket.log_reference_length');
$prefix = config('pay-pocket.log_reference_prefix');

if (! is_callable([$className, $methodName])) {
throw new InvalidArgumentException('Invalid configuration: The combination of log_reference_generator_class and log_reference_generator_method is not callable.');
}

$reference = call_user_func([$className, $methodName], $length);

return $prefix.$reference;
}
}
6 changes: 2 additions & 4 deletions src/Traits/GetWallets.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@

trait GetWallets
{
private function walletsInOrder()
private function walletsInOrder(): array
{
return array_map(
function ($enumCase) {
return $enumCase->value;
},
fn ($enumCase) => $enumCase->value,
WalletEnums::cases()
);
}
Expand Down
11 changes: 3 additions & 8 deletions src/Traits/HandlesDeposit.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@
use HPWebdeveloper\LaravelPayPocket\Exceptions\InvalidWalletTypeException;
use Illuminate\Support\Facades\DB;

// Use your defined exception

trait HandlesDeposit
{
/**
* Deposit an amount to the user's wallet of a specific type.
*
* @param ?string $notes
*
* @throws InvalidDepositException|InvalidValueException|InvalidWalletTypeException
* @throws InvalidDepositException
* @throws InvalidValueException
* @throws InvalidWalletTypeException
*/
public function deposit(string $type, int|float $amount, ?string $notes = null): bool
{
Expand Down Expand Up @@ -56,9 +54,6 @@ private function getDepositableTypes(): array
/**
* Check if the given type is valid.
*
* @var string
* @var array
*
* @throws InvalidWalletTypeException
*/
private function isRequestValid($type, array $depositable): bool
Expand Down
4 changes: 1 addition & 3 deletions src/Traits/HandlesPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ trait HandlesPayment
/**
* Pay the order value from the user's wallets.
*
* @param ?string $notes
*
* @throws \HPWebdeveloper\LaravelPayPocket\Exceptions\InsufficientBalanceException
* @throws InsufficientBalanceException
*/
public function pay(int|float $orderValue, ?string $notes = null): void
{
Expand Down
Loading

0 comments on commit 174ab31

Please sign in to comment.