Skip to content

Commit ea9775a

Browse files
committed
added error builder
1 parent b28cea2 commit ea9775a

File tree

3 files changed

+85
-14
lines changed

3 files changed

+85
-14
lines changed

Diff for: Plugin/ErrorBuilder.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace JMS\Payment\CoreBundle\Plugin;
4+
5+
use JMS\Payment\CoreBundle\Plugin\Exception\InvalidPaymentInstructionException;
6+
7+
/**
8+
* Convenience class for building up error messages.
9+
*
10+
* @author Johannes M. Schmitt <[email protected]>
11+
*/
12+
class ErrorBuilder
13+
{
14+
private $dataErrors = array();
15+
private $globalErrors = array();
16+
17+
public function addDataError($field, $messageTemplate)
18+
{
19+
$this->dataErrors[$field] = $messageTemplate;
20+
}
21+
22+
public function addGlobalError($messageTemplate)
23+
{
24+
$this->globalErrors[] = $messageTemplate;
25+
}
26+
27+
public function hasErrors()
28+
{
29+
return $this->dataErrors || $this->globalErrors;
30+
}
31+
32+
public function getException()
33+
{
34+
$ex = new InvalidPaymentInstructionException();
35+
$ex->setDataErrors($this->dataErrors);
36+
$ex->setGlobalErrors($this->globalErrors);
37+
38+
return $ex;
39+
}
40+
}

Diff for: Resources/doc/plugins.rst

+11-14
Original file line numberDiff line numberDiff line change
@@ -235,29 +235,30 @@ We are now going to implement the ``checkPaymentInstruction`` method for our for
235235
236236
use JMS\Payment\CoreBundle\Plugin\AbstractPlugin;
237237
use JMS\Payment\CoreBundle\Model\PaymentInstructionInterface;
238-
use JMS\Payment\CoreBundle\Plugin\Exception\InvalidPaymentInstructionException;
238+
use JMS\Payment\CoreBundle\Plugin\ErrorBuilder;
239239
240240
class CreditCardPlugin extends AbstractPlugin
241241
{
242242
public function checkPaymentInstruction(PaymentInstructionInterface $instruction)
243243
{
244-
$errors = array();
244+
$errorBuilder = new ErrorBuilder();
245245
$data = $instruction->getExtendedData();
246246
247247
if (!$data->get('holder')) {
248-
$errors['holder'] = 'form.error.required';
248+
$errorBuilder->addDataError('holder', 'form.error.required');
249249
}
250250
if (!$data->get('number')) {
251-
$errors['number'] = 'form.error.required';
251+
$errorBuilder->addDataError('number', 'form.error.required');
252+
}
253+
254+
if ($instruction->getAmount() > 10000) {
255+
$errorBuilder->addGlobalError('form.error.credit_card_max_limit_exceeded');
252256
}
253257
254258
// more checks here ...
255259
256-
if (count($errors) > 0) {
257-
$ex = new InvalidPaymentInstructionException('The payment instruction is invalid.');
258-
$ex->setDataErrors($errors);
259-
260-
throw $ex;
260+
if ($errorBuilder->hasErrors()) {
261+
throw $errorBuilder->getException();
261262
}
262263
}
263264
@@ -270,9 +271,5 @@ We are now going to implement the ``checkPaymentInstruction`` method for our for
270271
.. note ::
271272
272273
The data errors are automatically mapped to the respective fields of the form.
273-
274-
.. tip ::
274+
Global errors are applied to the form itself.
275275
276-
If you have an error that is not related to a specific fields, but rather to several
277-
fields, or the payment instruction as a whole, use ``setGlobalErrors`` instead of
278-
``setDataErrors``.

Diff for: Tests/Plugin/ErrorBuilderTest.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace JMS\Payment\CoreBundle\Tests\Plugin;
4+
5+
use JMS\Payment\CoreBundle\Plugin\ErrorBuilder;
6+
7+
class ErrorBuilderTest extends \PHPUnit_Framework_TestCase
8+
{
9+
private $builder;
10+
11+
public function testHasErrors()
12+
{
13+
$this->assertFalse($this->builder->hasErrors());
14+
15+
$this->builder->addGlobalError('foo');
16+
$this->assertTrue($this->builder->hasErrors());
17+
}
18+
19+
public function testGetException()
20+
{
21+
$this->builder->addDataError('foo', 'bar');
22+
$this->builder->addGlobalError('baz');
23+
24+
$ex = $this->builder->getException();
25+
$this->assertInstanceOf('JMS\Payment\CoreBundle\Plugin\Exception\InvalidPaymentInstructionException', $ex);
26+
$this->assertSame(array('foo' => 'bar'), $ex->getDataErrors());
27+
$this->assertSame(array('baz'), $ex->getGlobalErrors());
28+
}
29+
30+
protected function setUp()
31+
{
32+
$this->builder = new ErrorBuilder();
33+
}
34+
}

0 commit comments

Comments
 (0)