/**
  * Handle collection
  *
  * @param Transaction $transaction transaction
  * @param mixed       $data        data
  *
  * @return CollectionResponse
  */
 private function handleCollection(Transaction $transaction, $data)
 {
     $data = new CollectionResponse($data);
     if ($this->errorBuilder->hasErrors()) {
         $this->errorBuilder->setTransactionErrors($transaction);
         foreach ($this->errorBuilder->getErrors() as $error) {
             $errorCode = $error->getCode();
         }
     }
     $status = !$this->errorBuilder->hasErrors() ? Transaction::STATUS_OK : $errorCode;
     $transaction->setStatus($status);
     $transaction->setSuccess(!$this->errorBuilder->hasErrors());
     return $data;
 }
 /**
  * Handle individual entity
  *
  * @param Transaction         $transaction Transaction
  * @param CrudEntityInterface $entity      Entity
  *
  * @return void
  */
 private function handleEntity($transaction, CrudEntityInterface $entity)
 {
     $transaction->setRequestSource(Transaction::SOURCE_SERVICE);
     $transaction->setId(UUID::generate());
     $transaction->setRequestId(microtime());
     $transaction->setRelatedIds($entity->getIds());
     $errors = $this->errorBuilder->getEntityErrors($entity->getPrimaryKey());
     $messages = new ArrayCollection();
     $success = $errors->isEmpty();
     $status = $success ? Transaction::STATUS_CREATED : Transaction::STATUS_CONFLICT;
     $transaction->setStatus($status);
     $transaction->setSuccess($success);
     if ($success) {
         $this->noticeBuilder->addSuccess();
     } else {
         $messages->set('errors', $errors);
         $this->noticeBuilder->addFail();
     }
     $transaction->setMessages($messages);
     $this->entityManager->persist($transaction);
     $entity->setTransaction($transaction);
 }