Esempio n. 1
0
 /**
  * Add a new AuthTicket. Tickets will always expire in $this->_expireSeconds seconds.
  *
  * @param AuthTicketModel $model
  * @throws ControllerException
  * @return string auth_ticket added on success
  * @SuppressWarnings indentation
  */
 public function add($model)
 {
     if ($model->validateForAdd()) {
         try {
             $query = 'INSERT auth_ticket' . ' ( auth_ticket' . ', created' . ', updated' . ', expires' . ' )' . ' VALUES ( ?, NOW(), NOW(), ? )' . ' ON DUPLICATE KEY UPDATE expires = ?';
             $stmt = $this->_dbh->prepare($query);
             if (!$stmt) {
                 throw new ControllerException('Prepared statement failed for ' . $query);
             }
             $authTicket = $model->getAuthTicket();
             $expires = date('Y-m-d H:i:m', time() + $this->_expireSeconds);
             if (!$stmt->bind_param('sss', $authTicket, $expires, $expires)) {
                 throw new ControllerException('Binding parameters for prepared statement failed.');
             }
             if (!$stmt->execute()) {
                 throw new ControllerException('Failed to execute INSERT statement. Is this duplicate data? (' . $this->_dbh->error . ')');
             }
             /** @SuppressWarnings checkAliases */
             if (!$stmt->close()) {
                 throw new ControllerException('Something broke while trying to close the prepared statement.');
             }
             return $authTicket;
         } catch (Exception $e) {
             throw new ControllerException($e->getMessage());
         }
     } else {
         throw new ControllerException('Invalid data');
     }
 }