function DatabaseProfilerStorage::write

File

webprofiler/src/Profiler/DatabaseProfilerStorage.php, line 97

Class

DatabaseProfilerStorage
Implements a profiler storage using the DBTNG query api.

Namespace

Drupal\webprofiler\Profiler

Code

public function write(Profile $profile) {
    $args = [
        'token' => $profile->getToken(),
        'parent' => $profile->getParentToken(),
        'data' => base64_encode(serialize($profile->getCollectors())),
        'ip' => $profile->getIp(),
        'method' => $profile->getMethod(),
        'url' => $profile->getUrl(),
        'time' => $profile->getTime(),
        'created_at' => time(),
        'status_code' => $profile->getStatusCode(),
    ];
    try {
        $query = $this->database
            ->select('webprofiler', 'w')
            ->fields('w', [
            'token',
        ]);
        $query->condition('token', $profile->getToken());
        $count = $query->countQuery()
            ->execute()
            ->fetchAssoc();
        if ($count['expression']) {
            $this->database
                ->update('webprofiler')
                ->fields($args)
                ->condition('token', $profile->getToken())
                ->execute();
        }
        else {
            $this->database
                ->insert('webprofiler')
                ->fields($args)
                ->execute();
        }
        $status = TRUE;
    } catch (\Exception $e) {
        $status = FALSE;
    }
    return $status;
}