This repository was archived by the owner on Feb 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor.php
More file actions
executable file
·42 lines (31 loc) · 1.63 KB
/
processor.php
File metadata and controls
executable file
·42 lines (31 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
declare(strict_types=1);
use Damax\ChargeableApi\Bridge\Symfony\EventDispatcher\Event\PurchaseFinished;
use Damax\ChargeableApi\Bridge\Symfony\EventDispatcher\Events;
use Damax\ChargeableApi\Bridge\Symfony\EventDispatcher\NotificationStore;
use Damax\ChargeableApi\Identity\FixedIdentityFactory;
use Damax\ChargeableApi\Product\ChainResolver;
use Damax\ChargeableApi\Product\FixedProductResolver;
use Damax\ChargeableApi\Store\StoreProcessor;
use Damax\ChargeableApi\Store\WalletStore;
use Damax\ChargeableApi\Wallet\InMemoryWalletFactory;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Request;
require_once __DIR__ . '/../vendor/autoload.php';
// Creates fixed identity.
$identityFactory = new FixedIdentityFactory('john.doe');
// Resolves to single product.
$productResolver = new ChainResolver([new FixedProductResolver('Services', 10)]);
// Defines amount of credits in wallet for identities.
$walletFactory = new InMemoryWalletFactory(['john.doe' => 100, 'jane.doe' => 150]);
// Notifies about purchases.
$store = new NotificationStore(new WalletStore($walletFactory), $dispatcher = new EventDispatcher());
// Creates processor.
$processor = new StoreProcessor($store, $identityFactory, $productResolver);
$dispatcher->addListener(Events::PURCHASE_FINISHED, function (PurchaseFinished $event): void {
$product = $event->product();
// User 'john.doe' paid 10 credits for 'Services' product.
echo sprintf("User '%s' paid %d credits for '%s' product.\n", $event->identity(), $product->price()->toInteger(), $product->name());
});
// Run.
$processor->processRequest(Request::createFromGlobals());