Files
exchange-2.0/app/Classes/Modules/Companies/ControllersLogic/AssignCompanyToSegmentLogic.php
T
2023-03-18 23:00:02 +08:00

88 lines
3.0 KiB
PHP

<?php
namespace App\Classes\Modules\Companies\ControllersLogic;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Companies\Processors\AssignSegmentProcessor;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Segments\DataTransferObjects\SeasonalSegmentObject;
use App\Classes\Modules\Segments\Services\CreatesSeasonalSegment;
use App\Http\Resources\CompanyResource;
use App\Models\SeasonalSegment;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class AssignCompanyToSegmentLogic extends AbstractControllerLogic
{
/**
* @return array
*/
protected function notification():array {
return [
'title' => 'Assign Company To Segment',
'message' => 'You have successfully assigned Company to segment'
];
}
/** @var FetchesCompany */
private $fetchesCompany;
/** @var AssignSegmentProcessor */
private $assignCompanyToSegmentProcessor;
/** @var CreatesSeasonalSegment */
private $createsSeasonalSegment;
/**
* AssignCompanyToSegmentLogic constructor.
* @param FetchesCompany $fetchesCompany
* @param AssignSegmentProcessor $assignCompanyToSegmentProcessor
* @param CreatesSeasonalSegment $createsSeasonalSegment
*/
public function __construct(FetchesCompany $fetchesCompany, AssignSegmentProcessor $assignCompanyToSegmentProcessor, CreatesSeasonalSegment $createsSeasonalSegment)
{
$this->fetchesCompany = $fetchesCompany;
$this->assignCompanyToSegmentProcessor = $assignCompanyToSegmentProcessor;
$this->createsSeasonalSegment = $createsSeasonalSegment;
}
/**
* @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
{
$company = $this->fetchesCompany->execute(['id' => $request->route('id')]);
$this->assignCompanyToSegmentProcessor->execute($company, $request->input('segment_id'));
if (env('HONEY_TRAP_SEGMENT_ID') && env('HONEY_TRAP_SEGMENT_ID') == $request->input('segment_id')) {
// todo-new
// if (isset($request->input('ending_on')) && $request->input('ending_on') <= Carbon::now()) {
// return error
// }
// todo-new
// if (count($company->seasonalSegments)) {
// return error
// }
$start_date = $request->input('starting_on') ? $request->input('starting_on') : Carbon::now();
$seasonalSegmentObject = new SeasonalSegmentObject($company->id, $request->input('segment_id'), $start_date, $request->input('ending_on') ?? null);
$this->createsSeasonalSegment->execute($seasonalSegmentObject);
}
return $this->resourceResponse(new CompanyResource($company));
}
}