Skip to content

Instantly share code, notes, and snippets.

@scc-agnitio
Created January 8, 2021 07:00
Show Gist options
  • Select an option

  • Save scc-agnitio/1ff9ac18b42624d38a9f6c46123dcda9 to your computer and use it in GitHub Desktop.

Select an option

Save scc-agnitio/1ff9ac18b42624d38a9f6c46123dcda9 to your computer and use it in GitHub Desktop.
Mio Hasher
<?php
namespace App\Mio;
use App\Mio\Exceptions\MioHashingException;
use Exception;
use Illuminate\Support\Facades\Log;
class MioHelper
{
public static function hash($mioRecord)
{
return md5(serialize(self::processMio($mioRecord)));
}
public static function processMio($mioRecord, bool $hashes = false)
{
try {
$mio = json_decode($mioRecord->received_encoded_object, true);
$timezoneOffset = (int) $mio['callInfo']['timezone'];
// Set Data
$data['organisationId'] = $mio['organisationId'];
$data['userId'] = collect($mio['presenters'])->filter(function ($presenter) {
return $presenter['isPrimary'];
})->first()['id'] ?? null;
// call info
$data['callInfo'] = $hashes
? md5(serialize(self::parseCallInfo($mio['callInfo'], $timezoneOffset)))
: self::parseCallInfo($mio['callInfo'], $timezoneOffset);
// session data
$data['sessions'] = self::parseSessions($mio['sessions'], $timezoneOffset, $hashes);
// Deep sort data
$sorted = self::sortArrayRecursive($data);
return $sorted;
} catch (Exception $exception) {
$message = sprintf("Error hashing MIO ID %d", $mioRecord->id);
Log::error($message, [
'message' => $exception->getMessage(),
'line' => $exception->getLine(),
'trace' => $exception->getTrace(),
]);
throw new MioHashingException($message, $exception->getCode());
}
}
public static function sortArrayRecursive($data)
{
return collect($data)->sortKeys()->map(function ($item, $key) {
return is_array($item) ? self::sortArrayRecursive($item) : $item;
})->toArray();
}
protected static function parseCallInfo(array $callInfo, int $timezoneOffset)
{
return [
'startTime' => (int) $callInfo['startTime'] - $timezoneOffset,
'endTime' => (int) $callInfo['endTime'] - $timezoneOffset,
'activityName' => $callInfo['activityName'],
];
}
protected static function parseSessions(array $sessions, int $timezoneOffset = 0, bool $hashes = false)
{
$sessions = self::sortArrayRecursive($sessions);
return collect($sessions)->map(function ($session) use ($timezoneOffset, $hashes) {
$startTime = $session['startTime'] - $timezoneOffset;
$endTime = $session['endTime'] - $timezoneOffset;
$presentationId = $session['presentationId'];
$events = self::parseEvents($session['events'], $timezoneOffset, $hashes);
return [
'startTime' => $startTime,
'endTime' => $endTime,
'presentationId' => $presentationId,
'events' => $events,
];
})->toArray();
}
protected static function parseEvents(array $events, $timezoneOffset = 0, bool $hashes = false)
{
$events = collect($events)->reject(function ($event) {
return $event['category'] === 'Versions';
})->map(function ($event) use ($timezoneOffset, $hashes) {
$eventData = [
// 'category' => $event['category'],
'time' => (int) $event['time'] - $timezoneOffset
];
return $hashes ? md5(serialize($eventData)) : $eventData;
})->values()->toArray();
return $events;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment