PHP - conditions in symfony

44 Views Asked by At

I wrote a function where I pass an id parameter trough ItemRepository, and then I check if that item already belongs to another user. I think I made a mistake because every time it returns Exception I defined.

My Service:

public function itemCheck(User $user, $id)
{
    /** @var Item $item */
    $item = $this->getItemRepository()->find($id);

    if(!$item) {
        throw new AppClientException('Item not found!');
    }
    if($user->getId()) {
        if($user->getId() == $item->getId()) {
            return true;
        } else {
            throw new AppClientException('This item belongs to another user!!');
        }
    }
}
1

There are 1 best solutions below

3
JessGabriel On

You don't need to pass User as parameters anymore here. Make it simple Just do like :

public function itemCheck($id)
{
    /** @var Item $item */
    $item = $this->getItemRepository()->find($id);

    //no record found in database, its okay
    if(!$item) {
        return true;
    }

    return false;
}