Files
portal/app/Classes/Modules/PackingLists/ControllersLogic/CreatePackingListLogic.php
T
2021-07-24 15:17:54 +08:00

95 lines
3.2 KiB
PHP

<?php
namespace App\Classes\Modules\PackingLists\ControllersLogic;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\PackingLists\Services\CreatesPackingList;
use App\Classes\Modules\PackingLists\Services\CreatesPackingListPackage;
use App\Classes\Modules\PackingLists\Standards\Rules\CanCreatePackingList;
use App\Classes\Modules\PackingLists\Standards\Rules\CanCreatePackingListPackage;
use App\Classes\Modules\PackingLists\DataTransferObjects\PackingListObject;
use App\Classes\Modules\PackingLists\DataTransferObjects\PackingListPackageObject;
use App\Classes\Modules\Packages\Services\FetchesPackage;
use App\Http\Resources\PackingListResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class CreatePackingListLogic extends AbstractControllerLogic
{
/**
* @return array
*/
protected function notification():array {
return [
'title' => 'Created PackingList',
'message' => 'You have successfully created a new PackingList'
];
}
/** @var CanCreatePackingList */
private $canCreatePackingList;
private $canCreatePackingListPackage;
/** @var FetchesCompany */
private $fetchesPackage;
/** @var CreatesPackingList */
private $createsPackingList;
private $createsPackingListPackage;
/**
* CreatePackingListLogic constructor.
* @param CanCreatePackingList $canCreatePackingList
* @param FetchesDistrict $fetchesDistrict
* @param FetchesCompany $fetchesCompany
* @param CreatesPackingList $createsPackingList
*/
public function __construct(CanCreatePackingList $canCreatePackingList, CanCreatePackingListPackage $canCreatePackingListPackage, FetchesPackage $fetchesPackage, CreatesPackingList $createsPackingList, CreatesPackingListPackage $createsPackingListPackage)
{
$this->canCreatePackingList = $canCreatePackingList;
$this->canCreatePackingListPackage = $canCreatePackingListPackage;
$this->fetchesPackage = $fetchesPackage;
$this->createsPackingList = $createsPackingList;
$this->createsPackingListPackage = $createsPackingListPackage;
}
/**
* @param Request $request
* @return JsonResponse
* @throws \App\Classes\Exceptions\AccessForbiddenException
* @throws \App\Classes\Exceptions\MalformedRequestException
* @throws \App\Classes\Exceptions\RequestValidationException
*/
public function logic(Request $request) : JsonResponse
{
$package = $this->fetchesPackage->execute(['id'=>$request->input('package_id')]);
$reference_no = rand(1000000, 9999999);
$object = new PackingListObject($reference_no, ApprovalStatus::PENDING_SUBMISSION);
$this->canCreatePackingList->passes($object);
$packageList = $this->createsPackingList->execute( $object);
$object = new PackingListPackageObject($packageList->id, $package->id);
$this->canCreatePackingListPackage->passes($object);
$query = $this->createsPackingListPackage->execute($object);
return $this->resourceResponse(new PackingListResource($query));
}
}