mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/shipping-portal.git
synced 2026-08-19 04:24:12 +00:00
new: add classes required to assign user as an employee to a company. #1
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Companies\DataTransferObjects;
|
||||
|
||||
use App\Classes\General\Interfaces\DataTransferObject;
|
||||
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
||||
use App\Models\Company;
|
||||
use App\Models\User;
|
||||
|
||||
class EmploymentObject implements DataTransferObject
|
||||
{
|
||||
/** @var Company */
|
||||
private $company;
|
||||
|
||||
/** @var User */
|
||||
private $user;
|
||||
|
||||
/** @var int|null */
|
||||
private $status;
|
||||
|
||||
/**
|
||||
* EmploymentObject constructor.
|
||||
* @param Company $company
|
||||
* @param User $user
|
||||
* @param int|null $status
|
||||
*/
|
||||
public function __construct(Company $company, User $user, ?int $status = ApprovalStatus::APPROVED)
|
||||
{
|
||||
$this->company = $company;
|
||||
$this->user = $user;
|
||||
$this->status = $status;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Company
|
||||
*/
|
||||
public function getCompany(): Company
|
||||
{
|
||||
return $this->company;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return User
|
||||
*/
|
||||
public function getUser(): User
|
||||
{
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getStatus(): int
|
||||
{
|
||||
return $this->status;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Companies\Processors;
|
||||
|
||||
use App\Classes\Modules\Companies\Services\AssignsEmployee;
|
||||
use App\Classes\Modules\Companies\Standards\Rules\CanAssignEmployee;
|
||||
use App\Classes\Modules\Companies\DataTransferObjects\EmploymentObject;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class AssignEmployeeProcessor
|
||||
{
|
||||
|
||||
/** @var CanAssignEmployee */
|
||||
private $canAssignEmployee;
|
||||
|
||||
/** @var AssignsEmployee */
|
||||
private $assignsEmployee;
|
||||
|
||||
/**
|
||||
* AssignEmployeeProcessor constructor.
|
||||
* @param CanAssignEmployee $canAssignEmployee
|
||||
* @param AssignsEmployee $assignsEmployee
|
||||
*/
|
||||
public function __construct(CanAssignEmployee $canAssignEmployee, AssignsEmployee $assignsEmployee)
|
||||
{
|
||||
$this->canAssignEmployee = $canAssignEmployee;
|
||||
$this->assignsEmployee = $assignsEmployee;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param EmploymentObject $object
|
||||
* @return Model
|
||||
* @throws \App\Classes\Exceptions\AccessForbiddenException
|
||||
* @throws \App\Classes\Exceptions\MalformedRequestException
|
||||
* @throws \App\Classes\Exceptions\RequestValidationException
|
||||
*/
|
||||
public function execute(EmploymentObject $object): Model {
|
||||
|
||||
$this->canAssignEmployee->passes($object);
|
||||
|
||||
return $this->assignsEmployee->execute($object);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Companies\Services;
|
||||
|
||||
use App\Classes\Exceptions\MalformedRequestException;
|
||||
use App\Classes\General\Eloquent\AbstractUpdateRecord;
|
||||
use App\Classes\Modules\Companies\DataTransferObjects\EmploymentObject;
|
||||
use Illuminate\Database\QueryException;
|
||||
|
||||
class AssignsEmployee extends AbstractUpdateRecord
|
||||
{
|
||||
/**
|
||||
* @param EmploymentObject $object
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
* @throws MalformedRequestException
|
||||
*/
|
||||
public function execute(EmploymentObject $object)
|
||||
{
|
||||
try {
|
||||
|
||||
$object->getCompany()->employees()->attach($object->getUser(), ['status' => $object->getStatus()]);
|
||||
|
||||
return $object->getCompany();
|
||||
|
||||
} catch (QueryException $exception){
|
||||
throw new MalformedRequestException($exception);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Companies\Standards\Rules;
|
||||
|
||||
|
||||
use App\Classes\General\Abstracts\AbstractRule;
|
||||
use App\Classes\Modules\Companies\Standards\Validators\CompanyEmployeeValidation;
|
||||
use App\Classes\Modules\Companies\DataTransferObjects\EmploymentObject;
|
||||
|
||||
class CanAssignEmployee extends AbstractRule
|
||||
{
|
||||
|
||||
/** @var CompanyEmployeeValidation */
|
||||
private $companyEmployeeValidation;
|
||||
|
||||
|
||||
/**
|
||||
* CanCreateCompanyEmployee constructor.
|
||||
* @param CompanyEmployeeValidation $companyEmployeeValidation
|
||||
*/
|
||||
public function __construct(CompanyEmployeeValidation $companyEmployeeValidation)
|
||||
{
|
||||
$this->companyEmployeeValidation = $companyEmployeeValidation;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
protected function authorized(): bool
|
||||
{
|
||||
// TODO Set Authorization rules
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param EmploymentObject $object
|
||||
* @return bool
|
||||
* @throws \App\Classes\Exceptions\RequestValidationException
|
||||
*/
|
||||
protected function validators($object): bool
|
||||
{
|
||||
return $this->companyEmployeeValidation->validate($object);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param EmploymentObject $object
|
||||
* @return bool
|
||||
*/
|
||||
protected function criteria($object): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Companies\Standards\Validators;
|
||||
|
||||
use App\Classes\General\Abstracts\AbstractValidation;
|
||||
use App\Classes\Modules\Companies\DataTransferObjects\EmploymentObject;
|
||||
|
||||
class CompanyEmployeeValidation extends AbstractValidation
|
||||
{
|
||||
/**
|
||||
* @param EmploymentObject $object
|
||||
* @return array
|
||||
*/
|
||||
protected function data($object): array
|
||||
{
|
||||
return [
|
||||
'company_id' => $object->getCompany()->id,
|
||||
'user_id' => $object->getUser()->id,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function rules(): array
|
||||
{
|
||||
return [
|
||||
'company_id' => 'required',
|
||||
'user_id' => 'required',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function messages(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user