检查内部通信中是否存在用户

时间:2017-04-06 13:46:41

标签: php symfony intercom

我想在同一时间从我的数据库和对讲机中删除用户 但我想检查这个用户是否存在于对讲机中:

public function deleteUserAction(Request $request, $id)
{
    $doctrine = $this->getDoctrine();
    $em = $doctrine->getManager();
    $intercom = IntercomBasicAuthClient::factory(array(
        'app_id' => 'xxxxxx',
        'api_key' => 'xxxxxxxxxxxx'
    ));   
    $user = $doctrine->getRepository('ZaidUserBundle:User')->find($id);

    if($user) {
        //delete user from intercom
        //I want some condition here 
        $intercom->deleteUser(array("email" => $user->getEmail()));
        //delete user from database
        $em->remove($user);
        $em->flush();
    }

    return new Response(200);
}

2 个答案:

答案 0 :(得分:1)

doc here所述,您应该通过电子邮件简单地检索用户:

// Find user by email
$intercom->getUser(array("email" => "bob@example.com"));

希望这个帮助

答案 1 :(得分:0)

谢谢Matteo 这个解决方案对我有用

public function deleteUserAction(Request $request, $id)
{
    $doctrine = $this->getDoctrine();
    $em = $doctrine->getManager();
    $intercom = IntercomBasicAuthClient::factory(array(
        'app_id' => 'xxxxxx',
        'api_key' => 'xxxxxxxxxxxx'
    ));   
    $user = $doctrine->getRepository('ZaidUserBundle:User')->find($id);
    $existe = 1;
    if($user) {
        //delete user from intercom
        //I want some condition here 
         try{
        $intercom->deleteUser(array("email" => $user->getEmail()));

        } catch (\Intercom\Exception\IntercomException $e) {
                $existe = 0;
            }
        //delete user from database
if($existe==1){
        $em->remove($user);
        $em->flush();
}
    }

    return new Response(200);
}