canCrossAuthenticateUser = $canCrossAuthenticateUser; $this->crossAuthenticatesUser = $crossAuthenticatesUser; $this->generatesAuthenticationToken = $generatesAuthenticationToken; $this->fetchesUser = $fetchesUser; $this->authenticationRedirect = $authenticationRedirect; $this->createUserProcessor = $createUserProcessor; $this->createCompanyProcessor = $createCompanyProcessor; $this->createContactProcessor = $createContactProcessor; $this->createCompanyModuleProcessor = $createCompanyModuleProcessor; $this->createsCompanyConnection = $createsCompanyConnection; $this->createsCompanyConnection = $createsCompanyConnection; $this->approvesCompanyConnection = $approvesCompanyConnection; $this->assignEmployeeProcessor = $assignEmployeeProcessor; $this->uploadIdentityDocumentProcessor = $uploadIdentityDocumentProcessor; $this->createsUserSocialAccount = $createsUserSocialAccount; } /** * @param Request $request * @return array * @throws \App\Classes\Exceptions\AccessForbiddenException * @throws \App\Classes\Exceptions\AccessUnauthorisedException * @throws \App\Classes\Exceptions\RequestValidationException */ public function execute(Request $request): array { $object = new CrossAuthenticationCredentialsObject($request->input('access_token'), $request->input('platform')); $this->canCrossAuthenticateUser->passes($object); $cross = $this->crossAuthenticatesUser->execute($object); $user_social_account = UserSocialAccount::where('app_id', $cross->token->user->id)->first(); if (!$user_social_account) { $request->request->add([ 'name' => $cross->token->user->name, 'email' => $cross->token->user->email, 'password' => 'pass123', 'password_confirmation' => 'pass123', ]); $user = $this->createUserProcessor->execute( $request, RoleTypes::USER, env('APP_ENV') != 'production' ? ApprovalStatus::APPROVED : ApprovalStatus::PENDING_VERIFICATION ); $company = $this->createCompanyProcessor->execute( $cross->payload->data->name, CompanyType::COMPANY_BUSINESS, ApprovalStatus::PENDING_SUBMISSION ); $contactObject = new ContactObject( $cross->payload->data->contact->reference, $cross->payload->data->contact->phone, $cross->payload->data->contact->email, $cross->payload->data->contact->wechat_id ); $contact = $this->createContactProcessor->execute($contactObject, $company); $companyModule = $this->createCompanyModuleProcessor->execute($company, BusinessType::IMPORTER); $connectionObject = new CompanyConnectionObject( $companyModule, 'CIEF', mt_rand(1000, 9999).(new GeneratesInitials())->name($company->name)->length(3)->generate() ); $connection = $this->createsCompanyConnection->execute($connectionObject); $this->approvesCompanyConnection->execute($connection); $object = new EmploymentObject($companyModule, $user); $this->assignEmployeeProcessor->execute($object); $request->request->add([ 'files' => [$cross->document->src], 'identification_no' => $cross->payload->data->identification->reference, ]); $this->uploadIdentityDocumentProcessor->execute($request, $company); $object = new UserSocialAccountObject( $cross->token->user->id, $user, $request->input('platform') ); $this->createsUserSocialAccount->execute($object); } else { $user = $user_social_account->user()->first(); } return ['access_token' => $this->generatesAuthenticationToken->execute($user), 'redirect_url' => $this->authenticationRedirect->url($user)]; } }