| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Services\Tickets\Importer; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Person; |
| 6 |
|
| 7 |
class Common |
| 8 |
{ |
| 9 |
public static function updateOrCreatePerson($personData) |
| 10 |
{ |
| 11 |
$emailArray = [ |
| 12 |
'email' => $personData['email'], |
| 13 |
'person_type' => $personData['person_type'] |
| 14 |
]; |
| 15 |
|
| 16 |
$person = Person::updateOrCreate($emailArray, $personData); |
| 17 |
return $person->toArray(); |
| 18 |
} |
| 19 |
|
| 20 |
public static function formatPersonData($personData, $type) |
| 21 |
{ |
| 22 |
if(!$personData) { |
| 23 |
return []; |
| 24 |
} |
| 25 |
|
| 26 |
$name = explode(' ', $personData->name); |
| 27 |
|
| 28 |
return [ |
| 29 |
'first_name' => $name[0] ?? '', |
| 30 |
'last_name' => $name[1] ?? '', |
| 31 |
'email' => $personData->email ?? $personData->address, |
| 32 |
'person_type' => $type |
| 33 |
]; |
| 34 |
} |
| 35 |
|
| 36 |
// Download a file from a remote URL and create a new directory for this if not exists |
| 37 |
// Then save the file to the new directory and move this directory to a new given directory |
| 38 |
public static function downloadFile($remoteUrl, $baseDir, $fileName) |
| 39 |
{ |
| 40 |
$filePath = $baseDir . $fileName; |
| 41 |
|
| 42 |
if (!file_exists($baseDir)) { |
| 43 |
mkdir($baseDir, 0777, true); |
| 44 |
} |
| 45 |
|
| 46 |
if (!file_exists($filePath)) { |
| 47 |
$response = wp_remote_get($remoteUrl, [ |
| 48 |
'timeout' => 60, |
| 49 |
'stream' => false |
| 50 |
]); |
| 51 |
|
| 52 |
if (is_wp_error($response)) { |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
$file_contents = wp_remote_retrieve_body($response); |
| 57 |
if (empty($file_contents)) { |
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
file_put_contents($filePath, $file_contents); |
| 62 |
} |
| 63 |
|
| 64 |
return $filePath; |
| 65 |
} |
| 66 |
} |
| 67 |
|