1. Go to this page and download the library: Download krak/effects library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
krak / effects example snippets
use function Krak\Effects\{handleEffects, expect};
// Domain Entity
final class ShoppingCart
{
public function checkOut(CheckOutShoppingCart $checkOutShoppingCart) {
// ... build up captueCharge command
$capturedCharge = expect(CapturedCharge::class, yield new CaptureCharge(/* args */));
}
}
// Domain Commands/Effects
final class CheckOutShoppingCart {}
final class CaptureCharge {}
final class CapturedCharge {}
// Application Command Handler
final class HandleCheckOutShoppingCart
{
public function __invoke(CheckOutShoppingCart $checkOutShoppingCart): void {
$shoppingCart = $this->shoppingCarts->get($checkOutShoppingCart->shoppingCart());
handleEffects($shoppingCart->checkOut($checkOutShoppingCart), [
CaptureCharge::class => function(CaptureCharge $captureCharge) {
return $this->paymentGateway->capture($captureCharge); // returns a CapturedCharge instance
}
]);
}
}
final class Product
{
public function checkout() {
yield from $this->raiseEffects();
}
private function raiseEffects() {
$result = yield new Effect1();
}
}
use Prewk\Result;
use Krak\Effects\Bridge\Result\MapEffectResults;
use function Krak\Effects\expect;
final class Product
{
public function syncInventory() {
expect(Result::class, yield from MapEffectResults::map(
$this->fetchInventoryFromERP(),
$this->fetchPricingRules(),
$this->pushInventoryToThirdParty()
))->mapErr(function() {
// set some error state maybe.
})->map(function() {
// set some success state maybe.
});
}
public function fetchInventoryFromERP(){
return function() {
return expect(Result::class, yield new FetchInventoryFromERP($this->productId));
};
}
public function fetchPricingRules(){
return function(InventoryFromERP $inventoryFromERP) {
return expect(Result::class, yield new FetchPricingRulesForProduct($this->productId))
->map(function(PricingRules $pricingRules) use ($inventoryFromERP) {
return [$inventoryFromERP, $pricingRules];
});
};
}
public function pushInventoryToThirdParty() {
return function(array $tup) {
[$inventoryFromERP, $pricingRules] = $tup;
// calculate final inventory using special logic
return expect(Result::class, yield new PushInventoryToThirdParty($finalInventory));
};
}
}
// in some application service
\Krak\Effects\handleEffects($product->syncInventory(), []); // with handlers accordingly
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.