src/bbn/appui/observer.php
public function add(array $cfg): ?string
  {
    if (
      $this->id_user &&
      (null !== $cfg['request']) &&
      $this->check()
    ){
      $t = new bbn\util\timer();
      $t->start();
      if ( is_string($cfg['request']) ){
        $params = self::sanitize_params($cfg['params'] ?? []);
        $request = trim($cfg['request']);
      }
      else if ( is_array($cfg['request']) ){
        $params = null;
        $request = $cfg['request'];
      }
      else{
        return null;
      }
      $res = $this->_exec($request, $params);
      $duration = (int)ceil($t->stop() * 1000);
      if ( is_array($request) ){
        $request = json_encode($request);
      }
      $id_alias = $this->_get_id($request, $params);
      //die(var_dump($id_alias, $this->db->last()));
      // If it is a public observer it will be the id_alias and the main observer
      if (
        !$id_alias &&
        !empty($cfg['public']) &&
        $this->db->insert('bbn_observers', [
          'request' => $request,
          'params' => $params ?: null,
          'name' => $cfg['name'] ?? null,
          'duration' => $duration,
          'id_user' => null,
          'public' => 1,
          'result' => $res
        ])
      ){
        $id_alias = $this->db->last_id();
      }
      // Getting the ID of the observer corresponding to current user
      if ( $id_obs = $this->_get_id_from_user($request, $params) ){
        //
        $this->check_result($id_obs);
        return $id_obs;
      }
      else if ( $id_alias ){
        if ( $this->db->insert('bbn_observers', [
          'id_user' => $this->id_user,
          'public' => 0,
          'id_alias' => $id_alias,
          'next' => null,
          'result' => $res
        ]) ){
          return $this->db->last_id();
        }
      }
      else{
        if ( $this->db->insert('bbn_observers', [
          'request' => $request,
          'params' => $params ?: null,
          'name' => $cfg['name'] ?? null,
          'duration' => $duration,
          'id_user' => $this->id_user,
          'public' => 0,
          'result' => $res
        ]) ){
          return $this->db->last_id();
        }
      }
    }
    return null;
  }
public function add(array $cfg): ?string
  {
    if (
      $this->id_user &&
      (null !== $cfg['request']) &&
      $this->check()
    ){
      $t = new bbn\util\timer();
      $t->start();
      if ( is_string($cfg['request']) ){
        $params = self::sanitize_params($cfg['params'] ?? []);
        $request = trim($cfg['request']);
      }
      else if ( is_array($cfg['request']) ){
        $params = null;
        $request = $cfg['request'];
      }
      else{
        return null;
      }
      $res = $this->_exec($request, $params);
      $duration = (int)ceil($t->stop() * 1000);
      if ( is_array($request) ){
        $request = json_encode($request);
      }
      $id_alias = $this->_get_id($request, $params);
      //die(var_dump($id_alias, $this->db->last()));
      // If it is a public observer it will be the id_alias and the main observer
      if (
        !$id_alias &&
        !empty($cfg['public']) &&
        $this->db->insert('bbn_observers', [
          'request' => $request,
          'params' => $params ?: null,
          'name' => $cfg['name'] ?? null,
          'duration' => $duration,
          'id_user' => null,
          'public' => 1,
          'result' => $res
        ])
      ){
        $id_alias = $this->db->last_id();
      }
      // Getting the ID of the observer corresponding to current user
      if ( $id_obs = $this->_get_id_from_user($request, $params) ){
        //
        $this->check_result($id_obs);
        return $id_obs;
      }
      else if ( $id_alias ){
        if ( $this->db->insert('bbn_observers', [
          'id_user' => $this->id_user,
          'public' => 0,
          'id_alias' => $id_alias,
          'next' => null,
          'result' => $res
        ]) ){
          return $this->db->last_id();
        }
      }
      else{
        if ( $this->db->insert('bbn_observers', [
          'request' => $request,
          'params' => $params ?: null,
          'name' => $cfg['name'] ?? null,
          'duration' => $duration,
          'id_user' => $this->id_user,
          'public' => 0,
          'result' => $res
        ]) ){
          return $this->db->last_id();
        }
      }
    }
    return null;
  }
public function add ( array $ cfg ) : ? string { if ( $ this -> id_user && ( null !== $ cfg [ 'request' ] ) && $ this -> check ( ) ) { $ t = new bbn \ util \ timer ( ) ; $ t -> start ( ) ; if ( is_string ( $ cfg [ 'request' ] ) ) { $ params = self :: sanitize_params ( $ cfg [ 'params' ] ?? [ ] ) ; $ request = trim ( $ cfg [ 'request' ] ) ; } else if ( is_array ( $ cfg [ 'request' ] ) ) { $ params = null ; $ request = $ cfg [ 'request' ] ; } else { return null ; } $ res = $ this -> _exec ( $ request , $ params ) ; $ duration = ( int ) ceil ( $ t -> stop ( ) * 1000 ) ; if ( is_array ( $ request ) ) { $ request = json_encode ( $ request ) ; } $ id_alias = $ this -> _get_id ( $ request , $ params ) ; //die(var_dump($id_alias, $this->db->last())); // If it is a public observer it will be the id_alias and the main observer if ( ! $ id_alias && ! empty ( $ cfg [ 'public' ] ) && $ this -> db -> insert ( 'bbn_observers' , [ 'request' => $ request , 'params' => $ params ? : null , 'name' => $ cfg [ 'name' ] ?? null , 'duration' => $ duration , 'id_user' => null , 'public' => 1 , 'result' => $ res ] ) ) { $ id_alias = $ this -> db -> last_id ( ) ; } // Getting the ID of the observer corresponding to current user if ( $ id_obs = $ this -> _get_id_from_user ( $ request , $ params ) ) { // $ this -> check_result ( $ id_obs ) ; return $ id_obs ; } else if ( $ id_alias ) { if ( $ this -> db -> insert ( 'bbn_observers' , [ 'id_user' => $ this -> id_user , 'public' => 0 , 'id_alias' => $ id_alias , 'next' => null , 'result' => $ res ] ) ) { return $ this -> db -> last_id ( ) ; } } else { if ( $ this -> db -> insert ( 'bbn_observers' , [ 'request' => $ request , 'params' => $ params ? : null , 'name' => $ cfg [ 'name' ] ?? null , 'duration' => $ duration , 'id_user' => $ this -> id_user , 'public' => 0 , 'result' => $ res ] ) ) { return $ this -> db -> last_id ( ) ; } } } return null ; }
Adds a new observer and returns its id or the id of an existing one.

@param array $cfg
@return null|string
Adds a new observer and returns its id or the id of an existing one .