src/CMDBObject.php
public function upsert($type, $title, array $attributes = []) {
        $cmdbObjects = new CMDBObjects($this->api);

        $filter = [
            'title' => $title,
            'type' => $type
        ];

        $result = $cmdbObjects->read($filter);

        switch (count($result)) {
            case 0:
                return $this->create($type, $title, $attributes);
            case 1:
                if (!array_key_exists(0, $result) ||
                    !array_key_exists('id', $result[0])) {
                    throw new RuntimeException('Bad result');
                }

                return $result[0]['id'];
            default:
                throw new RuntimeException(sprintf(
                    'Found %s objects',
                    count($result)
                ));
        }
    }
public function upsert($type, $title, array $attributes = []) {
        $cmdbObjects = new CMDBObjects($this->api);

        $filter = [
            'title' => $title,
            'type' => $type
        ];

        $result = $cmdbObjects->read($filter);

        switch (count($result)) {
            case 0:
                return $this->create($type, $title, $attributes);
            case 1:
                if (!array_key_exists(0, $result) ||
                    !array_key_exists('id', $result[0])) {
                    throw new RuntimeException('Bad result');
                }

                return $result[0]['id'];
            default:
                throw new RuntimeException(sprintf(
                    'Found %s objects',
                    count($result)
                ));
        }
    }
public function upsert ( $ type , $ title , array $ attributes = [ ] ) { $ cmdbObjects = new CMDBObjects ( $ this -> api ) ; $ filter = [ 'title' => $ title , 'type' => $ type ] ; $ result = $ cmdbObjects -> read ( $ filter ) ; switch ( count ( $ result ) ) { case 0 : return $ this -> create ( $ type , $ title , $ attributes ) ; case 1 : if ( ! array_key_exists ( 0 , $ result ) || ! array_key_exists ( 'id' , $ result [ 0 ] ) ) { throw new RuntimeException ( 'Bad result' ) ; } return $ result [ 0 ] [ 'id' ] ; default : throw new RuntimeException ( sprintf ( 'Found %s objects' , count ( $ result ) ) ) ; } }
Create new object or fetch existing one based on its title and type

@param int|string $type Object type identifier or its constant
@param string $title Object title
@param array $attributes (Optional) additional common attributes
('category', 'purpose', 'cmdb_status', 'description')

@return int Object identifier

@throws Exception on error
Create new object or fetch existing one based on its title and type