Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample code #22

Open
abdfork opened this issue Dec 14, 2016 · 1 comment
Open

Sample code #22

abdfork opened this issue Dec 14, 2016 · 1 comment
Assignees
Labels

Comments

@abdfork
Copy link

abdfork commented Dec 14, 2016

Is there any sample code creating a ProcessContext, Events, and starting the workflow?

@iteman iteman self-assigned this Dec 15, 2016
@iteman
Copy link
Member

iteman commented Dec 15, 2016

If you directly use Workflower and PHPMentorsWorkflowerBundle, a code for starting processes of a workflow would be as the following:

// ...

class LoanRequestProcessStartUsecase implements CommandUsecaseInterface, ProcessAwareInterface
{
    // ...

    /**
     * @var EntityManagerInterface
     */
    private $entityManager;

    /**
     * @var Process
     */
    private $process;

    // ...

    /**
     * {@inheritdoc}
     */
    public function setProcess(Process $process)
    {
        $this->process = $process;
    }

    /**
     * {@inheritdoc}
     *
     * @return void
     */
    public function run(EntityInterface $entity)
    {
        assert($entity instanceof EventContextInterface);
        assert($entity->getProcessContext() instanceof LoanRequestProcess);

        $this->process->start($entity);

        // ...
        $loanRequestProcess = $entity->getProcessContext(); /* @var $loanRequestProcess LoanRequestProcess */
        $loanRequestProcess->setCurrentTask($loanRequestProcess->getWorkflow()->getCurrentFlowObject()->getId()); // set some information for query

        $this->entityManager->beginTransaction();
        try {
            $this->loanRequestProcessRepository->add($loanRequestProcess);
            $this->entityManager->flush();
            $this->entityManager->commit();
        } catch (\Exception $e) {
            $this->entityManager->rollback();
            throw $e;
        }
    }
}

In these cases, the usecase object would be called from controllers, CLI commands, and event listeners as the following:

// ...

class LoanRequestProcessListener
{
    // ...

    /**
     * @var LoanRequestProcessStartUsecase
     */
    private $loanRequestProcessStartUsecase;

    // ...

    /**
     * @param LoanRequestApplicationEvent $event
     */
    public function onSuccess(LoanRequestApplicationEvent $event)
    {
        try {
            $this->loanRequestProcessStartUsecase->run(new EventContext('Start', new LoanRequestProcess($event->getLoanRequestApplication()));
        } catch (\Exception $e) {
            // ...
        }

        // ...
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants