Last active
January 5, 2021 07:13
-
-
Save scc-agnitio/78acca384ad7e8a0ddee755a81ba0b74 to your computer and use it in GitHub Desktop.
Parses and creates a hash of the core values that make up a MIO.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class MioHelper | |
| { | |
| public static function hash($mioRecord) | |
| { | |
| $mio = self::processMio($mioRecord); | |
| return md5(serialize($mio)); | |
| } | |
| public static function processMio($mioRecord) | |
| { | |
| $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'] = md5(serialize(self::parseCallInfo($mio['callInfo'], $timezoneOffset))); | |
| // session data | |
| $data['sessions'] = self::parseSessions($mio['sessions'], $timezoneOffset); | |
| // Deep sort data | |
| $sorted = self::sortArrayRecursive($data); | |
| return $sorted; | |
| } | |
| protected 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) | |
| { | |
| $sessions = self::sortArrayRecursive($sessions); | |
| return collect($sessions)->map(function ($session) use ($timezoneOffset) { | |
| $startTime = $session['startTime'] - $timezoneOffset; | |
| $endTime = $session['endTime'] - $timezoneOffset; | |
| $presentationId = $session['presentationId']; | |
| $events = md5(serialize(self::parseEvents($session['events'], $timezoneOffset))); | |
| return [ | |
| 'startTime' => $startTime, | |
| 'endTime' => $endTime, | |
| 'presentationId' => $presentationId, | |
| 'events' => $events, | |
| ]; | |
| })->toArray(); | |
| } | |
| protected static function parseEvents(array $events, $timezoneOffset = 0) | |
| { | |
| $events = collect($events)->reject(function ($event) { | |
| return $event['category'] === 'Versions'; | |
| })->map(function ($event) use ($timezoneOffset) { | |
| return ['time' => (int) $event['time'] - $timezoneOffset]; | |
| })->values()->toArray(); | |
| return $events; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment