Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
17.31% |
9 / 52 |
|
25.00% |
2 / 8 |
CRAP | n/a |
0 / 0 |
|
| getSizeUploadedFileToHumanReadable | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| getSizeFromRemoteFile | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
20 | |||
| parseInstancyXml | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| xmlToArray | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
30 | |||
| isLocalProduction | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isProduction | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| role | |
63.64% |
7 / 11 |
|
0.00% |
0 / 1 |
7.73 | |||
| isNowGreaterThan | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | use Illuminate\Support\Str; |
| 4 | use App\Http\Models\Auth\Role; |
| 5 | use Carbon\Carbon; |
| 6 | |
| 7 | if (!function_exists('getSizeUploadedFileToHumanReadable')) { |
| 8 | function getSizeUploadedFileToHumanReadable($size, $precision = 2) |
| 9 | { |
| 10 | if ($size > 0) { |
| 11 | $size = (int) $size; |
| 12 | $base = log($size) / log(1024); |
| 13 | $suffixes = [' bytes', ' KB', ' MB', ' GB', ' TB']; |
| 14 | |
| 15 | return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)]; |
| 16 | } |
| 17 | |
| 18 | return $size; |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | if (!function_exists('getSizeFromRemoteFile')) { |
| 23 | function getSizeFromRemoteFile($remoteFile) |
| 24 | { |
| 25 | $ch = curl_init($remoteFile); |
| 26 | curl_setopt($ch, CURLOPT_NOBODY, true); |
| 27 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 28 | curl_setopt($ch, CURLOPT_HEADER, true); |
| 29 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //not necessary unless the file redirects (like the PHP example we're using here) |
| 30 | $data = curl_exec($ch); |
| 31 | curl_close($ch); |
| 32 | if ($data === false) { |
| 33 | echo 'cURL failed'; |
| 34 | exit; |
| 35 | } |
| 36 | $contentLength = 'unknown'; |
| 37 | $status = 'unknown'; |
| 38 | if (preg_match('/^HTTP\/1\.[01] (\d\d\d)/', $data, $matches)) { |
| 39 | $status = (int) $matches[1]; |
| 40 | } |
| 41 | if (preg_match('/Content-Length: (\d+)/', $data, $matches)) { |
| 42 | $contentLength = (int) $matches[1]; |
| 43 | } |
| 44 | |
| 45 | return $contentLength; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | function parseInstancyXml($xml) |
| 50 | { |
| 51 | // Load the XML string |
| 52 | $xmlObj = simplexml_load_string($xml); |
| 53 | // Convert XML to associative array |
| 54 | $xmlArray = xmlToArray($xmlObj); |
| 55 | // Print the associative array representation |
| 56 | return $xmlArray; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Function to convert SimpleXMLElement to array recursively |
| 61 | * |
| 62 | * @param $xml |
| 63 | * |
| 64 | * @return array |
| 65 | */ |
| 66 | function xmlToArray($xml) |
| 67 | { |
| 68 | $array = []; |
| 69 | foreach ($xml->attributes() as $key => $value) { |
| 70 | $array['@attributes'][$key] = (string) $value; |
| 71 | } |
| 72 | if ($xml->children()->count() > 0) { |
| 73 | foreach ($xml->children() as $child) { |
| 74 | if ($child->getName() !== 'text') { |
| 75 | $array[$child->getName()][] = xmlToArray($child); |
| 76 | } else { |
| 77 | $array['value'] = trim((string) $child); |
| 78 | } |
| 79 | } |
| 80 | } else { |
| 81 | $array['value'] = trim((string) $xml); |
| 82 | } |
| 83 | return $array; |
| 84 | } |
| 85 | |
| 86 | function isLocalProduction() |
| 87 | { |
| 88 | return env('APP_ENV') == 'jonatasprod'; |
| 89 | } |
| 90 | |
| 91 | function isProduction() |
| 92 | { |
| 93 | return env('APP_ENV') == 'production'; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | function role($role) |
| 98 | { |
| 99 | if (is_array($role)) { |
| 100 | $role = implode(',', $role); |
| 101 | } |
| 102 | |
| 103 | if (Str::contains($role, Role::VENGRESO_ADMIN)) { |
| 104 | return Role::VENGRESO_ADMIN; |
| 105 | } else if (Str::contains($role, Role::GLOBAL_ADMIN)) { |
| 106 | return Role::GLOBAL_ADMIN; |
| 107 | } else if (Str::contains($role, Role::GROUP_ADMIN)) { |
| 108 | return Role::GROUP_ADMIN; |
| 109 | } else if (Str::contains($role, Role::REPORTING_ADMIN)) { |
| 110 | return Role::REPORTING_ADMIN; |
| 111 | } else { |
| 112 | return Role::USER; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | function isNowGreaterThan(string $endDate): bool |
| 117 | { |
| 118 | $now = Carbon::now('UTC'); |
| 119 | $endDateUtc = Carbon::parse($endDate)->setTimezone('UTC'); |
| 120 | |
| 121 | return $now->greaterThan($endDateUtc); |
| 122 | } |