gitea_issues_exporter/app/Http/Controllers/ThirdPartyServices.php

98 lines
4.3 KiB
PHP
Raw Normal View History

2024-07-22 14:17:00 +02:00
<?php
namespace App\Http\Controllers;
use App\Mail\sendExportNotice;
use DateTime;
use Http\Discovery\Exception;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;
use RuntimeException;
class ThirdPartyServices extends Controller
{
private $nextcloud_url;
private $nextcloud_user;
private $nextcloud_password;
private $openproject_url;
private $openproject_user;
private $openproject_password;
private $third_party_integrations_nextcloud;
private $third_party_integrations_open_project;
public function __construct()
{
$this->third_party_integrations_nextcloud = env('THIRD_PARTY_INTEGRATIONS_NEXTCLOUD', false);
$this->third_party_integrations_open_project = env('THIRD_PARTY_INTEGRATIONS_OPEN_PROJECT', false);
$this->nextcloud_url = env('NEXTCLOUD_URL', false);
$this->nextcloud_user = env('NEXTCLOUD_USER', false);
$this->nextcloud_password = env('NEXTCLOUD_PASSWORD', false);
$this->nextcloud_upload_folder_path = env('NEXTCLOUD_UPLOAD_FOLDER_PATH', false);
$this->nextcloud_upload_folder_web_link = env('NEXTCLOUD_UPLOAD_FOLDER_WEB_LINK', false);
$this->openproject_url = env('OPENPROJECT_URL', false);
$this->openproject_token = env('OPENPROJECT_TOKEN', false);
$this->openproject_project = env('OPENPROJECT_PROJECT', false);
$this->openproject_task_name = env('OPENPROJECT_TASK_NAME', false);
$this->email_send_allow = env('EMAIL_SEND_ALLOW', false);
$this->email_address_recepient = env('EMAIL_ADDRESS_RECEPIENT', null);
$this->email_prefix_subject = env('EMAIL_PREFIX_SUBJECT', null);
}
function nextcloud_upload_csv(string $file_name, string $file_path)
{
try {
$nextcloud_url = $this->nextcloud_url . '/remote.php/dav/files/' . $this->nextcloud_user . '/' . $this->nextcloud_upload_folder_path . '/' . $file_name;
$file_contents = fopen($file_path, 'r');
try {
$res = Http::withBasicAuth($this->nextcloud_user, $this->nextcloud_password)
->attach($file_name, $file_contents)
->put($nextcloud_url);
$res->throw();
} catch (\Exception $e) {
Log::error('nextcloud_upload_csv Upload error - ' . $e->getMessage());
throw new RuntimeException("E-NEXTCLOUD-UPLOAD");
}
// Close the file handle
fclose($file_contents);
return $res;
} catch (\Exception $e) {
return false;
}
}
function openproject_add_task_to_agent(array $issues, array $company_agents)
{
$openproject_main_group_members_url = $this->openproject_url . '/api/v3/groups/' . $this->openproject_group_id;
try {
$res = Http::withToken($this->openproject_token)->get($openproject_url);
$res->throw();
} catch (\Exception $e) {
Log::error('nextcloud_upload_csv Upload error - ' . $e->getMessage());
throw new RuntimeException("E-NEXTCLOUD-UPLOAD");
}
}
function send_export_via_email(string $from_date, string $to_date, string $file_name){
$email_subject = $this->email_prefix_subject . ' '. $from_date.' - '.$to_date;
Mail::to($this->email_address_recepient)->send(new sendExportNotice(['export_date' => $email_subject, 'file_name' => $file_name, 'upload_folder_web_link' => $this->nextcloud_upload_folder_web_link], $email_subject));
return true;
}
function handle_third_party_services(string $file_name, string $file_path, array $issues, array $company_agents, string $from_date, string $to_date)
{
$nextcloud_res = $open_project_res = $email_res = false;
if ($this->third_party_integrations_nextcloud) {
$nextcloud_res = $this->nextcloud_upload_csv($file_name, $file_path);
}
if ($this->third_party_integrations_open_project) {
$open_project_res = $this->openproject_add_task_to_agent($issues, $company_agents);
}
if($this->email_send_allow) {
$email_res = $this->send_export_via_email($from_date, $to_date, $file_name);
}
return [$nextcloud_res, $open_project_res, $email_res];
}
}