Sylius/SyliusFlowBundle — pull request #23 by zachbadgett (2013-08-05 23:44:29+00:00)
Title: [WIP] Process validation

Currently the 'Coordinator' in FlowBundle will throw a NotFoundHttpException if a Scenario is not valid. This limits flexibility and can be handled more effectively.

This commit (mostly for poc) would allow more extendable validation for scenarios and will couple with a commit to Sylius/Sylius that will use the new feature.

```php
// ../CoreBundle/Checkout/CheckoutProcessScenario.php
$builder
->setDisplayRoute('sylius_checkout_display')
->setForwardRoute('sylius_checkout_forward')
->setRedirect('sylius_homepage')
->validate(new ProcessValidator(function () use ($cart) {
return !$cart->isEmpty();
}, 'Nothing to checkout.', '@SyliusWeb/Frontend/error.html.twig'))
;
```
```twig
{# ../WebBundle/Resources/views/Frontend/error.html.twig #}
{% extends 'SyliusWebBundle:Frontend:layout.html.twig' %}

{% block content %}

Something went wrong!

{{ error }}

{% endblock %}
```

The above blocks are using the default validator, if a cart is empty the page will display the error instead of throwing a NotFoundHttpException.

Extending process validator:
```php
// ../CoreBundle/Checkout/Validator/CheckoutProcessValidator.php
namespace Sylius\Bundle\CoreBundle\Checkout\Validator;

use Sylius\Bundle\FlowBundle\Validator\ProcessValidator;

class CheckoutProcessValidator extends ProcessValidator
{
private $cart;

public function __construct($cart)
{
$this->cart = $cart;
$this->setMessage('Nothing to checkout.');
$this->setTemplate('@SyliusWeb/Frontend/error.html.twig');
}

/**
* {@inheritdoc}
*/
public function isValid()
{
return (!$this->cart->isEmpty()) ? true : false;
}
}
```

```php
// ../CoreBundle/Checkout/CheckoutProcessScenario.php
$builder
->setDisplayRoute('sylius_checkout_display')
->setForwardRoute('sylius_checkout_forward')
->setRedirect('sylius_homepage')
->validate(new CheckoutProcessValidator($cart));
```

This would display the same message as before.

Suggestions and changes welcomed!

TODO:
Fix tests
Write test for validator