canAssignSegment = $canAssignSegment; $this->assignsSegment = $assignsSegment; $this->fetchesSegment = $fetchesSegment; } /** * @param Company $company * @param int|null $segmentId * @return Model * @throws \App\Classes\Exceptions\AccessForbiddenException * @throws \App\Classes\Exceptions\MalformedRequestException * @throws \App\Classes\Exceptions\RequestValidationException */ public function execute(Company $company, ?int $segmentId = SegmentConstants::STANDARD_SEGMENT): Model { $segment = $this->fetchesSegment->execute(['id' => $segmentId]); $companySegment = new CompanySegmentObject($company, $segment); try { $this->canAssignSegment->passes($companySegment); } catch (\Exception $e) { return $company; } return $this->assignsSegment->execute($company, $segment); } }