Controllers\xAPI\StatementStoreController::createStatements PHP Method

createStatements() private method

Creates statements from the content of the request.
private createStatements ( $options, callable $modifier = null ) : AssocArray
$modifier callable A function that modifies the statements before storing them.
return AssocArray Result of storing the statements.
    private function createStatements($options, callable $modifier = null)
    {
        Helpers::validateAtom(new XApiImt(explode(';', LockerRequest::header('Content-Type'))[0]));
        // Gets parts of the request.
        $parts = $this->getParts();
        $content = $parts['content'];
        // Decodes $statements from $content.
        try {
            if (Config::get('xapi.disable_duplicate_key_checks') !== true) {
                // Check incoming statements for duplicate keys and throw an error if found
                $jsonParser = new JsonParser();
                $jsonParser->parse($content, JsonParser::DETECT_KEY_CONFLICTS);
                // this will catch any parsing issues
            }
            $statements = json_decode($content);
            if ($statements === null && $content !== '') {
                throw new Exceptions\Exception('Invalid JSON');
            } else {
                if ($statements === null) {
                    $statements = [];
                }
            }
        } catch (\Seld\JsonLint\DuplicateKeyException $e) {
            $details = $e->getDetails();
            throw new Exceptions\Exception(sprintf('Invalid JSON: `%s` is a duplicate key on line %s', $details['key'], $details['line']));
        } catch (\Exception $e) {
            // some other parsing error occured
            throw new Exceptions\Exception('Invalid JSON: JSON could not be parsed');
        }
        // Ensures that $statements is an array.
        if (!is_array($statements)) {
            $statements = [$statements];
        }
        // Runs the modifier if there is one and there are statements.
        if (count($statements) > 0 && $modifier !== null) {
            $statements = $modifier($statements);
        }
        // Saves $statements with attachments.
        return $this->statements->store($statements, is_array($parts['attachments']) ? $parts['attachments'] : [], array_merge(['authority' => $this->getAuthority($options['client'])], $options));
    }