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

How to use $check in InterruptibleProcessor.php? #51

Open
wowcut opened this issue Dec 20, 2018 · 1 comment
Open

How to use $check in InterruptibleProcessor.php? #51

wowcut opened this issue Dec 20, 2018 · 1 comment

Comments

@wowcut
Copy link

wowcut commented Dec 20, 2018

Hi first let me thank you for this code, seems like this is exactly what I was looking for!
I just wanted to implement some kind of dependency for the individual stages of a pipeline with some extensive try catch error handling but then stumbled across the InterruptibleProcessor.php and I was wondering if there might be a better way to do that, but I am not sure how to use the $check callable - do I understand the code right that it allows for one $check callable for each pipeline and not for one $check for each $stage?

I just want to let pipelines stop or skip or trigger a different pipeline based on the results of one stage.

Also I am wondering: what would be the best way to build fallback chains? Something like "if stage_x fails, try again with $stage_y" or "if $stage_x produces result x skip to stage_z"?

I am just experimenting to learn if it might make sense to transform an existing large if-then-else monstrosity into a few pipelines and do not immediately see how to implement such kind of fallbacks. Also of course I would like to avoid writing lots of try catch error handling code - the result should be less code than the if-then-else horror I would like to replace... :)

If you could find the time for providing a little example how you would implement such a thing, it would be a great help! Thanks for your attention!

@AliasAPI
Copy link

// This is one way to check for errors and skip to the end of a pipeline during processing . . .

// Create the Pipeline
$pipeline = (new Pipeline(
// Set the processor to InterruptibleProcessor (instead of FingersCrossed)
new InterruptibleProcessor(
// Create a class GetOkay->__invoke() that calls $payload->getOkay()
// Pass GetOkay into InterruptibleProcessor so it knows where to check.
new GetOkay
)
))
// Create a Payload class with setOkay(), getOkay(), and a private property $okay
// Note: Line 25 InterruptibleProcessor is " if (true !== $check($payload)) { "
->pipe(new CreatePayload)
// Create a CheckBeforeFirstAction class to update $payload->setOkay() as needed.
// This class can check to make sure all requirements are OK to run FirstAction.
// Tip: Every class in the pipeline should (modify and) return the $payload object.
->pipe(new CheckBeforeFirstAction)
// CheckBeforeFirstAction(Payload $payload) returns the $payload object.
// The InterruptibleProcessor will check for an error using GetOkay->_invoke()
// If $payload->okay is still set to true the FirstAction will run
->pipe(new FirstAction)
// Create a CheckBeforeSecondAction class to $payload->setOkay() as needed.
->pipe(new CheckBeforeSecondAction)
// Set $payload->setOkay('false') inside CheckBeforeSecondAction as a test.
// The InterruptibleProcessor will check for an error using GetOkay->_invoke()
// The SecondAction will NOT be run since $payload->okay = false.
->pipe(new SecondAction);
// The $payload will be returned from InterruptibleProcessor.

// wowcut: Set a property in the $payload to specify which stages should run.
// I don't think you will reduce the if-then-else, but you can hide them in Check classes

Thanks to creator and maintainer of the Pipeline!

Drew

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

No branches or pull requests

2 participants