public function storeUserIfMissing ($message) {
    $userId = $message['from']['id'];
    $username = strtolower($message['from']['username']);

    $query = $this->database->pdo->prepare('SELECT username FROM users WHERE userId = :userId');
    $query->bindParam(':userId', $userId);
    $query->execute();

    $user = $query->fetch(PDO::FETCH_ASSOC) ?? false;

    // If the user is not registered.
    if (!$user) {
      $query = $this->database->pdo->prepare('INSERT INTO users (userId, username, points) VALUES(:userId, :username, 0)');
      $query->bindParam(':userId', $userId);
      $query->bindParam(':username', $username);

      $query->execute();
      return;
    }

    // If the username changed.
    if ($username !== $user['username']) {
      $query = $this->database->pdo->prepare('UPDATE users SET username = :username WHERE userId = :userId');
      $query->bindParam(':userId', $userId);
      $query->bindParam(':username', $username);

      $query->execute();
    }
  }
public function storeUserIfMissing ($message) {
    $userId = $message['from']['id'];
    $username = strtolower($message['from']['username']);

    $query = $this->database->pdo->prepare('SELECT username FROM users WHERE userId = :userId');
    $query->bindParam(':userId', $userId);
    $query->execute();

    $user = $query->fetch(PDO::FETCH_ASSOC) ?? false;

    // If the user is not registered.
    if (!$user) {
      $query = $this->database->pdo->prepare('INSERT INTO users (userId, username, points) VALUES(:userId, :username, 0)');
      $query->bindParam(':userId', $userId);
      $query->bindParam(':username', $username);

      $query->execute();
      return;
    }

    // If the username changed.
    if ($username !== $user['username']) {
      $query = $this->database->pdo->prepare('UPDATE users SET username = :username WHERE userId = :userId');
      $query->bindParam(':userId', $userId);
      $query->bindParam(':username', $username);

      $query->execute();
    }
  }
public function storeUserIfMissing ( $ message ) { $ userId = $ message [ 'from' ] [ 'id' ] ; $ username = strtolower ( $ message [ 'from' ] [ 'username' ] ) ; $ query = $ this -> database -> pdo -> prepare ( 'SELECT username FROM users WHERE userId = :userId' ) ; $ query -> bindParam ( ':userId' , $ userId ) ; $ query -> execute ( ) ; $ user = $ query -> fetch ( PDO :: FETCH_ASSOC ) ?? false ; // If the user is not registered. if ( ! $ user ) { $ query = $ this -> database -> pdo -> prepare ( 'INSERT INTO users (userId, username, points) VALUES(:userId, :username, 0)' ) ; $ query -> bindParam ( ':userId' , $ userId ) ; $ query -> bindParam ( ':username' , $ username ) ; $ query -> execute ( ) ; return ; } // If the username changed. if ( $ username !== $ user [ 'username' ] ) { $ query = $ this -> database -> pdo -> prepare ( 'UPDATE users SET username = :username WHERE userId = :userId' ) ; $ query -> bindParam ( ':userId' , $ userId ) ; $ query -> bindParam ( ':username' , $ username ) ; $ query -> execute ( ) ; } }
Checks if the user is already registered.
If yes, checks if her username is changed and update it.
Otherwise, register the new user.

@param $db The reference to the PDO object.
@param $message The incoming message.

@return void
Checks if the user is already registered . If yes checks if her username is changed and update it . Otherwise register the new user .