Skip to content

Instantly share code, notes, and snippets.

@jdeoliveira
Created June 29, 2012 16:26
Show Gist options
  • Save jdeoliveira/3018971 to your computer and use it in GitHub Desktop.
Save jdeoliveira/3018971 to your computer and use it in GitHub Desktop.
Complex flow with PHP as the scripting language
<flow name="flow" doc:name="flow">
<http:inbound-endpoint exchange-pattern="request-response"
host="localhost" port="8081" path="runPHP" doc:name="HTTP" />
<object-to-string-transformer doc:name="Object to String" />
<scripting:component doc:name="Analyze credit request">
<scripting:script engine="php">
<scripting:text>
<![CDATA[
<?php
$log->info("Hello from PHP, mule!");
$request = json_decode($payload, true);
if (strcmp(substr($request["customerId"], 0, 3), "ABC") == 0) $factor = 3;
else if (strcmp(substr($request["customerId"], 0, 3), "DEF") == 0) $factor = 2;
if ($request["requestedAmount"] > ($factor * $request["averageIncome"])) {
$message->setOutboundProperty("outcome", "rejected");
$request["maxAmount"] = ($factor * $request["averageIncome"]);
} else {
$message->setOutboundProperty("outcome", "approved");
}
return json_encode($request);
?>
]]>
</scripting:text>
</scripting:script>
</scripting:component>
<choice doc:name="Choice">
<when expression="outcome=approved" evaluator="header">
<processor-chain>
<logger
message="REQUEST APPROVED: #[json:customerId] for #[json:requestedAmount]"
level="INFO" doc:name="Approved request logger" />
<scripting:component doc:name="Prepare approval response">
<scripting:script engine="php">
<scripting:text>
<![CDATA[
<?php
$response["outcome"] = $message->getProperty("outcome");
return json_encode($response);
?>
]]>
</scripting:text>
</scripting:script>
</scripting:component>
</processor-chain>
</when>
<when expression="outcome=rejected" evaluator="header">
<processor-chain>
<logger
message="REJECTED REQUEST: #[json:customerId] for #[json:requestedAmount], max allowable: #[json:maxAmount]"
level="INFO" doc:name="Rejected request logger" />
<scripting:component doc:name="Prepare rejection response">
<scripting:script engine="php">
<scripting:text>
<![CDATA[
<?php
$request = json_decode($payload, true);
$response["outcome"] = $message->getProperty("outcome");
$response["maxAmount"] = $request["maxAmount"];
return json_encode($response);
?>
]]>
</scripting:text>
</scripting:script>
</scripting:component>
</processor-chain>
</when>
</choice>
</flow>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment