findByIdAndDelete() public method

public findByIdAndDelete ( $id )
Exemplo n.º 1
0
 /**
  * Deletes the contact for the given id.
  *
  * @return \Closure
  */
 public function delete()
 {
     /*
      * TODO: https://github.com/sulu-io/sulu/pull/1171
      * This method needs to be refactored since in the first
      * iteration the logic was just moved from the Controller
      * to this class due to better reusability.
      */
     $delete = function ($id) {
         /** @var Contact $contact */
         $contact = $this->contactRepository->findByIdAndDelete($id);
         if (!$contact) {
             throw new EntityNotFoundException($this->contactRepository->getClassName(), $id);
         }
         $addresses = $contact->getAddresses();
         /** @var Address $address */
         foreach ($addresses as $address) {
             if (!$address->hasRelations()) {
                 $this->em->remove($address);
             }
         }
         $phones = $contact->getPhones()->toArray();
         /** @var Phone $phone */
         foreach ($phones as $phone) {
             if ($phone->getAccounts()->count() == 0 && $phone->getContacts()->count() == 1) {
                 $this->em->remove($phone);
             }
         }
         $emails = $contact->getEmails()->toArray();
         /** @var Email $email */
         foreach ($emails as $email) {
             if ($email->getAccounts()->count() == 0 && $email->getContacts()->count() == 1) {
                 $this->em->remove($email);
             }
         }
         $urls = $contact->getUrls()->toArray();
         /** @var Url $url */
         foreach ($urls as $url) {
             if ($url->getAccounts()->count() == 0 && $url->getContacts()->count() == 1) {
                 $this->em->remove($url);
             }
         }
         $faxes = $contact->getFaxes()->toArray();
         /** @var Fax $fax */
         foreach ($faxes as $fax) {
             if ($fax->getAccounts()->count() == 0 && $fax->getContacts()->count() == 1) {
                 $this->em->remove($fax);
             }
         }
         $this->em->remove($contact);
         $this->em->flush();
     };
     return $delete;
 }