Skip to content

Instantly share code, notes, and snippets.

ENV.contentSecurityPolicy = {
'default-src': "'none'",
'script-src': "'self' http://192.168.56.120",
'font-src': "'self'",
'connect-src': "'self' ws://192.168.56.120",
'img-src': "'self'",
'style-src': "'self'",
'media-src': "'self'"
};
<?php
class UserHydrator
{
private $user;
private $resolver;
public function __construct(User $user)
{
<?php
use App\Models\Company;
use Illuminate\Database\Eloquent\Builder;
class CompanyController extends \BaseController
{
/**
* Display a listing of the resource.
*

Handling validation with a command bus

I've been struggling with coming up with a way to handle validation in my command bus architecture.

Throwing validation exceptions in middleware felt wrong. Those aren't really exceptional occurences. Validating data outside of the bus(in controllers/console commands) felt wrong. Validation could be easily side-stepped. Validating in handlers felt cluttered and seemed to violate the SRP.

I think I've finally come up with something I'm fairly happy with though.

What I tried first

<?php
use SimpleBus\Message\Message;
interface CommandPipe
{
/**
* @param Message $command
* @param callable $next
*/
<?php
namespace Acme\Project\Infrastructure\AppBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
/**
<?php
namespace Acme\Project\Infrastructure\AppBundle\Middleware;
use SimpleBus\Message\Handler\MessageHandler;
use SimpleBus\Message\Message;
class CommandHandlerSupportingLazyMiddleware implements MessageHandler
{
/**
<?php
namespace Acme\AwesomeApp\Infrastructure\UserBundle\Features\Context;
use Behat\Behat\Context\Context;
use Behat\Behat\Context\Environment\InitializedContextEnvironment;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Acme\AwesomeApp\Domain\User\Entity\User;
use Acme\AwesomeApp\Domain\User\Security\PasswordEncoder;

Return new ArrayCollections from getters

Do you have some business logic tied up in your adder methods? Something like this maybe.

class Cart
{
	private $items;

	public function __construct()

Start throwing "not found" exceptions in your repositories

99% of the time you're going to want to bail if you're expecting to find an entity by a specific key. Having code like the two private methods in the old code below in every single one of your handlers is crazy and not DRY.

I think the idea of "only throw exceptions when something exceptional happens" is too generic and doesn't take context into consideration.

A repository's only job is to find entities. If you give it a key and it can't find it, i think that's exceptional(in the context of the repository).

If it actually is expected in your client code it's not a big deal, catch the EntityNotFoundException and carry on.