Skip to content
This repository has been archived by the owner on Nov 27, 2020. It is now read-only.

Commit

Permalink
Merge pull request #33 from stefliekens/delete-exception
Browse files Browse the repository at this point in the history
Catch exceptions in deleteService
  • Loading branch information
veewee committed Jun 17, 2015
2 parents d4d482f + 4fd3076 commit f6bbb72
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 7 deletions.
3 changes: 1 addition & 2 deletions spec/Phpro/SmartCrud/Gateway/DoctrineCrudGatewaySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ public function it_should_delete_entity($objectManager, $entity)
public function it_should_not_delete_invalid_entity($objectManager, $entity)
{
$objectManager->flush()->willThrow('\Exception');

$this->delete($entity, array())->shouldReturn(false);
$this->shouldThrow('\Exception')->duringDelete($entity, array());
}
}
8 changes: 8 additions & 0 deletions spec/Phpro/SmartCrud/Service/SmartServiceResultSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,12 @@ public function it_should_have_a_list()
$this->setList($list)->shouldReturn($this);
$this->getList()->shouldReturn($list);
}

public function it_should_have_messages()
{
$messages = array('msg1');
$this->setMessages($messages)->shouldReturn($this);
$this->addMessage('msg2')->shouldReturn($this);
$this->getMessages()->shouldReturn(array('msg1', 'msg2'));
}
}
5 changes: 2 additions & 3 deletions src/Phpro/SmartCrud/Gateway/DoctrineCrudGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
class DoctrineCrudGateway
implements ObjectManagerAwareInterface, CrudGatewayInterface
{

const TYPE_ORM = 'ORM';
const TYPE_ODM = 'ODM';

Expand Down Expand Up @@ -133,8 +132,8 @@ public function update($entity, $parameters)
/**
* @param $entity
* @param $id
*
* @return bool
* @throws \Exception
*/
public function delete($entity, $id)
{
Expand All @@ -143,7 +142,7 @@ public function delete($entity, $id)
$em->remove($entity);
$em->flush();
} catch (\Exception $e) {
return false;
throw new \Exception($e->getMessage());
}

return true;
Expand Down
10 changes: 8 additions & 2 deletions src/Phpro/SmartCrud/Service/DeleteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ public function run($id = null, $data = null)
}

$em->trigger($this->createEvent(CrudEvent::BEFORE_DELETE, $entity));
$deleted = $this->getGateway()->delete($entity, $data);
try {
$deleted = $this->getGateway()->delete($entity, $data);
$result->setSuccess($deleted);
} catch (\Exception $exception) {
$result->setSuccess(false);
$result->addMessage($exception->getMessage());
return $result;
}
$em->trigger($this->createEvent(CrudEvent::AFTER_DELETE, $entity));

$result->setSuccess($deleted);
return $result;
}
}
33 changes: 33 additions & 0 deletions src/Phpro/SmartCrud/Service/SmartServiceResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class SmartServiceResult
*/
private $list = array();

/**
* @var array
*/
private $messages = array();

/**
* @param mixed|null $form
*/
Expand Down Expand Up @@ -109,4 +114,32 @@ public function isSuccessFull()
{
return $this->success;
}

/**
* @return array
*/
public function getMessages()
{
return $this->messages;
}

/**
* @param $message
* @return $this
*/
public function addMessage($message)
{
$this->messages[] = $message;
return $this;
}

/**
* @param array $messages
* @return $this
*/
public function setMessages($messages)
{
$this->messages = $messages;
return $this;
}
}

0 comments on commit f6bbb72

Please sign in to comment.