NotificationStore   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 24
c 0
b 0
f 0
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A purchase() 0 13 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Damax\ChargeableApi\Bridge\Symfony\EventDispatcher;
6
7
use Damax\ChargeableApi\Bridge\Symfony\EventDispatcher\Event\PurchaseFinished;
8
use Damax\ChargeableApi\Bridge\Symfony\EventDispatcher\Event\PurchaseRejected;
9
use Damax\ChargeableApi\Identity\Identity;
10
use Damax\ChargeableApi\InsufficientFunds;
11
use Damax\ChargeableApi\Product\Product;
12
use Damax\ChargeableApi\Store\Receipt;
13
use Damax\ChargeableApi\Store\Store;
14
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15
16
final class NotificationStore implements Store
17
{
18
    private $store;
19
    private $dispatcher;
20
21
    public function __construct(Store $store, EventDispatcherInterface $dispatcher)
22
    {
23
        $this->store = $store;
24
        $this->dispatcher = $dispatcher;
25
    }
26
27
    public function purchase(Identity $identity, Product $product): Receipt
28
    {
29
        try {
30
            $receipt = $this->store->purchase($identity, $product);
31
        } catch (InsufficientFunds $e) {
32
            $this->dispatcher->dispatch(Events::PURCHASE_REJECTED, new PurchaseRejected($identity, $product));
33
34
            throw $e;
35
        }
36
37
        $this->dispatcher->dispatch(Events::PURCHASE_FINISHED, new PurchaseFinished($receipt));
38
39
        return $receipt;
40
    }
41
}
42