Skip to content

Instantly share code, notes, and snippets.

@jsor
Last active December 17, 2015 09:36
Show Gist options
  • Save jsor/b77fe49e08b8c9ffb7e9 to your computer and use it in GitHub Desktop.
Save jsor/b77fe49e08b8c9ffb7e9 to your computer and use it in GitHub Desktop.
ReactPHP needs improving, any ideas?
<?php
namespace App\ProcessManager\Billing;
use App\Read\Billing\Queries\GetDefaultPlan;
use App\Read\Identity\Queries\UserWithId;
use App\Read\Identity\UserDto;
use App\Services\Bus\BillingCommandBus;
use App\Services\Bus\BillingQueryBus;
use App\Services\Bus\IdentityQueryBus;
use ProjectName\Billing\Model\Command\OpenAccountForUser;
use ProjectName\Identity\Models\Events\UserRegistered;
use Rhumsaa\Uuid\Uuid;
use React\Promise;
final class SetupBillingAccountForRegisteredUser
{
/** @var BillingCommandBus */
private $billingCommandBus;
/** @var BillingQueryBus */
private $billingQueryBus;
/** @var IdentityQueryBus */
private $identityQueryBus;
/**
* @param BillingCommandBus $billingCommandBus
* @param BillingQueryBus $billingQueryBus
* @param IdentityQueryBus $identityQueryBus
*/
public function __construct(BillingCommandBus $billingCommandBus,
BillingQueryBus $billingQueryBus,
IdentityQueryBus $identityQueryBus)
{
$this->billingCommandBus = $billingCommandBus;
$this->billingQueryBus = $billingQueryBus;
$this->identityQueryBus = $identityQueryBus;
}
/**
* @param UserRegistered $event
*/
public function __invoke(UserRegistered $event)
{
$defaultPlan = $this
->billingQueryBus
->dispatch(new GetDefaultPlan);
$aUser = $this
->identityQueryBus
->dispatch(UserWithId::fromString($event->userId()));
Promise\all(['defaultPlan' => $defaultPlan, 'aUser' => $aUser])
->done(function (array $result) {
$this->openAccountForUser(
$result['defaultPlan'],
$result['aUser']
);
});
}
private function openAccountForUser(PlanDto $plan, UserDto $aUser)
{
$command = OpenAccountForUser::withData(
Uuid::uuid4()->toString(),
$aUser->getUserId(),
$aUser->getName(),
$aUser->getEmail(),
$aUser->getAddress(),
$plan->getPlanId()
);
$this->billingCommandBus->dispatch($command);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment