Files
exchange-2.0/app/Classes/Modules/Currencies/ControllersLogic/CreateCurrencyLogic.php
T
CheeSiong 1dfb75545f List Currency Rate Logic Class
Fetch Currency Rate Logic Class
 Create Currency Rate Logic Class
 Update Currency Rate Logic Class
Currency Rate Converter Rate
2020-12-30 08:47:12 +08:00

86 lines
2.5 KiB
PHP

<?php
namespace App\Classes\Modules\Currencies\ControllersLogic;
use App\Classes\General\Abstracts\AbstractControllersLogic;
use App\Classes\Modules\Currencies\Standards\Rules\CanCreateCurrency;
use App\Classes\Modules\Currencies\Services\CreatesCurrency;
use App\Classes\Modules\Currencies\Services\CreatesCurrencyLog;
use App\Classes\Modules\Currencies\DataTransferObjects\CurrencyObject;
use App\Http\Resources\CurrencyResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class CreateCurrencyLogic extends AbstractControllersLogic
{
/**
* @return array
*/
protected function notification():array {
return [
'title' => 'Created Currency',
'message' => 'You have successfully created a new Currency'
];
}
/** @var CanCreateCurrency */
private $canCreateCurrency;
/** @var CreatesCurrency */
private $createsCurrency;
/** @var CreatesCurrencyLog */
private $createsCurrencyLog;
/**
* CreateSegmentLogic constructor.
* @param CanCreateCurrency $canCreateCurrency
* @param CreatesCurrency $createsCurrency
* @param CreatesCurrencyLog $createsCurrencyLog
*/
public function __construct(
CanCreateCurrency $canCreateCurrency,
CreatesCurrency $createsCurrency,
CreatesCurrencyLog $createsCurrencyLog
)
{
$this->canCreateCurrency = $canCreateCurrency;
$this->createsCurrency = $createsCurrency;
$this->createsCurrencyLog = $createsCurrencyLog;
}
/**
* @param Request $request
* @return JsonResponse
* @throws ErrorException
*/
public function logic(Request $request) : JsonResponse
{
try {
DB::beginTransaction();
$currency_object = new CurrencyObject(
$request->input('country_id'),
$request->input('name'),
$request->input('short_code'),
$request->input('symbol')
);
$this->canCreateCurrency->passes($currency_object);
$currency_query = $this->createsCurrency->execute($currency_object);
$currency_log_query = $this->createsCurrencyLog->execute($currency_query);
DB::commit();
return $this->resourceResponse(new CurrencyResource($currency_query));
} catch (\Exception $exception) {
throw new ErrorException($exception->getMessage(), $exception->getCode());
}
}
}