From 0772c5d4783b773a422b28384e6e369c8b947436 Mon Sep 17 00:00:00 2001 From: omair saleh Date: Tue, 1 Dec 2020 12:57:13 +0800 Subject: [PATCH] changes made to make the code follow our project coding standers to maintain consistency --- .../ControllersLogic/CreateWalletLogic.php | 49 +++++++++------ .../DataTransferObjects/WalletObject.php | 59 ++++++++----------- .../Services/ChecksIfWalletCodeExists.php | 27 +++++++++ .../Wallets/Services/CreatesWallet.php | 11 ++-- .../Wallets/Services/GeneratesWalletCode.php | 32 ++++++++++ .../Rules/CanCreateCompanyWallet.php | 54 +++++++++++++++++ .../Validators/CompanyWalletValidation.php | 41 +++++++++++++ app/Models/{Wallets.php => Wallet.php} | 2 +- ...9_212614_create_companies_wallet_table.php | 8 ++- routes/wallet.php | 7 +-- 10 files changed, 221 insertions(+), 69 deletions(-) create mode 100644 app/Classes/Modules/Wallets/Services/ChecksIfWalletCodeExists.php create mode 100644 app/Classes/Modules/Wallets/Services/GeneratesWalletCode.php create mode 100644 app/Classes/Modules/Wallets/Standards/Rules/CanCreateCompanyWallet.php create mode 100644 app/Classes/Modules/Wallets/Standards/Validators/CompanyWalletValidation.php rename app/Models/{Wallets.php => Wallet.php} (86%) diff --git a/app/Classes/Modules/Wallets/ControllersLogic/CreateWalletLogic.php b/app/Classes/Modules/Wallets/ControllersLogic/CreateWalletLogic.php index 197b5273..bc0803d8 100644 --- a/app/Classes/Modules/Wallets/ControllersLogic/CreateWalletLogic.php +++ b/app/Classes/Modules/Wallets/ControllersLogic/CreateWalletLogic.php @@ -4,8 +4,10 @@ namespace App\Classes\Modules\Wallets\ControllersLogic; use App\Classes\General\Abstracts\AbstractControllerLogic; +use App\Classes\Modules\SegmentCompanies\Standards\Rules\CanCreateCompanyWallet; use App\Classes\Modules\Wallets\DataTransferObjects\WalletObject; use App\Classes\Modules\Wallets\Services\CreatesWallet; +use App\Classes\Modules\Wallets\Services\GeneratesWalletCode; use App\Http\Resources\WalletResource; /* use App\Classes\Modules\Accounts\Standards\Rules\CanCreateUser; @@ -36,6 +38,8 @@ use Illuminate\Support\Facades\DB; class CreateWalletLogic extends AbstractControllerLogic { + + /** * @return array */ @@ -46,14 +50,26 @@ class CreateWalletLogic extends AbstractControllerLogic ]; } + /** @var CreatesWallet */ private $createsWallet; - - public function __construct( - CreatesWallet $createsWallet - ) + /** @var GeneratesWalletCode */ + private $generatesWalletCode; + + /** @var CanCreateCompanyWallet */ + private $canCreateCompanyWallet; + + /** + * CreateWalletLogic constructor. + * @param CreatesWallet $createsWallet + * @param GeneratesWalletCode $generatesWalletCode + * @param CanCreateCompanyWallet $canCreateCompanyWallet + */ + public function __construct(CreatesWallet $createsWallet, GeneratesWalletCode $generatesWalletCode, CanCreateCompanyWallet $canCreateCompanyWallet) { - $this->createsWallet= $createsWallet; + $this->createsWallet = $createsWallet; + $this->generatesWalletCode = $generatesWalletCode; + $this->canCreateCompanyWallet = $canCreateCompanyWallet; } /** @@ -64,26 +80,21 @@ class CreateWalletLogic extends AbstractControllerLogic public function logic(Request $request) : JsonResponse { try { - DB::beginTransaction(); - - echo $request->input('amount'); - echo $request->input('company_id'); - $amount = doubleval($request->input('amount')); - $wallet_object = new WalletObject( - $request->input('code'), - $request->input('currency'), - $request->input('amount'), - $request->input('company_id') - ); - $wallet_query = $this->createsWallet->execute($wallet_object); + DB::beginTransaction(); + + + $object = new WalletObject($request->input('currency'), $request->input('company_id'), $this->generatesWalletCode->execute()); + + $this->canCreateCompanyWallet->passes($object); + + $wallet = $this->createsWallet->execute($object); DB::commit(); - return $this->resourceResponse(new WalletResource($wallet_query)); + return $this->resourceResponse(new WalletResource($wallet)); } catch (\Exception $exception) { - dd($exception); throw new ErrorException($exception->getMessage(), $exception->getCode()); } diff --git a/app/Classes/Modules/Wallets/DataTransferObjects/WalletObject.php b/app/Classes/Modules/Wallets/DataTransferObjects/WalletObject.php index 116117ce..e24fe2f9 100644 --- a/app/Classes/Modules/Wallets/DataTransferObjects/WalletObject.php +++ b/app/Classes/Modules/Wallets/DataTransferObjects/WalletObject.php @@ -6,63 +6,54 @@ use App\Classes\Interfaces\DataTransferObject; class WalletObject implements DataTransferObject { - /** @var string|null */ - private $code; - /** @var string|null */ - private $amount; - - /** @var string|null */ + /** @var int */ private $company_id; - + + /** @var int */ private $currency; - - public function __construct( - ?string $code, - ?string $currency, - ?string $amount, - ?string $company_id - ) + /** @var int */ + private $code; + + /** + * WalletObject constructor. + * @param int $company_id + * @param int $currency + * @param int $code + */ + public function __construct(int $company_id, int $currency, int $code) { - $this->code = $code; - $this->currency = $currency; - $this->amount = $amount; $this->company_id = $company_id; + $this->currency = $currency; + $this->code = $code; } /** - * @return string + * @return int */ - public function getCode(): ?string + public function getCompanyId(): int { - return $this->code; + return $this->company_id; } /** - * @return string + * @return int */ - public function getCurrency(): ?string + public function getCurrency(): int { return $this->currency; } /** - * @return string + * @return int */ - public function getAmount(): ?float + public function getCode(): int { - return floatval($this->amount); - } - - /** - * @return int|null - */ - public function getCompanyId(): ?int - { - return $this->company_id; + return $this->code; } - + + } \ No newline at end of file diff --git a/app/Classes/Modules/Wallets/Services/ChecksIfWalletCodeExists.php b/app/Classes/Modules/Wallets/Services/ChecksIfWalletCodeExists.php new file mode 100644 index 00000000..4efedc38 --- /dev/null +++ b/app/Classes/Modules/Wallets/Services/ChecksIfWalletCodeExists.php @@ -0,0 +1,27 @@ +repository = $repository; + } + + public function execute(int $code): bool { + return $this->repository->where('code', $code)->exists(); + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Wallets/Services/CreatesWallet.php b/app/Classes/Modules/Wallets/Services/CreatesWallet.php index 9e7c0c5d..c76113c7 100644 --- a/app/Classes/Modules/Wallets/Services/CreatesWallet.php +++ b/app/Classes/Modules/Wallets/Services/CreatesWallet.php @@ -4,22 +4,21 @@ namespace App\Classes\Modules\Wallets\Services; use App\Classes\General\Eloquent\AbstractUpdateRecord; use App\Classes\Modules\Wallets\DataTransferObjects\WalletObject; -use App\Models\Wallets; +use App\Models\Wallet; class CreatesWallet extends AbstractUpdateRecord { /** - * @param UserObject $object + * @param WalletObject $object * @return \Illuminate\Database\Eloquent\Model * @throws \App\Classes\Exceptions\MalformedRequestException */ public function execute(WalletObject $object) { - $model = new Wallets(); + $model = new Wallet(); + $model->company_id = $object->getCompanyId(); $model->code = $object->getCode(); $model->currency = $object->getCurrency(); - $model->company_id = $object->getCompanyId(); - $model->amount = $object->getAmount(); - + return $this->handler($model); diff --git a/app/Classes/Modules/Wallets/Services/GeneratesWalletCode.php b/app/Classes/Modules/Wallets/Services/GeneratesWalletCode.php new file mode 100644 index 00000000..af2bda7e --- /dev/null +++ b/app/Classes/Modules/Wallets/Services/GeneratesWalletCode.php @@ -0,0 +1,32 @@ +walletCodeExists = $walletCodeExists; + } + + + /** + * @return int + */ + public function execute(): int { + + $code = mt_rand(100000001, 999999999); + return !$this->walletCodeExists->execute($code) ? $code : self::execute(); + + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Wallets/Standards/Rules/CanCreateCompanyWallet.php b/app/Classes/Modules/Wallets/Standards/Rules/CanCreateCompanyWallet.php new file mode 100644 index 00000000..aa1a49d1 --- /dev/null +++ b/app/Classes/Modules/Wallets/Standards/Rules/CanCreateCompanyWallet.php @@ -0,0 +1,54 @@ +companyWalletValidation = $companyWalletValidation; + } + + + /** + * @return bool + */ + protected function authorized(): bool + { + // TODO Set Authorization rules + return true; + } + + /** + * @param CompanyWalletValidation $object + * @return bool + * @throws \App\Classes\Exceptions\RequestValidationException + */ + protected function validators($object): bool + { + return $this->companyWalletValidation->validate($object); + } + + /** + * @param SegmentCompanyValidation $object + * @return bool + */ + protected function criteria($object): bool + { + return true; + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Wallets/Standards/Validators/CompanyWalletValidation.php b/app/Classes/Modules/Wallets/Standards/Validators/CompanyWalletValidation.php new file mode 100644 index 00000000..5d4cea89 --- /dev/null +++ b/app/Classes/Modules/Wallets/Standards/Validators/CompanyWalletValidation.php @@ -0,0 +1,41 @@ + $object->getCompanyId(), + 'currency' => $object->getCurrency(), + ]; + } + + /** + * @return array + */ + protected function rules(): array + { + return [ + 'company_id' => 'required', + 'currency' => 'required', + ]; + } + + /** + * @return array + */ + protected function messages(): array + { + return []; + } +} \ No newline at end of file diff --git a/app/Models/Wallets.php b/app/Models/Wallet.php similarity index 86% rename from app/Models/Wallets.php rename to app/Models/Wallet.php index ca947fd4..f9808e9e 100644 --- a/app/Models/Wallets.php +++ b/app/Models/Wallet.php @@ -5,7 +5,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Relations\HasOne; -class Wallets extends AbstractModel +class Wallet extends AbstractModel { protected $table = 'wallets'; diff --git a/database/migrations/2020_11_29_212614_create_companies_wallet_table.php b/database/migrations/2020_11_29_212614_create_companies_wallet_table.php index 475a4c26..c73c90c5 100644 --- a/database/migrations/2020_11_29_212614_create_companies_wallet_table.php +++ b/database/migrations/2020_11_29_212614_create_companies_wallet_table.php @@ -15,12 +15,14 @@ class CreateCompaniesWalletTable extends Migration { Schema::create('wallets', function (Blueprint $table) { $table->id(); + + $table->bigInteger('company_id')->unsigned(); $table->string('code'); $table->string('currency'); - $table->decimal('amount', 8, 2); - $table->bigInteger('company_id')->unsigned(); - $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade'); + $table->decimal('amount', 8, 2)->default(0.00); $table->timestamps(); + + $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade'); }); } diff --git a/routes/wallet.php b/routes/wallet.php index 2774ea40..b80135ce 100644 --- a/routes/wallet.php +++ b/routes/wallet.php @@ -2,12 +2,7 @@ use Illuminate\Support\Facades\Route; -Route::group(['prefix' => 'wallet', 'namespace' => 'Wallets', 'as' => 'wallet.'], function () { - - Route::group(['prefix' => 'authentication', 'as' => 'authentication.'], function () { - - }); - +Route::group(['prefix' => 'wallet', 'namespace' => 'Wallet', 'as' => 'wallet.'], function () { Route::post('/create', 'CreateWalletController@create')->name('create');